1 | 'use strict';
|
2 |
|
3 | var { NativeModules, DeviceEventEmitter } = require('react-native');
|
4 |
|
5 | var RNPushNotification = NativeModules.RNPushNotification;
|
6 | var _notifHandlers = new Map();
|
7 |
|
8 | var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
|
9 | var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
|
10 | var REMOTE_FETCH_EVENT = 'remoteFetch';
|
11 |
|
12 | var NotificationsComponent = function() {};
|
13 |
|
14 | NotificationsComponent.prototype.getInitialNotification = function() {
|
15 | return RNPushNotification.getInitialNotification().then(function(notification) {
|
16 | if (notification && notification.dataJSON) {
|
17 | return JSON.parse(notification.dataJSON);
|
18 | }
|
19 | return null;
|
20 | });
|
21 | };
|
22 |
|
23 | NotificationsComponent.prototype.requestPermissions = function(senderID: string) {
|
24 | RNPushNotification.requestPermissions(senderID);
|
25 | };
|
26 |
|
27 | NotificationsComponent.prototype.cancelLocalNotifications = function(details: Object) {
|
28 | RNPushNotification.cancelLocalNotifications(details);
|
29 | };
|
30 |
|
31 | NotificationsComponent.prototype.clearLocalNotification = function(details: Object) {
|
32 | RNPushNotification.clearLocalNotification(details);
|
33 | };
|
34 |
|
35 | NotificationsComponent.prototype.cancelAllLocalNotifications = function() {
|
36 | RNPushNotification.cancelAllLocalNotifications();
|
37 | };
|
38 |
|
39 | NotificationsComponent.prototype.presentLocalNotification = function(details: Object) {
|
40 | RNPushNotification.presentLocalNotification(details);
|
41 | };
|
42 |
|
43 | NotificationsComponent.prototype.scheduleLocalNotification = function(details: Object) {
|
44 | RNPushNotification.scheduleLocalNotification(details);
|
45 | };
|
46 |
|
47 | NotificationsComponent.prototype.setApplicationIconBadgeNumber = function(number: number) {
|
48 | if (!RNPushNotification.setApplicationIconBadgeNumber) {
|
49 | return;
|
50 | }
|
51 | RNPushNotification.setApplicationIconBadgeNumber(number);
|
52 | };
|
53 |
|
54 | NotificationsComponent.prototype.abandonPermissions = function() {
|
55 |
|
56 | };
|
57 |
|
58 | NotificationsComponent.prototype.deleteTokens = function() {
|
59 | RNPushNotification.deleteTokens();
|
60 | };
|
61 |
|
62 | NotificationsComponent.prototype.checkPermissions = function(callback: Function) {
|
63 | RNPushNotification.checkPermissions().then(alert => callback({ alert }));
|
64 | };
|
65 |
|
66 | NotificationsComponent.prototype.addEventListener = function(type: string, handler: Function) {
|
67 | var listener;
|
68 | if (type === 'notification') {
|
69 | listener = DeviceEventEmitter.addListener(DEVICE_NOTIF_EVENT, function(notifData) {
|
70 | var data = JSON.parse(notifData.dataJSON);
|
71 | handler(data);
|
72 | });
|
73 | } else if (type === 'register') {
|
74 | listener = DeviceEventEmitter.addListener(NOTIF_REGISTER_EVENT, function(registrationInfo) {
|
75 | handler(registrationInfo.deviceToken, registrationInfo.tokenType);
|
76 | });
|
77 | } else if (type === 'remoteFetch') {
|
78 | listener = DeviceEventEmitter.addListener(REMOTE_FETCH_EVENT, function(notifData) {
|
79 | var notificationData = JSON.parse(notifData.dataJSON);
|
80 | handler(notificationData);
|
81 | });
|
82 | }
|
83 |
|
84 | _notifHandlers.set(type, listener);
|
85 | };
|
86 |
|
87 | NotificationsComponent.prototype.removeEventListener = function(type: string, handler: Function) {
|
88 | var listener = _notifHandlers.get(type);
|
89 | if (!listener) {
|
90 | return;
|
91 | }
|
92 | listener.remove();
|
93 | _notifHandlers.delete(type);
|
94 | };
|
95 |
|
96 | NotificationsComponent.prototype.registerNotificationActions = function(details: Object) {
|
97 | RNPushNotification.registerNotificationActions(details);
|
98 | };
|
99 |
|
100 | NotificationsComponent.prototype.clearAllNotifications = function() {
|
101 | RNPushNotification.clearAllNotifications();
|
102 | };
|
103 |
|
104 | module.exports = {
|
105 | state: false,
|
106 | component: new NotificationsComponent(),
|
107 | };
|