forked from oney/react-native-gcm-android
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
71 lines (61 loc) · 1.67 KB
/
index.android.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
'use strict';
var {
NativeModules,
DeviceEventEmitter,
} = require('react-native');
var GcmModule = NativeModules.GcmModule;
var _notifHandlers = new Map();
var DEVICE_NOTIF_EVENT = 'remoteNotificationReceived';
var NOTIF_REGISTER_EVENT = 'remoteNotificationsRegistered';
class GcmAndroid {
static addEventListener(type: string, handler: Function) {
var listener;
if (type === 'notification') {
listener = DeviceEventEmitter.addListener(
DEVICE_NOTIF_EVENT,
(notifData) => {
GcmAndroid.isForground = notifData.isInForeground;
var data = JSON.parse(notifData.dataJSON);
handler(new GcmAndroid(data));
}
);
} else if (type === 'register') {
listener = DeviceEventEmitter.addListener(
NOTIF_REGISTER_EVENT,
(registrationInfo) => {
handler(registrationInfo.deviceToken);
}
);
}
_notifHandlers.set(handler, listener);
}
static requestPermissions() {
GcmModule.requestPermissions();
}
static stopService() {
GcmModule.stopService();
}
static createNotification(infos) {
GcmModule.createNotification(infos);
}
static abandonPermissions() {
}
static checkPermissions(callback: Function) {
}
static removeEventListener(type: string, handler: Function) {
var listener = _notifHandlers.get(handler);
if (!listener) {
return;
}
listener.remove();
_notifHandlers.delete(handler);
}
constructor(data) {
this.data = data;
}
}
if (GcmModule.launchNotification) {
GcmAndroid.launchNotification = JSON.parse(GcmModule.launchNotification);
}
GcmAndroid.isForground = true;
module.exports = GcmAndroid;