UNPKG

4 kBJavaScriptView Raw
1'use strict';
2
3var { NativeModules, DeviceEventEmitter } = require('react-native');
4
5var RNPushNotification = NativeModules.RNPushNotification;
6var _notifHandlers = new Map();
7
8var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
9var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
10var REMOTE_FETCH_EVENT = 'remoteFetch';
11
12var NotificationsComponent = function() {};
13
14NotificationsComponent.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
23NotificationsComponent.prototype.requestPermissions = function(senderID: string) {
24 RNPushNotification.requestPermissions(senderID);
25};
26
27NotificationsComponent.prototype.cancelLocalNotifications = function(details: Object) {
28 RNPushNotification.cancelLocalNotifications(details);
29};
30
31NotificationsComponent.prototype.clearLocalNotification = function(details: Object) {
32 RNPushNotification.clearLocalNotification(details);
33};
34
35NotificationsComponent.prototype.cancelAllLocalNotifications = function() {
36 RNPushNotification.cancelAllLocalNotifications();
37};
38
39NotificationsComponent.prototype.presentLocalNotification = function(details: Object) {
40 RNPushNotification.presentLocalNotification(details);
41};
42
43NotificationsComponent.prototype.scheduleLocalNotification = function(details: Object) {
44 RNPushNotification.scheduleLocalNotification(details);
45};
46
47NotificationsComponent.prototype.setApplicationIconBadgeNumber = function(number: number) {
48 if (!RNPushNotification.setApplicationIconBadgeNumber) {
49 return;
50 }
51 RNPushNotification.setApplicationIconBadgeNumber(number);
52};
53
54NotificationsComponent.prototype.abandonPermissions = function() {
55 /* Void */
56};
57
58NotificationsComponent.prototype.deleteTokens = function() {
59 RNPushNotification.deleteTokens();
60};
61
62NotificationsComponent.prototype.checkPermissions = function(callback: Function) {
63 RNPushNotification.checkPermissions().then(alert => callback({ alert }));
64};
65
66NotificationsComponent.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
87NotificationsComponent.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
96NotificationsComponent.prototype.registerNotificationActions = function(details: Object) {
97 RNPushNotification.registerNotificationActions(details);
98};
99
100NotificationsComponent.prototype.clearAllNotifications = function() {
101 RNPushNotification.clearAllNotifications();
102};
103
104NotificationsComponent.prototype.clearCachedNotifications = function(nodeType: string) {
105 RNPushNotification.clearCachedNotifications(nodeType);
106};
107
108NotificationsComponent.prototype.setUserId = function(userId: string) {
109 RNPushNotification.setUserId(userId);
110};
111
112NotificationsComponent.prototype.clearUserId = function() {
113 RNPushNotification.clearUserId();
114};
115
116module.exports = {
117 state: false,
118 component: new NotificationsComponent(),
119};