index.js 968 Bytes
Newer Older
1
import {NativeModules, DeviceEventEmitter} from 'react-native';
Libin Lu's avatar
init  
Libin Lu committed
2

Goran Gajic's avatar
Goran Gajic committed
3
const eventsMap = {
4 5
    refreshToken: 'FCMTokenRefreshed',
    notification: 'FCMNotificationReceived'
Goran Gajic's avatar
Goran Gajic committed
6
};
Libin Lu's avatar
init  
Libin Lu committed
7

Goran Gajic's avatar
Goran Gajic committed
8
const FIRMessaging = NativeModules.RNFIRMessaging;
9

Goran Gajic's avatar
Goran Gajic committed
10
const FCM = {};
Libin Lu's avatar
init  
Libin Lu committed
11

12
FCM.getFCMToken = () => {
Goran Gajic's avatar
Goran Gajic committed
13 14
    return FIRMessaging.getFCMToken();
};
Libin Lu's avatar
init  
Libin Lu committed
15

16 17
FCM.requestPermissions = () => {
    return FIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
18
};
Libin Lu's avatar
init  
Libin Lu committed
19

20 21
FCM.on = (event, callback) => {
    const nativeEvent = eventsMap[event];
Goran Gajic's avatar
Goran Gajic committed
22

23
    const listener = DeviceEventEmitter.addListener(nativeEvent, callback);
Goran Gajic's avatar
Goran Gajic committed
24

25 26 27
    return function remove() {
        listener.remove();
    };
Goran Gajic's avatar
Goran Gajic committed
28
};
Libin Lu's avatar
init  
Libin Lu committed
29

30 31
FCM.subscribeToTopic = (topic) => {
    FIRMessaging.subscribeToTopic(topic);
32
};
33 34 35

FCM.unsubscribeFromTopic = (topic) => {
    FIRMessaging.unsubscribeFromTopic(topic);
36 37 38 39 40 41
};

//once doesn't seem to work
DeviceEventEmitter.addListener('FCMInitData', (data)=>{
  FCM.initialData = data;
});
42

Libin Lu's avatar
Libin Lu committed
43 44
FCM.initialData = FIRMessaging.initialData;

Goran Gajic's avatar
Goran Gajic committed
45
module.exports = FCM;