index.js 1.62 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

Libin Lu's avatar
Libin Lu committed
9
const RNFIRMessaging = 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
FCM.getInitialNotification = () => {
Libin Lu's avatar
Libin Lu committed
14
    return RNFIRMessaging.getInitialNotification();
Libin Lu's avatar
Libin Lu committed
15 16
}

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

21
FCM.requestPermissions = () => {
Libin Lu's avatar
Libin Lu committed
22
    return RNFIRMessaging.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
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
26
    RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
27 28 29
};

FCM.scheduleLocalNotification = function(details) {
Libin Lu's avatar
Libin Lu committed
30
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
31 32 33
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
34
    return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
35 36 37
};

FCM.cancelLocalNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
38
    RNFIRMessaging.cancelLocalNotification(notificationID);
Libin Lu's avatar
Libin Lu committed
39 40 41
};

FCM.cancelAllLocalNotifications = () => {
Libin Lu's avatar
Libin Lu committed
42
    RNFIRMessaging.cancelAllLocalNotifications();
Libin Lu's avatar
Libin Lu committed
43 44 45
};

FCM.setBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
46
    RNFIRMessaging.setBadgeNumber();
Libin Lu's avatar
Libin Lu committed
47 48 49
}

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
50
    return RNFIRMessaging.getBadgeNumber();
Libin Lu's avatar
Libin Lu committed
51 52
}

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
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
63
    RNFIRMessaging.subscribeToTopic(topic);
64
};
65 66

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
67
    RNFIRMessaging.unsubscribeFromTopic(topic);
68 69
};

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