forked from OneSignal/react-native-onesignal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
375 lines (289 loc) · 10.6 KB
/
index.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
'use strict';
import { NativeModules, NativeEventEmitter, NetInfo, Platform } from 'react-native';
import invariant from 'invariant';
const RNOneSignal = NativeModules.OneSignal;
const eventBroadcastNames = [
'OneSignal-remoteNotificationReceived',
'OneSignal-remoteNotificationOpened',
'OneSignal-idsAvailable',
'OneSignal-emailSubscription'
];
var oneSignalEventEmitter;
var _eventNames = [ "received", "opened", "ids", "emailSubscription"];
var _notificationHandler = new Map();
var _notificationCache = new Map();
var _listeners = [];
if (RNOneSignal != null) {
oneSignalEventEmitter = new NativeEventEmitter(RNOneSignal);
for(var i = 0; i < eventBroadcastNames.length; i++) {
var eventBroadcastName = eventBroadcastNames[i];
var eventName = _eventNames[i];
_listeners[eventName] = handleEventBroadcast(eventName, eventBroadcastName)
}
}
function handleEventBroadcast(type, broadcast) {
return oneSignalEventEmitter.addListener(
broadcast, (notification) => {
// Check if we have added listener for this type yet
// Cache the result first if we have not.
var handler = _notificationHandler.get(type);
if (handler) {
handler(notification);
} else {
_notificationCache.set(type, notification);
}
}
);
}
function checkIfInitialized() {
return RNOneSignal != null;
}
export default class OneSignal {
static addEventListener(type, handler) {
if (!checkIfInitialized()) return;
// Listen to events of notification received, opened, device registered and IDSAvailable.
invariant(
type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription',
'OneSignal only supports `received`, `opened`, and `ids` events'
);
_notificationHandler.set(type, handler);
if (type == 'opened') {
RNOneSignal.didSetNotificationOpenedHandler();
}
// Check if there is a cache for this type of event
var cache = _notificationCache.get(type);
if (handler && cache) {
handler(cache);
_notificationCache.delete(type);
}
}
static removeEventListener(type, handler) {
if (!checkIfInitialized()) return;
invariant(
type === 'received' || type === 'opened' || type === 'ids' || type == 'emailSubscription',
'OneSignal only supports `received`, `opened`, and `ids` events'
);
_notificationHandler.delete(type);
}
static clearListeners() {
if (!checkIfInitialized()) return;
for(var i = 0; i < _eventNames.length; i++) {
_listeners[_eventNames].remove();
}
}
static registerForPushNotifications() {
if (!checkIfInitialized()) return;
if (Platform.OS === 'ios') {
RNOneSignal.registerForPushNotifications();
} else {
console.log("This function is not supported on Android");
}
}
static promptForPushNotificationsWithUserResponse(callback) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'ios') {
invariant(
typeof callback === 'function',
'Must provide a valid callback'
);
RNOneSignal.promptForPushNotificationsWithUserResponse(callback);
} else {
console.log("This function is not supported on Android");
}
}
static requestPermissions(permissions) {
if (!checkIfInitialized()) return;
var requestedPermissions = {};
if (Platform.OS === 'ios') {
if (permissions) {
requestedPermissions = {
alert: !!permissions.alert,
badge: !!permissions.badge,
sound: !!permissions.sound
};
} else {
requestedPermissions = {
alert: true,
badge: true,
sound: true
};
}
RNOneSignal.requestPermissions(requestedPermissions);
} else {
console.log("This function is not supported on Android");
}
}
static configure() {
if (!checkIfInitialized()) return;
RNOneSignal.configure();
}
static init(appId, iOSSettings) {
if (!checkIfInitialized()) return;
if (Platform.OS == 'ios') {
RNOneSignal.initWithAppId(appId, iOSSettings);
} else {
RNOneSignal.init(appId);
}
}
static checkPermissions(callback) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'ios') {
invariant(
typeof callback === 'function',
'Must provide a valid callback'
);
RNOneSignal.checkPermissions(callback);
} else {
console.log("This function is not supported on Android");
}
}
static promptForPushNotificationPermissions(callback) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'ios') {
RNOneSignal.promptForPushNotificationPermissions(callback);
} else {
console.log('This function is not supported on Android');
}
}
static getPermissionSubscriptionState(callback) {
if (!checkIfInitialized()) return;
invariant(
typeof callback === 'function',
'Must provide a valid callback'
);
RNOneSignal.getPermissionSubscriptionState(callback);
}
static sendTag(key, value) {
if (!checkIfInitialized()) return;
RNOneSignal.sendTag(key, value);
}
static sendTags(tags) {
if (!checkIfInitialized()) return;
RNOneSignal.sendTags(tags || {});
}
static getTags(next) {
if (!checkIfInitialized()) return;
RNOneSignal.getTags(next);
}
static deleteTag(key) {
if (!checkIfInitialized()) return;
RNOneSignal.deleteTag(key);
}
static enableVibrate(enable) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'android') {
RNOneSignal.enableVibrate(enable);
} else {
console.log("This function is not supported on iOS");
}
}
static enableSound(enable) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'android') {
RNOneSignal.enableSound(enable);
} else {
console.log("This function is not supported on iOS");
}
}
static setEmail(email, emailAuthCode, callback) {
if (!checkIfInitialized()) return;
if (emailAuthCode == undefined) {
//emailAuthCode is an optional parameter
//since JS does not support function overloading,
//unauthenticated setEmail calls will have emailAuthCode as the callback
RNOneSignal.setUnauthenticatedEmail(email, function(){});
} else if (callback == undefined && typeof emailAuthCode == 'function') {
RNOneSignal.setUnauthenticatedEmail(email, emailAuthCode);
} else if (callback == undefined) {
RNOneSignal.setEmail(email, emailAuthCode, function(){});
} else {
RNOneSignal.setEmail(email, emailAuthCode, callback);
}
}
static logoutEmail(callback) {
if (!checkIfInitialized()) return;
invariant(
typeof callback === 'function',
'Must provide a valid callback'
);
RNOneSignal.logoutEmail(callback);
}
static setLocationShared(shared) {
if (!checkIfInitialized()) return;
RNOneSignal.setLocationShared(shared);
}
static setSubscription(enable) {
if (!checkIfInitialized()) return;
RNOneSignal.setSubscription(enable);
}
static promptLocation() {
if (!checkIfInitialized()) return;
//Supported in both iOS & Android
RNOneSignal.promptLocation();
}
static inFocusDisplaying(displayOption) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'android') {
//Android: Set Display option of the notifications. displayOption is of type OSInFocusDisplayOption
// 0 -> None, 1 -> InAppAlert, 2 -> Notification
RNOneSignal.inFocusDisplaying(displayOption);
} else {
//iOS: displayOption is a number, 0 -> None, 1 -> InAppAlert, 2 -> Notification
RNOneSignal.setInFocusDisplayType(displayOption);
}
}
static postNotification(contents, data, player_id, otherParameters) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'android') {
RNOneSignal.postNotification(JSON.stringify(contents), JSON.stringify(data), player_id, JSON.stringify(otherParameters));
} else {
RNOneSignal.postNotification(contents, data, player_id, otherParameters);
}
}
static clearOneSignalNotifications() {
if (!checkIfInitialized()) return;
if (Platform.OS === 'android') {
RNOneSignal.clearOneSignalNotifications();
} else {
console.log("This function is not supported on iOS");
}
}
static cancelNotification(id) {
if (!checkIfInitialized()) return;
if (Platform.OS === 'android') {
RNOneSignal.cancelNotification(id);
} else {
console.log("This function is not supported on iOS");
}
}
//Sends MD5 and SHA1 hashes of the user's email address (https://documentation.onesignal.com/docs/ios-sdk-api#section-synchashedemail)
static syncHashedEmail(email) {
if (!checkIfInitialized()) return;
RNOneSignal.syncHashedEmail(email);
}
static setLogLevel(nsLogLevel, visualLogLevel) {
if (!checkIfInitialized()) return;
RNOneSignal.setLogLevel(nsLogLevel, visualLogLevel);
}
static setRequiresUserPrivacyConsent(required) {
if (!checkIfInitialized()) return;
RNOneSignal.setRequiresUserPrivacyConsent(required);
}
static provideUserConsent(granted) {
if (!checkIfInitialized()) return;
RNOneSignal.provideUserConsent(granted);
}
static userProvidedPrivacyConsent() {
if (!checkIfInitialized()) return;
//returns a promise
return RNOneSignal.userProvidedPrivacyConsent();
}
static setExternalUserId(externalId) {
if (!checkIfInitialized()) return;
RNOneSignal.setExternalUserId(externalId);
}
static removeExternalUserId() {
if (!checkIfInitialized()) return;
RNOneSignal.removeExternalUserId();
}
}