-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
486 lines (426 loc) · 12.7 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
const { RNPushe } = NativeModules;
let pusheEventEmitter = new NativeEventEmitter(RNPushe);
const EVENTS_TYPES = [
// Notification
"received",
"clicked",
"dismissed",
"button_clicked",
"custom_content_received",
]
// key = events that user can attach handlers on them
// value = broadcast events that are emitted from the native
// and are corrospond to the ones in (co.pushe.plus.utils)
const _pusheEvents = new Map([
// Notification
[EVENTS_TYPES[0], "Pushe-NotificationReceived"],
[EVENTS_TYPES[1], "Pushe-Clicked"],
[EVENTS_TYPES[2], "Pushe-Dismissed"],
[EVENTS_TYPES[3], "Pushe-ButtonClicked"],
[EVENTS_TYPES[4], "Pushe-CustomContentReceived"],
]);
// store all broadcastListeners (actually their returned subscriptions) and their handlers in this object
const _broadcastListeners = {};
const _cachedNotification = new Map();
const _userEventHandlers = new Map();
function _attachEventBroadcasts(event, nativeBroadcastEvent) {
return pusheEventEmitter.addListener(nativeBroadcastEvent, (notification) => {
let userEventHandler = _userEventHandlers.get(event);
// Check if user already set a handler
// for this event type then call it
// if not cache notification for later
if (userEventHandler) {
userEventHandler(notification);
} else {
_cachedNotification.set(event, notification);
}
});
}
// Start point for attaching nativeBrodcast events
if (RNPushe !== null && Platform.OS === 'android') {
_pusheEvents.forEach(function(nativeBroadcastEvent, event) {
_broadcastListeners[event] = _attachEventBroadcasts(event, nativeBroadcastEvent);
});
}
class Pushe {
/**
* Available events type to add listener on them
*/
static EVENTS = {
RECEIVED: EVENTS_TYPES[0],
CLICKED: EVENTS_TYPES[1],
DISMISSED: EVENTS_TYPES[2],
BUTTON_CLICKED: EVENTS_TYPES[3],
CUSTOM_CONTENT_RECEIVED: EVENTS_TYPES[4],
}
static ANDROID_ID_TYPES = {
CUSTOM_ID: 'CUSTOM_ID',
ANDROID_ID: 'ANDROID_ID',
ADVERTISEMENT_ID: 'ADVERTISEMENT_ID'
}
static addEventListener(eventType, eventHandler) {
if (!eventHandler) return;
// save user eventHandler
_userEventHandlers.set(eventType, eventHandler);
// If already we have a cached notification for this eventType
// call userEventHandler with this cached notification
const cachedNotification = _cachedNotification.get(eventType);
if (cachedNotification) {
eventHandler(cachedNotification);
_cachedNotification.delete(eventType);
}
}
static removeEventListener(eventType) {
_userEventHandlers.delete(eventType);
}
static clearListeners() {
_pusheEvents.forEach((_value, key) => {
pusheEventEmitter.removeAllListeners(_broadcastListeners[key]);
_broadcastListeners.delete(key);
});
}
/**
* for android:
* Only for apps that activate GDPR feature.
* Calling this means user gave consent due to GDPR rule and Pushe is allowed to work.
* (Calling this only once, is enough)
*
* <b>By default this code is not needed to ba called for Android</b>
*
* for ios:
* for ios 13.6 and later, auto initializtion feature doesnt work. so call this function to
* initialize ios-sdk manually.
*/
static initialize() {
return RNPushe.initialize();
}
/**
* Check if Pushe is initialized or not
*
* it will return promise of type boolean
*
* @return {Promise<boolean>} Promise - if no parameter passed
*/
static isInitialized() {
if (Platform.OS === 'ios') return;
return RNPushe.isInitialized();
}
/**
* Set special permission.
*/
static setUserConsentGiven() {
if (Platform.OS == 'ios') return;
return RNPushe.setUserConsentGiven();
}
/**
* Check if Pushe is registered or not
*
* it will return promise of type boolean
*
* @return {Promise<boolean>} Promise - if no parameter passed
*/
static isRegistered() {
return RNPushe.isRegistered();
}
/**
* it will called when push registertion is completed
*/
static onPusheRegisterationComplete() {
if (Platform.OS === 'ios') return;
return RNPushe.onRegistrationComplete();
}
/**
* it will called when push initialization is completed
*/
static onPusheInitializationComplete() {
if (Platform.OS === 'ios') return;
return RNPushe.onInitializationComplete();
}
/**
* get advertisingId
* it will return a promise
*/
static getGoogleAdvertisingId() {
if (Platform.OS === 'ios') return;
return RNPushe.getGoogleAdvertisingId();
}
/**
* Returns androidId of device (No function in iOS)
* @deprecated since 2.1.1. In order to get android id, use <code>getDeviceId()</code>
*/
static getAndroidId() {
if (Platform.OS === 'ios') return;
return RNPushe.getAndroidId();
}
/**
* set custom id
* @param {string} id
* @returns promise
*/
static setCustomId(id) {
if (Platform.OS === 'ios') return;
return RNPushe.setCustomId(id);
}
/**
* get custom id
*/
static getCustomId() {
if (Platform.OS === 'ios') return;
return RNPushe.getCustomId();
}
/**
* set user email
* @param {String} email
*/
static setUserEmail(email) {
if (Platform.OS === 'ios') return;
return RNPushe.setUserEmail(email);
}
/**
* get user email
*/
static getUserEmail() {
if (Platform.OS === 'ios') return;
return RNPushe.getUserEmail();
}
/**
* set user phone number
* @param {String} phone
*/
static setUserPhoneNumber(phone) {
if (Platform.OS === 'ios') return;
return RNPushe.setUserPhoneNumber(phone);
}
/**
* get user phone number
*/
static getUserPhoneNumber() {
if (Platform.OS === 'ios') return;
return RNPushe.getUserPhoneNumber();
}
/**
* Subscribe a topic
*
* @param {string} topicName
* @return void
*/
static subscribeToTopic(topicName) {
return RNPushe.subscribeToTopic(topicName);
}
/**
* Unsubscribe from a topic
*
* @param {string} topicName
* @return void
*/
static unsubscribeFromTopic(topicName) {
return RNPushe.unsubscribeFromTopic(topicName);
}
/**
* get subscribed topics
*/
static getSubscribedTopics() {
return RNPushe.getSubscribedTopics();
}
/**
*
* @param {object} tags - Object of key: string, value: string
*/
static addTags(tags) {
return RNPushe.addTags(tags);
}
/**
*
* @param {list} list - a list of strings
*/
static removeTags(list) {
return RNPushe.removeTags(list);
}
static getSubscribedTags() {
return RNPushe.getSubscribedTags();
}
// region Notifiation module
/**
* Disable notification
*
*/
static disableNotifications() {
if (Platform.OS === 'ios') return;
return RNPushe.disableNotifications();
}
/**
* Enable notification
*
*/
static enableNotifications() {
if (Platform.OS === 'ios') return;
return RNPushe.enableNotifications();
}
/**
* Check weather notification is disabled or not
*/
static isNotificationEnable() {
if (Platform.OS === 'ios') return;
return RNPushe.isNotificationEnable();
}
/**
* enable custom sound
*/
static enableCustomSound() {
if (Platform.OS === 'ios') return;
return RNPushe.enableCustomSound();
}
/**
* disble custom sound
*/
static disableCustomSound() {
if (Platform.OS === 'ios') return;
return RNPushe.disableCustomSound();
}
/**
* Check weather custom sound is disbled or not
*/
static isCustomSoundEnable() {
if (Platform.OS === 'ios') return;
return RNPushe.isCustomSoundEnable();
}
/**
* Send notification to another device
*/
static sendNotificationToUser({type, userId, ...otherParams}) {
if (Platform.OS === 'ios') return;
if (!type || !userId) {
return Promise.reject("Must specify `type` & `userId`");
}
if (!Pushe.ANDROID_ID_TYPES[type]) {
return Promise.reject("Provide valid type from `Pushe.ANDROID_ID_TYPES`");
}
return RNPushe.sendNotificationToUser(type, userId, JSON.stringify(otherParams));
}
/**
* Create a notification channel (only Android 8.0+)
*
* @param {string} channelId
* @param {string} channelName
* @param {string} description
* @param {number<int>} importance
* @param {boolean} enableLight
* @param {boolean} enableVibration
* @param {bollean} showBadge
* @param {number<int>} ledColor
* @param {array} vibrationPattern
* @return void
*/
static createNotificationChannel(...params) {
if (Platform.OS === 'ios') return;
return RNPushe.createNotificationChannel(...params);
}
/**
* Remove notification channel with channelId
*
* @param {string} channelId
*/
static removeNotificationChannel(channelId) {
if (Platform.OS === 'ios') return;
return RNPushe.removeNotificationChannel(channelId);
}
/**
* Enable notification foreground awareness for all incoming notifications
* Refer to documentation for further information
* @returns Nothing for iOS, and a promise for Android
*/
static enableNotificationForceForegroundAware() {
if (Platform.OS == 'ios') return;
return RNPushe.enableNotificationForceForegroundAware();
}
/**
* Disables notification foreground awareness for all incoming notifications
* Instead notifications will decide what should happen (`show_foreground` key in notification message)
* Refer to documentation for further information
* @returns Nothing for iOS, and a promise for Android
*/
static disableNotificationForceForegroundAware() {
if (Platform.OS == 'ios') return;
return RNPushe.disableNotificationForceForegroundAware();
}
/**
* Is the ability enabled or not
* Refer to documentation for further information
* @returns Nothing for iOS, and a promise for Android
*/
static isForceForegroundAware() {
if (Platform.OS == 'ios') return;
return RNPushe.isForegroundAwareByForce();
}
// endregion
static sendEcommerceData(name,price) {
if (Platform.OS === 'ios') return;
return RNPushe.sendEcommerceData(name,price);
}
static EventAction = {
CUSTOM : 'custom',
SIGNUP : 'sign_up',
LOGIN : 'login',
PURCHASE : 'purchase',
ACHIEVEMENT : 'achievement',
LEVEL : 'level'
}
/**
* sends event to server
*
* @param {string} name
* @param {EventAction} action
* @param {object} data - Object of key: string, value: objc
*/
static sendEvent(name, action=Pushe.EventAction.CUSTOM, data={}) {
return RNPushe.sendEvent(name, action, data);
}
// iOS specific methods
/**
* Returns APNs-token for iOS
*/
static getAPNsToken() {
if (Platform.OS === 'android') return;
return RNPushe.getAPNsTokenAsString();
}
/**
* Returns DeviceId for iOS and Android
*/
static getDeviceId() {
return RNPushe.getDeviceId();
}
/**
* Returns AdvertisingId for iOS
*/
static getAdvertisingId() {
if (Platform.OS === 'android') return;
return RNPushe.getAdvertisingId();
}
// region Fcm and Hms modules
/**
* Get token of FCM module (if active)
* @returns the token (if service fcm is active), empty otherwise
*/
static getFcmToken() {
if (Platform.OS == 'ios') return;
return RNPushe.getFcmToken();
}
/**
* Get token of HMS module (if active)
* @returns the token (if service hms is active), empty otherwise
*/
static getHmsToken() {
if (Platform.OS == 'ios') return;
return RNPushe.getHmsToken();
}
/**
* @returns The current active service (fcm, hms or empty if no service is currently active)
*/
static getActiveService() {
if (Platform.OS == 'ios') return;
return RNPushe.getActiveService();
}
// endregion
}
export default Pushe;