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

Libin Lu's avatar
Libin Lu committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
export const FCMEvent = {
  RefreshToken: 'FCMTokenRefreshed',
  Notification: 'FCMNotificationReceived'
}

export const RemoteNotificationResult = {
  NewData: 'UIBackgroundFetchResultNewData',
  NoData: 'UIBackgroundFetchResultNoData',
  ResultFailed: 'UIBackgroundFetchResultFailed'
}

export const WillPresentNotificationResult = {
  All: 'UNNotificationPresentationOptionAll',
  None: 'UNNotificationPresentationOptionNone'
}
Libin Lu's avatar
init  
Libin Lu committed
18

Libin Lu's avatar
Libin Lu committed
19
const RNFIRMessaging = NativeModules.RNFIRMessaging;
20

Goran Gajic's avatar
Goran Gajic committed
21
const FCM = {};
Libin Lu's avatar
init  
Libin Lu committed
22

Libin Lu's avatar
Libin Lu committed
23
FCM.getInitialNotification = () => {
Libin Lu's avatar
Libin Lu committed
24
    return RNFIRMessaging.getInitialNotification();
Libin Lu's avatar
Libin Lu committed
25 26
}

27
FCM.getFCMToken = () => {
Libin Lu's avatar
Libin Lu committed
28
    return RNFIRMessaging.getFCMToken();
Goran Gajic's avatar
Goran Gajic committed
29
};
Libin Lu's avatar
init  
Libin Lu committed
30

31
FCM.requestPermissions = () => {
Libin Lu's avatar
Libin Lu committed
32
    return RNFIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
33
};
Libin Lu's avatar
init  
Libin Lu committed
34

Libin Lu's avatar
Libin Lu committed
35
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
36
    details.id = details.id || new Date().getTime().toString()
Libin Lu's avatar
Libin Lu committed
37
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
38
    RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
39 40 41
};

FCM.scheduleLocalNotification = function(details) {
42 43 44
    if (!details.id) {
        throw new Error("id is required for scheduled notification");
    }
Libin Lu's avatar
Libin Lu committed
45
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
46
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
47 48 49
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
50
    return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
51 52 53
};

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

FCM.cancelAllLocalNotifications = () => {
Libin Lu's avatar
Libin Lu committed
61
    RNFIRMessaging.cancelAllLocalNotifications();
Libin Lu's avatar
Libin Lu committed
62 63
};

Libin Lu's avatar
Libin Lu committed
64 65 66 67 68 69 70 71 72 73 74
FCM.removeDeliveredNotification = (notificationID) => {
	if(!notificationID){
		return;
	}
	RNFIRMessaging.removeDeliveredNotification(notificationID);
}

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

Libin Lu's avatar
Libin Lu committed
75 76
FCM.setBadgeNumber = (number) => {
    RNFIRMessaging.setBadgeNumber(number);
Libin Lu's avatar
Libin Lu committed
77 78 79
}

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
80
    return RNFIRMessaging.getBadgeNumber();
Libin Lu's avatar
Libin Lu committed
81 82
}

Libin Lu's avatar
Libin Lu committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113

function finish(result){
  if(Platform.OS !== 'ios'){
    return;
  }
  if(!this._finishCalled && this._completionHandlerId){
    this._finishCalled = true;
    switch(this.notification_event_type){
      case 'remote_notification':
        result = result || RemoteNotificationResult.NoData;
        if(!Object.values(RemoteNotificationResult).includes(result)){
          throw new Error(`Invalid RemoteNotificationResult, use import {RemoteNotificationResult} from 'react-native-fcm' to avoid typo`);
        }
        RNFIRMessaging.finishRemoteNotification(this._completionHandlerId, result);
        return;
      case 'notification_response':
        RNFIRMessaging.finishNotificationResponse(this._completionHandlerId);
        return;
      case 'will_present_notification':
        result = result || (this.show_in_foreground ? WillPresentNotificationResult.All : WillPresentNotificationResult.None);
        if(!Object.values(WillPresentNotificationResult).includes(result)){
          throw new Error(`Invalid WillPresentNotificationResult, make sure you use import {WillPresentNotificationResult} from 'react-native-fcm' to avoid typo`);
        }
        RNFIRMessaging.finishWillPresentNotification(this._completionHandlerId, result);
        return;
      default:
        return;
    }
  }
}

114
FCM.on = (event, callback) => {
Libin Lu's avatar
Libin Lu committed
115 116
    if (!Object.values(FCMEvent).includes(event)) {
        throw new Error(`Invalid FCM event subscription, use import {FCMEvent} from 'react-native-fcm' to avoid typo`);
117
    };
Libin Lu's avatar
Libin Lu committed
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

    if(event === FCMEvent.Notification){
      return DeviceEventEmitter.addListener(event, async(data)=>{
        data.finish = finish;
        try{
          await callback();
        } catch(err){
          console.error('Notification handler err', err)
          throw err;
        }
        if(!data._finishCalled){
          data.finish();
        }
      })
    }
    return DeviceEventEmitter.addListener(event, callback);
Goran Gajic's avatar
Goran Gajic committed
134
};
Libin Lu's avatar
init  
Libin Lu committed
135

136
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
137
    RNFIRMessaging.subscribeToTopic(topic);
138
};
139 140

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
141
    RNFIRMessaging.unsubscribeFromTopic(topic);
142 143
};

144 145 146 147
FCM.send = (senderId, payload) => {
    RNFIRMessaging.send(senderId, payload);
};

Libin Lu's avatar
Libin Lu committed
148
export default FCM;