index.js 1.81 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
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
27
    RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
28 29 30
};

FCM.scheduleLocalNotification = function(details) {
31 32 33
    if (!details.id) {
        throw new Error("id is required for scheduled notification");
    }
Libin Lu's avatar
Libin Lu committed
34
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
35
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
36 37 38
};

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

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

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

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

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

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

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

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

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

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