index.js 2.29 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.id = details.id || new Date().getTime().toString()
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 44 45 46
    if(!notificationID){
		return;
	}
	RNFIRMessaging.cancelLocalNotification(notificationID);
Libin Lu's avatar
Libin Lu committed
47 48 49
};

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

Libin Lu's avatar
Libin Lu committed
53 54 55 56 57 58 59 60 61 62 63
FCM.removeDeliveredNotification = (notificationID) => {
	if(!notificationID){
		return;
	}
	RNFIRMessaging.removeDeliveredNotification(notificationID);
}

FCM.removeAllDeliveredNotifications = () => {
	RNFIRMessaging.removeAllDeliveredNotifications();
}

Libin Lu's avatar
Libin Lu committed
64 65
FCM.setBadgeNumber = (number) => {
    RNFIRMessaging.setBadgeNumber(number);
Libin Lu's avatar
Libin Lu committed
66 67 68
}

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
69
    return RNFIRMessaging.getBadgeNumber();
Libin Lu's avatar
Libin Lu committed
70 71
}

72 73
FCM.on = (event, callback) => {
    const nativeEvent = eventsMap[event];
74
    if (!nativeEvent) {
Libin Lu's avatar
Libin Lu committed
75
        throw new Error('FCM event must be "refreshToken" or "notification"');
76
    }
77
    const listener = DeviceEventEmitter.addListener(nativeEvent, callback);
Goran Gajic's avatar
Goran Gajic committed
78

79 80 81
    return function remove() {
        listener.remove();
    };
Goran Gajic's avatar
Goran Gajic committed
82
};
Libin Lu's avatar
init  
Libin Lu committed
83

84
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
85
    RNFIRMessaging.subscribeToTopic(topic);
86
};
87 88

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
89
    RNFIRMessaging.unsubscribeFromTopic(topic);
90 91
};

92 93 94 95
FCM.send = (senderId, payload) => {
    RNFIRMessaging.send(senderId, payload);
};

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