forked from wix/react-native-notifications
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ios.js
270 lines (231 loc) · 9.22 KB
/
index.ios.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/**
* @flow
*/
"use strict";
import { NativeModules, DeviceEventEmitter, NativeAppEventEmitter } from "react-native";
import Map from "core-js/library/es6/map";
import uuid from "uuid";
const NativeRNNotifications = NativeModules.RNNotifications; // eslint-disable-line no-unused-vars
import IOSNotification from "./notification.ios";
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT = "remoteNotificationsRegistered";
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT = "remoteNotificationsRegistrationFailed";
export const DEVICE_PUSH_KIT_REGISTERED_EVENT = "pushKitRegistered";
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = "notificationReceivedForeground";
export const DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT = "notificationReceivedBackground";
export const DEVICE_NOTIFICATION_OPENED_EVENT = "notificationOpened";
const DEVICE_NOTIFICATION_ACTION_RECEIVED = "notificationActionReceived";
const _exportedEvents = [
DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
DEVICE_PUSH_KIT_REGISTERED_EVENT,
DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT,
DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT,
DEVICE_NOTIFICATION_OPENED_EVENT
];
const _notificationHandlers = new Map();
const _actionHandlers = new Map();
let _actionListener;
export class NotificationAction {
options: Object;
handler: Function;
constructor(options: Object, handler: Function) {
this.options = options;
this.handler = handler;
}
}
export class NotificationCategory {
options: Object;
constructor(options: Object) {
this.options = options;
}
}
export default class NotificationsIOS {
/**
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
*
* Valid events are:
*
* - `remoteNotificationsRegistered` : Fired when the user registers for remote notifications. The handler will be invoked with a hex string representing the deviceToken.
* - `notificationReceivedForeground` : Fired when a notification (local / remote) is received when app is on foreground state.
* - `notificationReceivedBackground`: Fired when a background notification is received.
* - `notificationOpened`: Fired when a notification (local / remote) is opened.
*/
static addEventListener(type: string, handler: Function) {
if (_exportedEvents.indexOf(type) !== -1) {
let listener;
if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT) {
listener = DeviceEventEmitter.addListener(
DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
registration => handler(registration.deviceToken)
);
} else if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT) {
listener = DeviceEventEmitter.addListener(
DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
error => handler(error)
);
} else if (type === DEVICE_PUSH_KIT_REGISTERED_EVENT) {
listener = DeviceEventEmitter.addListener(
DEVICE_PUSH_KIT_REGISTERED_EVENT,
registration => handler(registration.pushKitToken)
);
} else {
listener = DeviceEventEmitter.addListener(
type,
notification => handler(new IOSNotification(notification))
);
}
_notificationHandlers.set(handler, listener);
}
}
/**
* Removes the event listener. Do this in `componentWillUnmount` to prevent
* memory leaks
*/
static removeEventListener(type: string, handler: Function) {
if (_exportedEvents.indexOf(type) !== -1) {
const listener = _notificationHandlers.get(handler);
if (listener) {
listener.remove();
_notificationHandlers.delete(handler);
}
}
}
static _actionHandlerDispatcher(action: Object) {
const actionHandler = _actionHandlers.get(action.identifier);
if (actionHandler) {
action.notification = new IOSNotification(action.notification);
actionHandler(action, () => {
NativeRNNotifications.completionHandler(action.completionKey);
});
}
}
/**
* Sets the notification categories
*/
static requestPermissions(categories: Array<NotificationCategory>) {
let notificationCategories = [];
if (categories) {
// subscribe once for all actions
_actionListener = NativeAppEventEmitter.addListener(DEVICE_NOTIFICATION_ACTION_RECEIVED, this._actionHandlerDispatcher.bind(this));
notificationCategories = categories.map(category => {
return Object.assign({}, category.options, {
actions: category.options.actions.map(action => {
// subscribe to action event
_actionHandlers.set(action.options.identifier, action.handler);
return action.options;
})
});
});
}
NativeRNNotifications.requestPermissionsWithCategories(notificationCategories);
}
/**
* Unregister for all remote notifications received via Apple Push Notification service.
*/
static abandonPermissions() {
NativeRNNotifications.abandonPermissions();
}
/**
* Removes the event listener. Do this in `componentWillUnmount` to prevent
* memory leaks
*/
static resetCategories() {
if (_actionListener) {
_actionListener.remove();
}
_actionHandlers.clear();
}
static getBadgesCount(callback: Function) {
NativeRNNotifications.getBadgesCount(callback);
}
static setBadgesCount(count: number) {
NativeRNNotifications.setBadgesCount(count);
}
static registerPushKit() {
NativeRNNotifications.registerPushKit();
}
static backgroundTimeRemaining(callback: Function) {
NativeRNNotifications.backgroundTimeRemaining(callback);
}
static consumeBackgroundQueue() {
NativeRNNotifications.consumeBackgroundQueue();
}
static log(message: string) {
NativeRNNotifications.log(message);
}
static async getInitialNotification() {
const notification = await NativeRNNotifications.getInitialNotification();
if (notification) {
return new IOSNotification(notification);
} else {
return undefined;
}
}
/**
* Presenting local notification
*
* notification is an object containing:
*
* - `alertBody` : The message displayed in the notification alert.
* - `alertTitle` : The message title displayed in the notification.
* - `alertAction` : The "action" displayed beneath an actionable notification. Defaults to "view";
* - `soundName` : The sound played when the notification is fired (optional).
* - `silent` : If true, the notification sound will be suppressed (optional).
* - `category` : The category of this notification, required for actionable notifications (optional).
* - `userInfo` : An optional object containing additional notification data.
* - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
* - `imageURL` : URL for an image that will be downloaded and inserted into the notification (optional).
*
* id (optional) is a string for the desired notification id. If none is provided, a UUID is generated
*/
static localNotification(notification: Object, id?: String) {
const notificationId = id || uuid.v4();
NativeRNNotifications.localNotification(notification, notificationId);
return notificationId;
}
static cancelLocalNotification(notificationId: String) {
NativeRNNotifications.cancelLocalNotification(notificationId);
}
static cancelAllLocalNotifications() {
NativeRNNotifications.cancelAllLocalNotifications();
}
static isRegisteredForRemoteNotifications() {
return NativeRNNotifications.isRegisteredForRemoteNotifications();
}
static checkPermissions() {
return NativeRNNotifications.checkPermissions();
}
/**
* Remove all delivered notifications from Notification Center
*/
static removeAllDeliveredNotifications() {
return NativeRNNotifications.removeAllDeliveredNotifications();
}
/**
* Removes the specified notifications from Notification Center
*
* @param identifiers Array of notification identifiers
*/
static removeDeliveredNotifications(identifiers: Array<String>) {
return NativeRNNotifications.removeDeliveredNotifications(identifiers);
}
/**
* Provides you with a list of the app’s notifications that are still displayed in Notification Center
*
* @param callback Function which receive an array of delivered notifications
*
* A delivered notification is an object containing:
*
* - `identifier` : The identifier of this notification.
* - `alertBody` : The message displayed in the notification alert.
* - `alertTitle` : The message title displayed in the notification.
* - `category` : The category of this notification, if has one.
* - `userInfo` : An optional object containing additional notification data.
* - `thread-id` : The thread identifier of this notification, if has one.
* - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
*/
static getDeliveredNotifications(callback: (notifications: Array<Object>) => void) {
return NativeRNNotifications.getDeliveredNotifications(callback);
}
}