forked from Pushwoosh/pushwoosh-react-native-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
487 lines (448 loc) · 13.4 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
'use strict';
import { NativeModules } from 'react-native';
const PushwooshModule = NativeModules.Pushwoosh;
//Class: PushNotification
//Use `PushNotification` to register device for push notifications on Pushwoosh and customize notification appearance.
//
//Example:
//(start code)
//DeviceEventEmitter.addListener('pushOpened', (e: Event) => {
// console.warn("pushOpened: " + JSON.stringify(e));
// alert(JSON.stringify(e));
//});
//
//const Pushwoosh = require('pushwoosh-react-native-plugin');
//
//Pushwoosh.init({ "pw_appid" : "XXXX-XXXX", "project_number" : "XXXXXXXXXXXXX" });
//
//Pushwoosh.register(
// (token) => {
// console.warn("Registered for pushes: " + token);
// },
// (error) => {
// console.warn("Failed to register: " + error);
// }
//);
//(end)
class PushNotification {
//Function: init
//Call this first thing with your Pushwoosh App ID (pw_appid parameter) and Google Project ID for Android (projectid parameter)
//
//Example:
//(start code)
// //initialize Pushwoosh with projectid: "GOOGLE_PROJECT_ID", appid : "PUSHWOOSH_APP_ID". This will trigger all pending push notifications on start.
// Pushwoosh.init({ projectid: "XXXXXXXXXXXXXXX", pw_appid : "XXXXX-XXXXX" });
//(end)
init(config: Object, success: ?Function, fail: ?Function) {
if (!success) {
success = function() {};
}
if (!fail) {
fail = function(error) {};
}
PushwooshModule.init(config, success, fail);
}
//Function: createLocalNotification
//Creates a local notification with a specified message, delay and custom data
//
//Example:
//(start code)
// //creates a local notification with "message" content, 5 seconds delay and passes {"somedata":"optional"} object in payload
// Pushwoosh.createLocalNotification({msg:"message", seconds:5, userData:{"somedata":"optional"}});
//(end)
createLocalNotification(data: Object){
PushwooshModule.createLocalNotification(data);
}
//Function: clearLocalNotification
//Clears all existing and cancels all pending local notifications
//
//Example:
//(start code)
// Pushwoosh.clearLocalNotification();
//(end)
clearLocalNotification(){
PushwooshModule.clearLocalNotification();
}
//Function: register
//Call this to register for push notifications and retreive a push Token
//
//Example:
//(start code)
// Pushwoosh.registerDevice(
// function(token)
// {
// alert(token);
// },
// function(status)
// {
// alert("failed to register: " + status);
// }
// );
//(end)
register(success: ?Function, fail: ?Function) {
if (!success) {
success = function(token) {};
}
if (!fail) {
fail = function(error) {};
}
PushwooshModule.register(success, fail);
}
//Function: unregister
//Unregisters device from push notifications
unregister(success: ?Function, fail: ?Function) {
if (!success) {
success = function(token) {};
}
if (!fail) {
fail = function(error) {};
}
PushwooshModule.unregister(success, fail);
}
//Function: onPushOpen
//Deprecated - use DeviceEventEmitter.addListener('pushOpened', callback) instead
onPushOpen(callback: Function) {
PushwooshModule.onPushOpen(callback);
}
//Function: setTags
//Call this to set tags for the device
//
//Example:
//sets the following tags: "deviceName" with value "hello" and "deviceId" with value 10
//(start code)
// Pushwoosh.setTags({deviceName:"hello", deviceId:10},
// function(status) {
// console.warn('setTags success');
// },
// function(status) {
// console.warn('setTags failed');
// }
// );
//
// //settings list tags "MyTag" with values (array) "hello", "world"
// pushNotification.setTags({"MyTag":["hello", "world"]});
//(end)
setTags(tags: Object, success: ?Function, fail: ?Function) {
if (!success) {
success = function() {};
}
if (!fail) {
fail = function(error) {};
}
PushwooshModule.setTags(tags, success, fail);
}
//Function: getTags
//Call this to get tags for the device
//
//Example:
//(start code)
// Pushwoosh.getTags(
// function(tags)
// {
// console.warn('tags for the device: ' + JSON.stringify(tags));
// },
// function(error)
// {
// console.warn('get tags error: ' + JSON.stringify(error));
// }
// );
//(end)
getTags(success: Function, fail: ?Function) {
if (!fail) {
fail = function(error) {};
}
PushwooshModule.getTags(success, fail);
}
//Function: setShowPushnotificationAlert
//Set push notifications alert when push notification is received while the app is running, default is `true`
//
//Example:
//(start code)
// Pushwoosh.setShowPushnotificationAlert(false);
//(end)
setShowPushnotificationAlert(showPushnotificationAlert: boolean) {
PushwooshModule.setShowPushnotificationAlert(showPushnotificationAlert);
}
//Function: getShowPushnotificationAlert
//Show push notifications alert when push notification is received while the app is running, default is `true`
//
//Example:
//(start code)
// Pushwoosh.getShowPushnotificationAlert((showPushnotificationAlert) => {
// console.warn("showPushnotificationAlert = " + showPushnotificationAlert);
// });
//(end)
getShowPushnotificationAlert(callback: Function) {
PushwooshModule.getShowPushnotificationAlert(callback);
}
//Function: getPushToken
//Call this to get push token if it is available. Note the token also comes in registerDevice function callback.
//
//Example:
//(start code)
// Pushwoosh.getPushToken(
// function(token)
// {
// console.warn('push token: ' + token);
// }
// );
//(end)
getPushToken(success: Function) {
PushwooshModule.getPushToken(success);
}
//Function: getHwid
//Call this to get Pushwoosh HWID used for communications with Pushwoosh API
//
//Example:
//(start code)
// Pushwoosh.getHwid(
// function(token) {
// console.warn('Pushwoosh HWID: ' + token);
// }
// );
//(end)
getHwid(success: Function) {
PushwooshModule.getHwid(success);
}
//Function: setUserId
//[android, ios] Set User indentifier. This could be Facebook ID, username or email, or any other user ID.
//This allows data and events to be matched across multiple user devices.
//
//Parameters:
// "userId" - user string identifier
//
setUserId(userId: string) {
PushwooshModule.setUserId(userId);
}
//Function: postEvent
//[android, ios] Post events for In-App Messages. This can trigger In-App message display as specified in Pushwoosh Control Panel.
//
//Parameters:
// "event" - event to trigger
// "attributes" - object with additional event attributes
//
// Example:
//(start code)
// Pushwoosh.setUserId("XXXXXX");
// Pushwoosh.postEvent("buttonPressed", { "buttonNumber" : 4, "buttonLabel" : "banner" });
//(end)
postEvent(event: string, attributes: ?Object) {
if (!attributes) {
attributes = {};
}
PushwooshModule.postEvent(event, attributes);
}
//Function: setApplicationIconBadgeNumber
//[android, ios, wp8, windows] Set the application icon badge number
//
//Parameters:
// "badgeNumber" - icon badge number
//
setApplicationIconBadgeNumber(badgeNumber: number) {
PushwooshModule.setApplicationIconBadgeNumber(badgeNumber);
}
//Function: getApplicationIconBadgeNumber
//[android, ios] Returns the application icon badge number
//
//Parameters:
// "callback" - success callback
//
//Example:
//(start code)
// Pushwoosh.getApplicationIconBadgeNumber(function(badge){ alert(badge);} );
//(end)
getApplicationIconBadgeNumber(callback: Function) {
PushwooshModule.getApplicationIconBadgeNumber(callback);
}
//Function: addToApplicationIconBadgeNumber
//[android, ios] Adds value to the application icon badge
//
//Parameters:
// "badgeNumber" - incremental icon badge number
//
//Example:
//(start code)
// Pushwoosh.addToApplicationIconBadgeNumber(5);
// Pushwoosh.addToApplicationIconBadgeNumber(-5);
//(end)
addToApplicationIconBadgeNumber(badgeNumber: number) {
PushwooshModule.addToApplicationIconBadgeNumber(badgeNumber);
}
//Function: setMultiNotificationMode
//[android] Allows multiple notifications to be displayed in the Android Notification Center
setMultiNotificationMode(on: boolean) {
PushwooshModule.setMultiNotificationMode(on);
}
//Function: setLightScreenOnNotification
//[android] Turns the screen on if notification arrives
//
//Parameters:
// "on" - enable/disable screen unlock (is disabled by default)
//
setLightScreenOnNotification(on: boolean) {
PushwooshModule.setLightScreenOnNotification(on);
}
//Function: setEnableLED
//[android] Enables led blinking when notification arrives and display is off
//
//Parameters:
// "on" - enable/disable led blink (is disabled by default)
//
setEnableLED(on: boolean) {
PushwooshModule.setEnableLED(on);
}
//Function: setEnableLED
//[android] Set led color. Use with <setEnableLED>
//
//Parameters:
// "color" - led color in ARGB integer format
//
setColorLED(color: number) {
PushwooshModule.setColorLED(color);
}
//Function: setSoundType
//[android] Sets default sound to play when push notification arrive.
//
//Parameters:
// "type" - Sound type (0 - default, 1 - no sound, 2 - always)
//
setSoundType(type: number) {
PushwooshModule.setSoundType(type);
}
//Function: setVibrateType
//[android] Sets default vibration mode when push notification arrive.
//
//Parameters:
// "type" - Vibration type (0 - default, 1 - no vibration, 2 - always)
//
setVibrateType(type: number) {
PushwooshModule.setVibrateType(type);
}
//Function: presentInboxUI
//[android, ios] Opens Inbox screen.
//
// Supported style keys:
//
// Customizes the date formatting
// "dateFormat"
//
// The default icon in the cell next to the message; if not specified, the app icon is used
// "defaultImageIcon"
//
// The appearance of the unread messages mark (iOS only)
// "unreadImage"
//
// The image which is displayed if an error occurs and the list of inbox messages is empty
// "listErrorImage"
//
// The image which is displayed if the list of inbox messages is empty
// "listEmptyImage"
//
// The error text which is displayed when an error occurs; cannot be localized
// "listErrorMessage"
//
// The text which is displayed if the list of inbox messages is empty; cannot be localized
// "listEmptyMessage"
//
// The default text color (iOS only)
// "defaultTextColor"
//
// The accent color
// "accentColor"
//
// The default background color
// "backgroundColor"
//
// The default selection color
// "highlightColor"
//
// The color of message titles
// "titleColor"
//
// The color of message titles if message was readed (Android only)
// "readTitleColor"
//
// The color of messages descriptions
// "descriptionColor"
//
// The color of messages descriptions if message was readed (Android only)
// "readDescriptionColor"
//
// The color of message dates
// "dateColor"
//
// The color of message dates if message was readed (Android only)
// "readDateColor"
//
// The color of the separator
// "dividerColor"
//
//Example:
//(start code)
// Pushwoosh.presentInboxUI({
// "dateFormat" : "dd.MMMM.YYYY",
// "defaultImageIcon" : Image.resolveAssetSource(require('./icon.png')),
// "listErrorImage" : Image.resolveAssetSource(require('./error.png')),
// "listEmptyImage" : Image.resolveAssetSource(require('./empty.png')),
// "listErrorMessage" : "Error message1",
// "listEmptyMessage" : "Error message2",
// "accentColor" : processColor('#ff00ff'),
// "highlightColor" : processColor('yellow'),
// "dateColor" : processColor('blue'),
// "titleColor" : processColor('#ff00ff'),
// "dividerColor" : processColor('#ff00ff'),
// "descriptionColor" : processColor('green'),
// "backgroundColor" : processColor('rgba(255, 100, 30, 1.0)'),
// "barBackgroundColor" : processColor('#ffffff'),
// "barAccentColor" : processColor('#ffd700'),
// "barTextColor" : processColor('#282828')
// });
//(end)
presentInboxUI(style: ?Object) {
PushwooshModule.presentInboxUI(style);
}
// Show inApp for change setting Enable/disable all communication with Pushwoosh
showGDPRConsentUI(){
PushwooshModule.showGDPRConsentUI();
}
// Show inApp for all device data from Pushwoosh and stops all interactions and communication permanently.
showGDPRDeletionUI(){
PushwooshModule.showGDPRDeletionUI();
}
isDeviceDataRemoved(success: Function){
PushwooshModule.isDeviceDataRemoved(success);
}
// Return flag is enable communication with server
isCommunicationEnabled(success: Function){
PushwooshModule.isCommunicationEnabled(success);
}
// Return flag is enabled GDPR on server
isAvailableGDPR(success: Function){
PushwooshModule.isAvailableGDPR(success);
}
// Enable/disable all communication with Pushwoosh. Enabled by default.
setCommunicationEnabled(enable: boolean, success: ?Function, fail: ?Function) {
if (!success) {
success = function() {};
}
if (!fail) {
fail = function(error) {};
}
PushwooshModule.setCommunicationEnabled(enable, success, fail);
}
// Removes all device data from Pushwoosh and stops all interactions and communication permanently.
removeAllDeviceData( success: ?Function, fail: ?Function) {
if (!success) {
success = function() {};
}
if (!fail) {
fail = function(error) {};
}
PushwooshModule.removeAllDeviceData(success, fail);
}
// Set notification icon background color
setNotificationIconBackgroundColor(color: string) {
PushwooshModule.setNotificationIconBackgroundColor(color);
}
}
module.exports = new PushNotification();