index.js 1.73 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) {
30 31 32
    if (!details.id) {
        throw new Error("id is required for scheduled notification");
    }
Libin Lu's avatar
Libin Lu committed
33
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
34 35 36
};

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

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

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

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

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

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

60 61 62
    return function remove() {
        listener.remove();
    };
Goran Gajic's avatar
Goran Gajic committed
63
};
Libin Lu's avatar
init  
Libin Lu committed
64

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

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

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