-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
55 lines (43 loc) · 1.42 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
'use strict';
/**
* This exposes the native ToastAndroid module as a JS module. This has a
* function 'show' which takes the following parameters:
*
* 1. String message: A string with the text to toast
* 2. int duration: The duration of the toast. May be ToastAndroid.SHORT or
* ToastAndroid.LONG
*/
import { NativeModules, DeviceEventEmitter, Platform } from 'react-native';
export const FCMEvent = {
RefreshToken: 'FCMTokenRefreshed',
Notification: 'FCMNotificationReceived'
}
const RNFirebaseModule = NativeModules.RNFirebaseModule;
const FCM = {};
FCM.getInitialNotification = () => {
return RNFirebaseModule.getInitialNotification();
}
FCM.getFCMToken = () => {
return RNFirebaseModule.getFCMToken();
};
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)=>{
try{
await callback(data);
} catch(err){
console.error('Notification handler err', err)
throw err;
}
})
}
return DeviceEventEmitter.addListener(event, callback);
};
FCM.send = (senderId, payload) => {
RNFirebaseModule.send(senderId, payload);
};
export default FCM;
//module.exports = NativeModules.RNFirebaseModule;