|
2 | 2 |
|
3 | 3 | import { |
4 | 4 | NativeModules, |
5 | | - DeviceEventEmitter, |
| 5 | + NativeEventEmitter, |
6 | 6 | Platform, |
7 | 7 | } from 'react-native'; |
8 | 8 |
|
9 | | -var RNVoipPushNotificationManager = NativeModules.RNVoipPushNotificationManager; |
10 | | -var invariant = require('fbjs/lib/invariant'); |
| 9 | +const RNVoipPushNotificationManager = NativeModules.RNVoipPushNotificationManager; |
11 | 10 |
|
12 | | -var _notifHandlers = new Map(); |
| 11 | +const eventEmitter = new NativeEventEmitter(RNVoipPushNotificationManager); |
| 12 | +const _eventHandlers = new Map(); |
13 | 13 |
|
14 | | -var DEVICE_NOTIF_EVENT = 'voipRemoteNotificationReceived'; |
15 | | -var NOTIF_REGISTER_EVENT = 'voipRemoteNotificationsRegistered'; |
16 | | -var DEVICE_LOCAL_NOTIF_EVENT = 'voipLocalNotificationReceived'; |
| 14 | +// --- native unique event names |
| 15 | +const RNVoipPushRemoteNotificationsRegisteredEvent = "RNVoipPushRemoteNotificationsRegisteredEvent"; // --- 'register' |
| 16 | +const RNVoipPushRemoteNotificationReceivedEvent = "RNVoipPushRemoteNotificationReceivedEvent"; // --- 'notification' |
| 17 | +const RNVoipPushDidLoadWithEvents = "RNVoipPushDidLoadWithEvents"; // --- 'didLoadWithEvents' |
17 | 18 |
|
18 | 19 | export default class RNVoipPushNotification { |
19 | 20 |
|
20 | | - static wakeupByPush = (Platform.OS == 'ios' && RNVoipPushNotificationManager.wakeupByPush === 'true'); |
21 | | - |
22 | | - /** |
23 | | - * Schedules the localNotification for immediate presentation. |
24 | | - * |
25 | | - * details is an object containing: |
26 | | - * |
27 | | - * - `alertBody` : The message displayed in the notification alert. |
28 | | - * - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view"; |
29 | | - * - `soundName` : The sound played when the notification is fired (optional). |
30 | | - * - `category` : The category of this notification, required for actionable notifications (optional). |
31 | | - * - `userInfo` : An optional object containing additional notification data. |
32 | | - */ |
33 | | - static presentLocalNotification(details) { |
34 | | - RNVoipPushNotificationManager.presentLocalNotification(details); |
35 | | - } |
36 | | - |
37 | 21 | /** |
38 | 22 | * Attaches a listener to remote notification events while the app is running |
39 | 23 | * in the foreground or the background. |
40 | 24 | * |
41 | 25 | * Valid events are: |
42 | 26 | * |
43 | | - * - `notification` : Fired when a remote notification is received. The |
44 | | - * handler will be invoked with an instance of `PushNotificationIOS`. |
45 | | - * - `register`: Fired when the user registers for remote notifications. The |
46 | | - * handler will be invoked with a hex string representing the deviceToken. |
| 27 | + * - `notification` : Fired when a remote notification is received. |
| 28 | + * - `register`: Fired when the user registers for remote notifications. |
| 29 | + * - `didLoadWithEvents`: Fired when the user have initially subscribed any listener and has cached events already. |
47 | 30 | */ |
48 | 31 | static addEventListener(type, handler) { |
49 | | - invariant( |
50 | | - type === 'notification' || type === 'register' || type === 'localNotification', |
51 | | - 'RNVoipPushNotificationManager only supports `notification`, `register` and `localNotification` events' |
52 | | - ); |
53 | | - var listener; |
| 32 | + let listener; |
54 | 33 | if (type === 'notification') { |
55 | | - listener = DeviceEventEmitter.addListener( |
56 | | - DEVICE_NOTIF_EVENT, |
57 | | - (notifData) => { |
58 | | - handler(new RNVoipPushNotification(notifData)); |
| 34 | + listener = eventEmitter.addListener( |
| 35 | + RNVoipPushRemoteNotificationReceivedEvent, |
| 36 | + (notificationPayload) => { |
| 37 | + handler(notificationPayload); |
59 | 38 | } |
60 | 39 | ); |
61 | | - } else if (type === 'localNotification') { |
62 | | - listener = DeviceEventEmitter.addListener( |
63 | | - DEVICE_LOCAL_NOTIF_EVENT, |
64 | | - (notifData) => { |
65 | | - handler(new RNVoipPushNotification(notifData)); |
| 40 | + } else if (type === 'register') { |
| 41 | + listener = eventEmitter.addListener( |
| 42 | + RNVoipPushRemoteNotificationsRegisteredEvent, |
| 43 | + (deviceToken) => { |
| 44 | + handler(deviceToken); |
66 | 45 | } |
67 | 46 | ); |
68 | | - } else if (type === 'register') { |
69 | | - listener = DeviceEventEmitter.addListener( |
70 | | - NOTIF_REGISTER_EVENT, |
71 | | - (registrationInfo) => { |
72 | | - handler(registrationInfo.deviceToken); |
| 47 | + } else if (type === 'didLoadWithEvents') { |
| 48 | + listener = eventEmitter.addListener( |
| 49 | + RNVoipPushDidLoadWithEvents, |
| 50 | + (events) => { |
| 51 | + handler(events); |
73 | 52 | } |
74 | 53 | ); |
| 54 | + } else { |
| 55 | + return; |
75 | 56 | } |
76 | | - _notifHandlers.set(handler, listener); |
| 57 | + |
| 58 | + // --- we only support one listener at a time, remove to prevent leak |
| 59 | + RNVoipPushNotification.removeEventListener(type); |
| 60 | + _eventHandlers.set(type, listener); |
77 | 61 | } |
78 | 62 |
|
79 | 63 | /** |
80 | 64 | * Removes the event listener. Do this in `componentWillUnmount` to prevent |
81 | 65 | * memory leaks |
82 | 66 | */ |
83 | | - static removeEventListener(type, handler) { |
84 | | - invariant( |
85 | | - type === 'notification' || type === 'register' || type === 'localNotification', |
86 | | - 'RNVoipPushNotification only supports `notification`, `register` and `localNotification` events' |
87 | | - ); |
88 | | - var listener = _notifHandlers.get(handler); |
| 67 | + static removeEventListener(type) { |
| 68 | + let listener = _eventHandlers.get(type); |
89 | 69 | if (!listener) { |
90 | 70 | return; |
91 | 71 | } |
92 | 72 | listener.remove(); |
93 | | - _notifHandlers.delete(handler); |
94 | | - } |
95 | | - |
96 | | - /** |
97 | | - * Requests notification permissions from iOS, prompting the user's |
98 | | - * dialog box. By default, it will request all notification permissions, but |
99 | | - * a subset of these can be requested by passing a map of requested |
100 | | - * permissions. |
101 | | - * The following permissions are supported: |
102 | | - * |
103 | | - * - `alert` |
104 | | - * - `badge` |
105 | | - * - `sound` |
106 | | - * |
107 | | - * If a map is provided to the method, only the permissions with truthy values |
108 | | - * will be requested. |
109 | | - */ |
110 | | - static requestPermissions(permissions) { |
111 | | - var requestedPermissions = {}; |
112 | | - if (permissions) { |
113 | | - requestedPermissions = { |
114 | | - alert: !!permissions.alert, |
115 | | - badge: !!permissions.badge, |
116 | | - sound: !!permissions.sound |
117 | | - }; |
118 | | - } else { |
119 | | - requestedPermissions = { |
120 | | - alert: true, |
121 | | - badge: true, |
122 | | - sound: true |
123 | | - }; |
124 | | - } |
125 | | - RNVoipPushNotificationManager.requestPermissions(requestedPermissions); |
| 73 | + _eventHandlers.delete(type); |
126 | 74 | } |
127 | 75 |
|
128 | 76 | /** |
@@ -150,63 +98,4 @@ export default class RNVoipPushNotification { |
150 | 98 | RNVoipPushNotificationManager.onVoipNotificationCompleted(uuid); |
151 | 99 | } |
152 | 100 |
|
153 | | - /** |
154 | | - * You will never need to instantiate `RNVoipPushNotification` yourself. |
155 | | - * Listening to the `notification` event and invoking |
156 | | - * `popInitialNotification` is sufficient |
157 | | - */ |
158 | | - constructor(nativeNotif) { |
159 | | - this._data = {}; |
160 | | - |
161 | | - // Extract data from Apple's `aps` dict as defined: |
162 | | - |
163 | | - // https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Chapters/ApplePushService.html |
164 | | - |
165 | | - Object.keys(nativeNotif).forEach((notifKey) => { |
166 | | - var notifVal = nativeNotif[notifKey]; |
167 | | - if (notifKey === 'aps') { |
168 | | - this._alert = notifVal.alert; |
169 | | - this._sound = notifVal.sound; |
170 | | - this._badgeCount = notifVal.badge; |
171 | | - } else { |
172 | | - this._data[notifKey] = notifVal; |
173 | | - } |
174 | | - }); |
175 | | - } |
176 | | - |
177 | | - /** |
178 | | - * An alias for `getAlert` to get the notification's main message string |
179 | | - */ |
180 | | - getMessage() { |
181 | | - // alias because "alert" is an ambiguous name |
182 | | - return this._alert; |
183 | | - } |
184 | | - |
185 | | - /** |
186 | | - * Gets the sound string from the `aps` object |
187 | | - */ |
188 | | - getSound() { |
189 | | - return this._sound; |
190 | | - } |
191 | | - |
192 | | - /** |
193 | | - * Gets the notification's main message from the `aps` object |
194 | | - */ |
195 | | - getAlert() { |
196 | | - return this._alert; |
197 | | - } |
198 | | - |
199 | | - /** |
200 | | - * Gets the badge count number from the `aps` object |
201 | | - */ |
202 | | - getBadgeCount() { |
203 | | - return this._badgeCount; |
204 | | - } |
205 | | - |
206 | | - /** |
207 | | - * Gets the data object on the notif |
208 | | - */ |
209 | | - getData() { |
210 | | - return this._data; |
211 | | - } |
212 | 101 | } |
0 commit comments