forked from jpventura/react-native-fcm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
225 lines (186 loc) · 6.35 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
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';
const EventEmitter = new NativeEventEmitter(NativeModules.RNFIRMessaging || {});
export const FCMEvent = {
RefreshToken: 'FCMTokenRefreshed',
Notification: 'FCMNotificationReceived',
DirectChannelConnectionChanged: 'FCMDirectChannelConnectionChanged'
};
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'
};
export const NotificationCategoryOption = {
CustomDismissAction: 'UNNotificationCategoryOptionCustomDismissAction',
AllowInCarPlay: 'UNNotificationCategoryOptionAllowInCarPlay',
PreviewsShowTitle: 'UNNotificationCategoryOptionHiddenPreviewsShowTitle',
PreviewsShowSubtitle: 'UNNotificationCategoryOptionHiddenPreviewsShowSubtitle',
None: 'UNNotificationCategoryOptionNone'
};
export const NotificationActionOption = {
AuthenticationRequired: 'UNNotificationActionOptionAuthenticationRequired',
Destructive: 'UNNotificationActionOptionDestructive',
Foreground: 'UNNotificationActionOptionForeground',
None: 'UNNotificationActionOptionNone',
};
export const NotificationActionType = {
Default: 'UNNotificationActionTypeDefault',
TextInput: 'UNNotificationActionTypeTextInput',
};
const RNFIRMessaging = NativeModules.RNFIRMessaging;
const FCM = {};
FCM.getInitialNotification = () => {
return RNFIRMessaging.getInitialNotification();
};
FCM.enableDirectChannel = () => {
if (Platform.OS === 'ios') {
return RNFIRMessaging.enableDirectChannel();
}
};
FCM.isDirectChannelEstablished = () => {
return Platform.OS === 'ios' ? RNFIRMessaging.isDirectChannelEstablished() : Promise.resolve(true);
};
FCM.getFCMToken = () => {
return RNFIRMessaging.getFCMToken();
};
FCM.getEntityFCMToken = () => {
return RNFIRMessaging.getEntityFCMToken();
}
FCM.deleteEntityFCMToken = () => {
return RNFIRMessaging.deleteEntityFCMToken();
}
FCM.deleteInstanceId = () =>{
return RNFIRMessaging.deleteInstanceId();
};
FCM.getAPNSToken = () => {
if (Platform.OS === 'ios') {
return RNFIRMessaging.getAPNSToken();
}
};
FCM.requestPermissions = () => {
return RNFIRMessaging.requestPermissions();
};
FCM.createNotificationChannel = (channel) => {
if (Platform.OS === 'android') {
return RNFIRMessaging.createNotificationChannel(channel);
}
}
FCM.deleteNotificationChannel = (channel) => {
if (Platform.OS === 'android') {
return RNFIRMessaging.deleteNotificationChannel(channel);
}
}
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 EventEmitter.addListener(event, async(data) => {
data.finish = finish;
try {
await callback(data);
} catch (err) {
console.error('Notification handler err:\n'+err.stack);
throw err;
}
if (!data._finishCalled) {
data.finish();
}
});
}
return EventEmitter.addListener(event, callback);
};
FCM.subscribeToTopic = (topic) => {
RNFIRMessaging.subscribeToTopic(topic);
};
FCM.unsubscribeFromTopic = (topic) => {
RNFIRMessaging.unsubscribeFromTopic(topic);
};
FCM.send = (senderId, payload) => {
RNFIRMessaging.send(senderId, payload);
};
FCM.setNotificationCategories = (categories) => {
if (Platform.OS === 'ios') {
RNFIRMessaging.setNotificationCategories(categories);
}
}
export default FCM;
export {};