index.js 1.83 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',
5
    notification: 'FCMNotificationReceived'
Goran Gajic's avatar
Goran Gajic committed
6
};
Libin Lu's avatar
init  
Libin Lu committed
7

Libin Lu's avatar
Libin Lu committed
8
const RNFIRMessaging = NativeModules.RNFIRMessaging;
9

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

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

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

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

Libin Lu's avatar
Libin Lu committed
24
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
25
    details.local_notification = true;
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) {
30 31 32
    if (!details.id) {
        throw new Error("id is required for scheduled notification");
    }
Libin Lu's avatar
Libin Lu committed
33
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
34
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
35 36 37
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
38
    return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
39 40 41
};

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

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

Libin Lu's avatar
Libin Lu committed
49 50
FCM.setBadgeNumber = (number) => {
    RNFIRMessaging.setBadgeNumber(number);
Libin Lu's avatar
Libin Lu committed
51 52 53
}

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
54
    return RNFIRMessaging.getBadgeNumber();
Libin Lu's avatar
Libin Lu committed
55 56
}

57 58
FCM.on = (event, callback) => {
    const nativeEvent = eventsMap[event];
59 60 61
    if (!nativeEvent) {
        throw new Error('FCM invalid event');
    }
62
    const listener = DeviceEventEmitter.addListener(nativeEvent, callback);
Goran Gajic's avatar
Goran Gajic committed
63

64 65 66
    return function remove() {
        listener.remove();
    };
Goran Gajic's avatar
Goran Gajic committed
67
};
Libin Lu's avatar
init  
Libin Lu committed
68

69
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
70
    RNFIRMessaging.subscribeToTopic(topic);
71
};
72 73

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
74
    RNFIRMessaging.unsubscribeFromTopic(topic);
75 76
};

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