index.js 794 Bytes
Newer Older
Goran Gajic's avatar
Goran Gajic committed
1 2 3 4
import {
  NativeModules,
  DeviceEventEmitter,
} from 'react-native';
Libin Lu's avatar
init  
Libin Lu committed
5

Goran Gajic's avatar
Goran Gajic committed
6 7 8 9
const eventsMap = {
  refersh: 'FCMTokenRefreshed',
  notification: 'FCMNotificationReceived',
};
Libin Lu's avatar
init  
Libin Lu committed
10

Goran Gajic's avatar
Goran Gajic committed
11
const FIRMessaging = NativeModules.RNFIRMessaging;
12

Goran Gajic's avatar
Goran Gajic committed
13
const FCM = {};
Libin Lu's avatar
init  
Libin Lu committed
14

Goran Gajic's avatar
Goran Gajic committed
15 16 17
FCM.getFCMToken = function getFCMToken() {
    return FIRMessaging.getFCMToken();
};
Libin Lu's avatar
init  
Libin Lu committed
18

Goran Gajic's avatar
Goran Gajic committed
19 20 21
FCM.requestPermissions = function requestPermissions() {
  return FIRMessaging.requestPermissions();
};
Libin Lu's avatar
init  
Libin Lu committed
22

Goran Gajic's avatar
Goran Gajic committed
23 24 25 26 27 28 29 30 31 32 33
FCM.on = function on(event, callback) {
  const nativeEvent = eventsMap[event];

  const listener = DeviceEventEmitter.addListener(nativeEvent, (params) => {
    callback(params);
  });

  return function remove() {
    listener.remove();
  };
};
Libin Lu's avatar
init  
Libin Lu committed
34

Libin Lu's avatar
Libin Lu committed
35 36 37
FCM.initialData = FIRMessaging.initialData;
FCM.initialAction = FIRMessaging.initialAction;

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