-
Notifications
You must be signed in to change notification settings - Fork 0
/
NotificationHandler.js
80 lines (62 loc) · 2.32 KB
/
NotificationHandler.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* eslint-disable prettier/prettier */
import PushNotification from 'react-native-push-notification';
import messaging from '@react-native-firebase/messaging';
class NotificationHandler {
onNotification(notification) {
console.log('NotificationHandler Notif Masuk:', notification);
if (typeof this._onNotification === 'function') {
this._onNotification(notification);
}
}
async onRegister(token) {
console.log('NotificationHandler Register:', token.token);
if (typeof this._onRegister === 'function') {
this._onRegister(token);
}
}
onAction(notification) {
console.log ('Notification action received:');
console.log(notification.action);
console.log(notification);
if(notification.action === 'Yes') {
PushNotification.invokeApp(notification);
}
}
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError(err) {
console.log(err);
}
attachRegister(handler) {
this._onRegister = handler;
}
attachNotification(handler) {
this._onNotification = handler;
}
}
const handler = new NotificationHandler();
PushNotification.configure({
// (optional) Called when Token is generated (iOS and Android)
onRegister: handler.onRegister.bind(handler),
// (required) Called when a remote or local notification is opened or received
onNotification: handler.onNotification.bind(handler),
// (optional) Called when Action is pressed (Android)
onAction: handler.onAction.bind(handler),
// (optional) Called when the user fails to register for remote notifications. Typically occurs when APNS is having issues, or the device is a simulator. (iOS)
onRegistrationError: handler.onRegistrationError.bind(handler),
// IOS ONLY (optional): default: all - Permissions to register.
permissions: {
alert: true,
badge: true,
sound: true,
},
// Should the initial notification be popped automatically
// default: true
popInitialNotification: true,
/**
* (optional) default: true
* - Specified if permissions (ios) and token (android and ios) will requested or not,
* - if not, you must call PushNotificationsHandler.requestPermissions() later
*/
requestPermissions: true,
});
export default handler;