forked from evollu/react-native-fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (129 loc) · 4.39 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
import {NativeModules, DeviceEventEmitter, Platform} from 'react-native';
export const FCMEvent = {
RefreshToken: 'FCMTokenRefreshed',
Notification: 'FCMNotificationReceived'
}
export const RemoteNotificationResult = {
NewData: 'UIBackgroundFetchResultNewData',
NoData: 'UIBackgroundFetchResultNoData',
ResultFailed: 'UIBackgroundFetchResultFailed'
}
export const WillPresentNotificationResult = {
All: 'UNNotificationPresentationOptionAll',
None: 'UNNotificationPresentationOptionNone'
}
export const NotificationType = {
Remote: 'remote_notification',
NotificationResponse: 'notification_response',
WillPresent: 'will_present_notification',
Local: 'local_notification'
}
const RNFIRMessaging = NativeModules.RNFIRMessaging;
const FCM = {};
FCM.getInitialNotification = () => {
return RNFIRMessaging.getInitialNotification();
}
FCM.getFCMToken = () => {
return RNFIRMessaging.getFCMToken();
};
FCM.requestPermissions = () => {
return RNFIRMessaging.requestPermissions();
};
FCM.presentLocalNotification = (details) => {
details.id = details.id || new Date().getTime().toString()
details.local_notification = true;
RNFIRMessaging.presentLocalNotification(details);
};
FCM.scheduleLocalNotification = function(details) {
if (!details.id) {
throw new Error("id is required for scheduled notification");
}
details.local_notification = true;
RNFIRMessaging.scheduleLocalNotification(details);
};
FCM.getScheduledLocalNotifications = function() {
return RNFIRMessaging.getScheduledLocalNotifications();
};
FCM.cancelLocalNotification = (notificationID) => {
if(!notificationID){
return;
}
RNFIRMessaging.cancelLocalNotification(notificationID);
};
FCM.cancelAllLocalNotifications = () => {
RNFIRMessaging.cancelAllLocalNotifications();
};
FCM.removeDeliveredNotification = (notificationID) => {
if(!notificationID){
return;
}
RNFIRMessaging.removeDeliveredNotification(notificationID);
}
FCM.removeAllDeliveredNotifications = () => {
RNFIRMessaging.removeAllDeliveredNotifications();
}
FCM.setBadgeNumber = (number) => {
RNFIRMessaging.setBadgeNumber(number);
}
FCM.getBadgeNumber = () => {
return RNFIRMessaging.getBadgeNumber();
}
function finish(result){
if(Platform.OS !== 'ios'){
return;
}
if(!this._finishCalled && this._completionHandlerId){
this._finishCalled = true;
switch(this._notificationType){
case NotificationType.Remote:
result = result || RemoteNotificationResult.NoData;
if(!Object.values(RemoteNotificationResult).includes(result)){
throw new Error(`Invalid RemoteNotificationResult, use import {RemoteNotificationResult} from 'react-native-fcm' to avoid typo`);
}
RNFIRMessaging.finishRemoteNotification(this._completionHandlerId, result);
return;
case NotificationType.NotificationResponse:
RNFIRMessaging.finishNotificationResponse(this._completionHandlerId);
return;
case NotificationType.WillPresent:
result = result || (this.show_in_foreground ? WillPresentNotificationResult.All : WillPresentNotificationResult.None);
if(!Object.values(WillPresentNotificationResult).includes(result)){
throw new Error(`Invalid WillPresentNotificationResult, make sure you use import {WillPresentNotificationResult} from 'react-native-fcm' to avoid typo`);
}
RNFIRMessaging.finishWillPresentNotification(this._completionHandlerId, result);
return;
default:
return;
}
}
}
FCM.on = (event, callback) => {
if (!Object.values(FCMEvent).includes(event)) {
throw new Error(`Invalid FCM event subscription, use import {FCMEvent} from 'react-native-fcm' to avoid typo`);
};
if(event === FCMEvent.Notification){
return DeviceEventEmitter.addListener(event, async(data)=>{
data.finish = finish;
try{
await callback(data);
} catch(err){
console.error('Notification handler err', err)
throw err;
}
if(!data._finishCalled){
data.finish();
}
})
}
return DeviceEventEmitter.addListener(event, callback);
};
FCM.subscribeToTopic = (topic) => {
RNFIRMessaging.subscribeToTopic(topic);
};
FCM.unsubscribeFromTopic = (topic) => {
RNFIRMessaging.unsubscribeFromTopic(topic);
};
FCM.send = (senderId, payload) => {
RNFIRMessaging.send(senderId, payload);
};
export default FCM;