-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
654 lines (600 loc) · 16.1 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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
import {
DeviceEventEmitter,
Linking,
NativeEventEmitter,
NativeModules,
Platform,
} from 'react-native';
const BlueshiftEventEmitter = new NativeEventEmitter(
NativeModules.BlueshiftReactEventsManager,
);
const Blueshift = {
/**
* Initialize the components of the Blueshift SDK. This mainly initializes the
* event emitter instance to start firing the events when the app is ready to
* receive them.
*
* Usage -
* Blueshift.init();
*
*/
init: function () {
if (Platform.OS === 'android') {
NativeModules.BlueshiftBridge.init();
}
},
/**
* Add event listener for a event name to listen to events fired by Blueshift SDK
*
* Usage -
* Blueshift.addEventListener("PushNotificationClickedEvent", this.handlePush);
*
* @param {string} eventName Name of event to listen to.
* @param {function} callback callback function to handle the event
*
*/
addEventListener: function (eventName, callback) {
if (BlueshiftEventEmitter) {
BlueshiftEventEmitter.addListener(eventName, callback);
}
if (Platform.OS === 'android') {
NativeModules.BlueshiftBridge.onAddEventListener(eventName);
}
},
/**
* Remove the event listener.
*
* Usage -
* Blueshift.removeEventListener("PushNotificationClickedEvent");
*
* @param {string} eventName Name of event remove the listener.
*
*/
removeEventListener: function (eventName) {
if (BlueshiftEventEmitter) {
BlueshiftEventEmitter.removeAllListeners(eventName);
}
},
/**
* Registers a page for showing in-app message.
*
* Usage -
* Blueshift.registerForInAppMessage("IndexScreen");
*
* @param {string} screenName Name of the screen.
*
*/
registerForInAppMessage: function (screenName) {
NativeModules.BlueshiftBridge.registerForInAppMessage(screenName);
},
/**
* Get registered screen name for in-app messages.
*
* Usage -
* Blueshift.getRegisteredForInAppScreenName(screenName = {
* console.log("registered screen name - " + screenName);
* });
*
*/
getRegisteredForInAppScreenName: function (callback) {
NativeModules.BlueshiftBridge.getRegisteredForInAppScreenName(callback);
},
/**
* Unregisters a page from showing in-app message.
*
* Usage -
* Blueshift.unregisterForInAppMessage();
*
*/
unregisterForInAppMessage: function () {
NativeModules.BlueshiftBridge.unregisterForInAppMessage();
},
/**
* Fetches in-app messages from the Blueshift API and provides them in the callbacks.
*
*
* Usage -
* Blueshift.fetchInAppNotification();
*
*/
fetchInAppNotification: function () {
NativeModules.BlueshiftBridge.fetchInAppNotification();
},
/**
* Display in-app message if the current page is registered for in-app messages.
*
* Usage -
* Blueshift.displayInAppNotification();
*
*/
displayInAppNotification: function () {
NativeModules.BlueshiftBridge.displayInAppNotification();
},
/**
* Sends an identify event with the details available.
*
* Usage -
* Blueshift.Blueshift.identifyWithDetails({})
*
* @param {object} details Additional params (if any)
*
*/
identifyWithDetails: function (details) {
NativeModules.BlueshiftBridge.identifyWithDetails(details);
},
/**
* Send any custom event to Blueshift.
*
* Usage -
* Blueshift.trackCustomEvent("CustomEvent",{},false);
*
* @param {string} eventName Name of the custom event.
* @param {object} details Additional params (if any).
* @param {boolean} isBatch Tells if this event can be batched or not.
*
*/
trackCustomEvent: function (eventName, details, isBatch) {
NativeModules.BlueshiftBridge.trackCustomEvent(eventName, details, isBatch);
},
/**
* Track screen view using Blueshift.
*
* Usage -
* Blueshift.trackScreenView("IndexScreen",{},false);
*
* @param {string} screenName Name of the screen to track.
* @param {object} details Additional params (if any).
* @param {boolean} isBatch Tells if this event can be batched or not.
*
*/
trackScreenView: function (screenName, details, isBatch) {
NativeModules.BlueshiftBridge.trackScreenView(screenName, details, isBatch);
},
/**
* Save email in the SDK.
*
* Usage -
* Blueshift.setUserInfoEmailId("[email protected]");
*
* @param {string} emailId email of the customer.
*
*/
setUserInfoEmailId: function (emailId) {
NativeModules.BlueshiftBridge.setUserInfoEmailId(emailId);
},
/**
* Save customerId in the SDK.
*
* Usage -
* Blueshift.setUserInfoCustomerId("cust123456");
*
* @param {string} customerId customerId of the customer.
*
*/
setUserInfoCustomerId: function (customerId) {
NativeModules.BlueshiftBridge.setUserInfoCustomerId(customerId);
},
/**
* Save firstname in the SDK.
*
* Usage -
* Blueshift.setUserInfoFirstName("John");
*
* @param {string} firstname firstname of the customer.
*
*/
setUserInfoFirstName: function (firstname) {
NativeModules.BlueshiftBridge.setUserInfoFirstName(firstname);
},
/**
* Save lastname in the SDK.
*
* Usage -
* Blueshift.setUserInfoLastName("Cartor");
*
* @param {string} lastname lastname of the customer.
*
*/
setUserInfoLastName: function (lastname) {
NativeModules.BlueshiftBridge.setUserInfoLastName(lastname);
},
/**
* Save additional user info in the SDK.
*
* Usage -
* Blueshift.setUserInfoExtras({"profession":"software engineer", "usertype":"premium"});
*
* @param {object} extras additional user info.
*
*/
setUserInfoExtras: function (extras) {
NativeModules.BlueshiftBridge.setUserInfoExtras(extras);
},
/**
* Remove all the saved user info from the SDK.
*
* Usage -
* Blueshift.removeUserInfo();
*
*/
removeUserInfo: function () {
NativeModules.BlueshiftBridge.removeUserInfo();
},
/**
* Enable/disable SDK's event tracking.
*
* Usage -
* Blueshift.setEnableTracking(true);
*
* @param {boolean} enabled When true, tracking is enabled. When false, disabled.
*
*/
setEnableTracking: function (enabled) {
NativeModules.BlueshiftBridge.setEnableTracking(enabled);
},
/**
* Opt-in or opt-out of push notifications sent from Blueshift.
*
* Usage -
* Blueshift.setEnablePush(true);
*
* @param {boolean} isEnabled When true, opt-in else opt-out.
*
*/
setEnablePush: function (isEnabled) {
NativeModules.BlueshiftBridge.setEnablePush(isEnabled);
},
/**
* Opt-in or opt-out of in-app notifications sent from Blueshift.
*
* Usage -
* Blueshift.setEnableInApp(true);
*
* @param {boolean} isEnabled When true, opt-in else opt-out.
*
*/
setEnableInApp: function (isEnabled) {
NativeModules.BlueshiftBridge.setEnableInApp(isEnabled);
},
/**
* Set IDFA of the device in the Blueshift SDK.
* Note - This is only applicable for the iOS devices.
*
* Usage -
* Blueshift.setIDFA("EA7583CD-A667-48BC-B806-42ECB2B48606");
*
* @param {string} IDFAString IDFA value.
*
*/
setIDFA: function (IDFAString) {
if (Platform.OS === 'ios') {
NativeModules.BlueshiftBridge.setIDFA(IDFAString);
}
},
/**
* Register for remote notifications using SDK. Calling this method will show push permission dialogue to the user.
* Note - This is only applicable for the iOS devices.
*
* Usage -
* Blueshift.registerForRemoteNotification();
*
*/
registerForRemoteNotification: function () {
if (Platform.OS === 'ios') {
NativeModules.BlueshiftBridge.registerForRemoteNotification();
}
},
/**
* Set current location of the device in the Blueshift SDK.
* Note - This is only applicable for the iOS devices.
*
* Usage -
* Blueshift.setCurrentLocation(18.5245649,73.7228812);
*
* @param {Number} latitude location latitude value.
* @param {Number} longitude location longitude value.
*
*/
setCurrentLocation: function (latitude, longitude) {
if (Platform.OS === 'ios') {
NativeModules.BlueshiftBridge.setCurrentLocation(latitude, longitude);
}
},
/**
* Calls Blueshift's live content API with email and given slot name and live content context.
*
* Usage -
* Blueshift.getLiveContentByEmail("testSlot",{},(err,result) => {
* if (result) {
* console.log(result);
* } else {
* console.log(err);
* }
* });
*
* @param {string} slot slot name of the live content.
* @param {object} lcContext live content context.
* @param {function} callback callback function.
*
*/
getLiveContentByEmail: function (slot, lcContext, callback) {
NativeModules.BlueshiftBridge.getLiveContentByEmail(
slot,
lcContext,
callback,
);
},
/**
* Calls Blueshift's live content API with customer id and given slot name and live content context.
*
* Usage -
* Blueshift.getLiveContentByCustomerId("testSlot",{},(err,result) => {
* if (result) {
* console.log(result);
* } else {
* console.log(err);
* }
* });
*
* @param {string} slot slot name of the live content.
* @param {object} lcContext live content context.
* @param {function} callback callback function.
*
*/
getLiveContentByCustomerId: function (slot, lcContext, callback) {
NativeModules.BlueshiftBridge.getLiveContentByCustomerId(
slot,
lcContext,
callback,
);
},
/**
* Calls Blueshift's live content API with device id and given slot name and live content context.
*
* Usage -
* Blueshift.getLiveContentByDeviceId("testSlot",{},(err,result) => {
* if (result) {
* console.log(result);
* } else {
* console.log(err);
* }
* });
*
* @param {string} slot slot name of the live content.
* @param {object} lcContext live content context.
* @param {function} callback callback function.
*
*/
getLiveContentByDeviceId: function (slot, lcContext, callback) {
NativeModules.BlueshiftBridge.getLiveContentByDeviceId(
slot,
lcContext,
callback,
);
},
/**
* Get opt-in or opt-out status of in-app notifications set in the SDK.
*
* Usage -
* Blueshift.getEnableInAppStatus((value) => {
* console.log("status"+value);
* });
*
* @param {function} callback success callback.
*
*/
getEnableInAppStatus: function (callback) {
NativeModules.BlueshiftBridge.getEnableInAppStatus(callback);
},
/**
* Get opt-in or opt-out status of push notifications set in the SDK.
*
* Usage -
* Blueshift.getEnablePushStatus((value) => {
* console.log("status"+value);
* });
*
* @param {function} callback success callback.
*
*/
getEnablePushStatus: function (callback) {
NativeModules.BlueshiftBridge.getEnablePushStatus(callback);
},
/**
* Get status of event tracking set in the SDK.
*
* Usage -
* Blueshift.getEnableTrackingStatus((value) => {
* console.log("status"+value);
* });
*
* @param {function} callback success callback.
*
*/
getEnableTrackingStatus: function (callback) {
NativeModules.BlueshiftBridge.getEnableTrackingStatus(callback);
},
/**
* Get email id string set in the SDK.
*
* Usage -
* Blueshift.getUserInfoEmailId((value) => {
* console.log("Email id"+value);
* });
*
* @param {function} callback success callback.
*
*/
getUserInfoEmailId: function (callback) {
NativeModules.BlueshiftBridge.getUserInfoEmailId(callback);
},
/**
* Get customer id string set in the SDK.
*
* Usage -
* Blueshift.getUserInfoCustomerId((value) => {
* console.log("Customer id"+value);
* });
*
* @param {function} callback success callback.
*
*/
getUserInfoCustomerId: function (callback) {
NativeModules.BlueshiftBridge.getUserInfoCustomerId(callback);
},
/**
* Get first name string set in the SDK.
*
* Usage -
* Blueshift.getUserInfoFirstName((value) => {
* console.log("First name"+value);
* });
*
* @param {function} callback success callback.
*
*/
getUserInfoFirstName: function (callback) {
NativeModules.BlueshiftBridge.getUserInfoFirstName(callback);
},
/**
* Get last name string set in the SDK.
*
* Usage -
* Blueshift.getUserInfoLastName((value) => {
* console.log("Last name"+value);
* });
*
* @param {function} callback success callback.
*
*/
getUserInfoLastName: function (callback) {
NativeModules.BlueshiftBridge.getUserInfoLastName(callback);
},
/**
* Get extras JSON data set in the SDK.
*
* Usage -
* Blueshift.getUserInfoExtras((value) => {
* console.log("Extras"+value);
* });
*
* @param {function} callback success callback.
*
*/
getUserInfoExtras: function (callback) {
NativeModules.BlueshiftBridge.getUserInfoExtras(callback);
},
/**
* Get current device id string used by the SDK.
*
* Usage -
* Blueshift.getCurrentDeviceId((value) => {
* console.log("Device id"+value);
* });
*
* @param {function} callback success callback.
*
*/
getCurrentDeviceId: function (callback) {
NativeModules.BlueshiftBridge.getCurrentDeviceId(callback);
},
/**
* Reset the current device id. This is only applicable if the device id
* source is set to GUID for Android or UUID for iOS.
*
* Usage -
* Blueshift.resetDeviceId();
*/
resetDeviceId: function () {
NativeModules.BlueshiftBridge.resetDeviceId();
},
/**
* Sync Blueshift Inbox messages on the local cache.
* This will sync new messges, delete the expired messages, and update the unread status
* of the message to the locally cached messges.
*
* Usage -
* Blueshift.syncInboxMessages((status) => {
* if (status) {
* console.log("sync complete");
* }
* });
*/
syncInboxMessages: function (callback) {
NativeModules.BlueshiftBridge.syncInboxMessages(callback);
},
/**
* Get unread messages count to show on the notification badge.
*
* Usage -
* Blueshift.getUnreadInboxMessageCount((count) => {
* console.log("unread messages count"+count);
* });
*/
getUnreadInboxMessageCount: function (callback) {
NativeModules.BlueshiftBridge.getUnreadInboxMessageCount(callback);
},
/**
* Get inbox messages list to show in the list view.
*
* Usage -
* Blueshift.getInboxMessages((messages) => {
* console.log("unread messages count"+count);
* });
*/
getInboxMessages: function (callback) {
NativeModules.BlueshiftBridge.getInboxMessages(messages => {
callback(messages);
});
},
/**
* Show in-app notification for the Inbox message.
*
* @param {BlueshiftInboxMessage} message
*
* Usage -
* Blueshift.showInboxMessage();
*/
showInboxMessage: function (message) {
NativeModules.BlueshiftBridge.showInboxMessage(message);
},
/**
* Delete inbox message.
*
* @param {BlueshiftInboxMessage} message
*
* Usage -
* Blueshift.deleteInboxMessage();
*/
deleteInboxMessage: function (message, callback) {
NativeModules.BlueshiftBridge.deleteInboxMessage(message, callback);
},
/**
* Process the Blueshift url and provide the final url to Linking's "url" callback
*
* @param {string} url
*
*/
processBlueshiftUrl: function (url) {
if (Platform.OS === 'android') {
NativeModules.BlueshiftBridge.processBlueshiftUrl(url);
}
},
/**
* Checks if the given Url is of Blueshift's format.
*
* @param {string} url
*/
isBlueshiftUrl: function (url) {
if (url) {
let hasBlueshiftPath = url.includes('/track') || url.includes('/z/');
let hasBlueshiftArgs =
url.includes('eid=') || (url.includes('mid=') && url.includes('uid='));
return hasBlueshiftPath && hasBlueshiftArgs;
} else {
console.log('Blueshift: The URL is null.');
}
return false;
},
};
module.exports = Blueshift;