-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
113 lines (99 loc) · 3.69 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
'use strict';
import {
NativeModules,
NativeEventEmitter,
Platform,
} from 'react-native';
const RNVoipPushNotificationManager = NativeModules.RNVoipPushNotificationManager;
const eventEmitter = new NativeEventEmitter(RNVoipPushNotificationManager);
const _eventHandlers = new Map();
// --- native unique event names
const RNVoipPushRemoteNotificationsRegisteredEvent = "RNVoipPushRemoteNotificationsRegisteredEvent"; // --- 'register'
const RNVoipPushRemoteNotificationReceivedEvent = "RNVoipPushRemoteNotificationReceivedEvent"; // --- 'notification'
const RNVoipPushDidLoadWithEvents = "RNVoipPushDidLoadWithEvents"; // --- 'didLoadWithEvents'
export default class RNVoipPushNotification {
static get RNVoipPushRemoteNotificationsRegisteredEvent() {
return RNVoipPushRemoteNotificationsRegisteredEvent;
}
static get RNVoipPushRemoteNotificationReceivedEvent() {
return RNVoipPushRemoteNotificationReceivedEvent;
}
static get RNVoipPushDidLoadWithEvents() {
return RNVoipPushDidLoadWithEvents;
}
/**
* Attaches a listener to remote notification events while the app is running
* in the foreground or the background.
*
* Valid events are:
*
* - `notification` : Fired when a remote notification is received.
* - `register`: Fired when the user registers for remote notifications.
* - `didLoadWithEvents`: Fired when the user have initially subscribed any listener and has cached events already.
*/
static addEventListener(type, handler) {
let listener;
if (type === 'notification') {
listener = eventEmitter.addListener(
RNVoipPushRemoteNotificationReceivedEvent,
(notificationPayload) => {
handler(notificationPayload);
}
);
} else if (type === 'register') {
listener = eventEmitter.addListener(
RNVoipPushRemoteNotificationsRegisteredEvent,
(deviceToken) => {
handler(deviceToken);
}
);
} else if (type === 'didLoadWithEvents') {
listener = eventEmitter.addListener(
RNVoipPushDidLoadWithEvents,
(events) => {
handler(events);
}
);
} else {
return;
}
// --- we only support one listener at a time, remove to prevent leak
RNVoipPushNotification.removeEventListener(type);
_eventHandlers.set(type, listener);
}
/**
* Removes the event listener. Do this in `componentWillUnmount` to prevent
* memory leaks
*/
static removeEventListener(type) {
let listener = _eventHandlers.get(type);
if (!listener) {
return;
}
listener.remove();
_eventHandlers.delete(type);
}
/**
* Register for voip token
*
* @static
* @memberof RNVoipPushNotification
*/
static registerVoipToken() {
RNVoipPushNotificationManager.registerVoipToken();
}
/**
* When you have processed necessary initialization for voip push, tell ios completed.
* This is mainly for ios 11+, which apple required us to execute `complete()` when we finished.
* If you want to use this function, make sure you call `[RNVoipPushNotificationManager addCompletionHandler:uuid completionHandler:completion];`
* in `didReceiveIncomingPushWithPayload` in your AppDelegate.m
*
* @static
* @memberof RNVoipPushNotification
*
* uuid:
*/
static onVoipNotificationCompleted(uuid) {
RNVoipPushNotificationManager.onVoipNotificationCompleted(uuid);
}
}