forked from OneSignal/react-native-onesignal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
237 lines (198 loc) · 7.23 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
'use strict';
import { NativeModules, NativeAppEventEmitter, NetInfo, Platform } from 'react-native';
import invariant from 'invariant';
var RNOneSignal = NativeModules.OneSignal;
var DEVICE_NOTIF_RECEIVED_EVENT = 'OneSignal-remoteNotificationReceived';
var DEVICE_NOTIF_OPENED_EVENT = 'OneSignal-remoteNotificationOpened';
var DEVICE_NOTIF_REG_EVENT = 'OneSignal-remoteNotificationsRegistered';
var DEVICE_IDS_AVAILABLE = 'OneSignal-idsAvailable';
const _notifHandlers = new Map();
function handleConnectionStateChange(isConnected) {
if (!isConnected) return;
OneSignal.configure();
NetInfo.isConnected.removeEventListener('connectionChange', handleConnectionStateChange);
}
NetInfo.isConnected.fetch().then(isConnected => {
if (isConnected) return OneSignal.configure();
NetInfo.isConnected.addEventListener('connectionChange', handleConnectionStateChange);
}).catch((...args) => console.warn("Error: ", args));
export default class OneSignal {
static addEventListener(type: any, handler: Function) {
// Listen to events of notification received, opened, device registered and IDSAvailable.
invariant(
type === 'received' || type === 'opened' || type === 'registered' || type === 'ids',
'OneSignal only supports `received`, `opened`, `registered`, and `ids` events'
);
var listener;
if (type === 'received') {
listener = NativeAppEventEmitter.addListener(
DEVICE_NOTIF_RECEIVED_EVENT,
(notification) => {
handler(notification);
}
);
} else if (type === 'opened') {
listener = NativeAppEventEmitter.addListener(
DEVICE_NOTIF_OPENED_EVENT,
(result) => {
handler(result);
}
);
} else if (type === 'registered') {
listener = NativeAppEventEmitter.addListener(
DEVICE_NOTIF_REG_EVENT,
(notifData) => {
handler(notifData);
}
);
} else if (type === 'ids') {
listener = NativeAppEventEmitter.addListener(
DEVICE_IDS_AVAILABLE,
(ids) => {
handler(ids);
}
);
}
_notifHandlers.set(type, listener);
}
static removeEventListener(type: any, handler: Function) {
invariant(
type === 'received' || type === 'opened' || type === 'registered' || type === 'ids',
'OneSignal only supports `received`, `opened`, `registered`, and `ids` events'
);
var listener = _notifHandlers.get(type);
if (!listener) {
return;
}
listener.remove();
_notifHandlers.delete(type);
}
static registerForPushNotifications() {
if (Platform.OS === 'ios') {
RNOneSignal.registerForPushNotifications();
} else {
console.log("This function is not supported on Android");
}
}
static promptForPushNotificationsWithUserResponse(callback: Function) {
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) {
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() {
RNOneSignal.configure();
}
static checkPermissions(callback: Function) {
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 getPermissionSubscriptionState(callback: Function) {
invariant(
typeof callback === 'function',
'Must provide a valid callback'
);
RNOneSignal.getPermissionSubscriptionState(callback);
}
static sendTag(key, value) {
RNOneSignal.sendTag(key, value);
}
static sendTags(tags) {
RNOneSignal.sendTags(tags || {});
}
static getTags(next) {
RNOneSignal.getTags(next);
}
static deleteTag(key) {
RNOneSignal.deleteTag(key);
}
static enableVibrate(enable) {
if (Platform.OS === 'android') {
RNOneSignal.enableVibrate(enable);
} else {
console.log("This function is not supported on iOS");
}
}
static enableSound(enable) {
if (Platform.OS === 'android') {
RNOneSignal.enableSound(enable);
} else {
console.log("This function is not supported on iOS");
}
}
static setSubscription(enable) {
RNOneSignal.setSubscription(enable);
}
static promptLocation() {
//Supported in both iOS & Android
RNOneSignal.promptLocation();
}
//Android only: Set Display option of the notifications. displayOption is of type OSInFocusDisplayOption
// 0 -> None, 1 -> InAppAlert, 2 -> Notification
static inFocusDisplaying(displayOption) {
if (Platform.OS === 'android') {
RNOneSignal.inFocusDisplaying(displayOption);
}
}
static postNotification(contents, data, player_id, otherParameters) {
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 (Platform.OS === 'android') {
RNOneSignal.clearOneSignalNotifications();
} else {
console.log("This function is not supported on iOS");
}
}
static cancelNotification(id) {
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) {
RNOneSignal.syncHashedEmail(email);
}
static setLogLevel(nsLogLevel, visualLogLevel) {
RNOneSignal.setLogLevel(nsLogLevel, visualLogLevel);
}
}