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

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

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

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

Libin Lu's avatar
Libin Lu committed
13 14 15 16
FCM.getInitialNotification = () => {
    return FIRMessaging.getInitialNotification();
}

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

21 22
FCM.requestPermissions = () => {
    return FIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
23
};
Libin Lu's avatar
init  
Libin Lu committed
24

Libin Lu's avatar
Libin Lu committed
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
FCM.presentLocalNotification = (details) => {
    FIRMessaging.presentLocalNotification(details);
};

FCM.scheduleLocalNotification = function(details) {
    FIRMessaging.scheduleLocalNotification(details);
};

FCM.getScheduledLocalNotifications = function() {
    return FIRMessaging.getScheduledLocalNotifications();
};

FCM.cancelLocalNotification = (notificationID) => {
    FIRMessaging.cancelLocalNotification(notificationID);
};

FCM.cancelAllLocalNotifications = () => {
    FIRMessaging.cancelAllLocalNotifications();
};

FCM.setBadgeNumber = () => {
    FIRMessaging.setBadgeNumber();
}

FCM.getBadgeNumber = () => {
    return FIRMessaging.getBadgeNumber();
}

53 54 55
FCM.on = (event, callback) => {
    const nativeEvent = eventsMap[event];
    const listener = DeviceEventEmitter.addListener(nativeEvent, callback);
Goran Gajic's avatar
Goran Gajic committed
56

57 58 59
    return function remove() {
        listener.remove();
    };
Goran Gajic's avatar
Goran Gajic committed
60
};
Libin Lu's avatar
init  
Libin Lu committed
61

62 63
FCM.subscribeToTopic = (topic) => {
    FIRMessaging.subscribeToTopic(topic);
64
};
65 66 67

FCM.unsubscribeFromTopic = (topic) => {
    FIRMessaging.unsubscribeFromTopic(topic);
68 69 70
};

//once doesn't seem to work
Libin Lu's avatar
Libin Lu committed
71 72
DeviceEventEmitter.addListener('FCMInitData', (data) => {
    FCM.initialData = data;
73
});
74

Libin Lu's avatar
Libin Lu committed
75 76
FCM.initialData = FIRMessaging.initialData;

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