forked from OneSignal/react-native-onesignal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
82 lines (65 loc) · 2.18 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
/**
* @providesModule Notifications
*/
'use strict';
import { NativeModules, DeviceEventEmitter } from 'react-native';
const { RNOneSignal } = NativeModules;
var Notifications = {
onError: false,
onNotificationOpened: false,
};
var _pendingNotifications = [];
var DEVICE_NOTIF_EVENT = 'remoteNotificationOpened';
/**
* Configure local and remote notifications
* @param {Object} options
* @param {function} options.onNotificationOpened - Fired when a remote notification is received.
* @param {function} options.onError - None
*/
Notifications.configure = function(options: Object) {
if ( typeof options.onError !== 'undefined' ) {
this.onError = options.onError;
}
if ( typeof options.onNotificationOpened !== 'undefined' ) {
this.onNotificationOpened = options.onNotificationOpened;
if (_pendingNotifications.length > 0) {
var notification = _pendingNotifications.pop();
this._onNotificationOpened(notification.message, notification.data, notification.isActive);
}
}
};
/* Unregister */
Notifications.unregister = function() {
this.onNotificationOpened = false;
};
Notifications._onNotificationOpened = function(message, data, isActive) {
if ( this.onNotificationOpened === false ) {
var notification = {message: message, data: data, isActive: isActive};
_pendingNotifications.push(notification);
return;
}
this.onNotificationOpened(message, data, isActive);
};
Notifications.sendTag = function(key, value) {
RNOneSignal.sendTag(key, value);
};
Notifications.sendTags = function(tags) {
RNOneSignal.sendTags(tags || {});
};
Notifications.getTags = function(next) {
RNOneSignal.getTags(next);
}
Notifications.deleteTag = function(key) {
RNOneSignal.deleteTag(key);
}
Notifications.idsAvailable = function(idsAvailable) {
RNOneSignal.idsAvailable(idsAvailable);
}
DeviceEventEmitter.addListener(DEVICE_NOTIF_EVENT, function(notifData) {
var message = notifData.message;
var data = (notifData.additionalData !== null && typeof notifData.additionalData === 'object') ? notifData.additionalData : JSON.parse(notifData.additionalData);
var isActive = notifData.isActive;
Notifications._onNotificationOpened(message, data, isActive);
}
);
module.exports = Notifications;