index.js 4.39 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 20 21 22 23 24 25
export const NotificationType = {
  Remote: 'remote_notification',
  NotificationResponse: 'notification_response',
  WillPresent: 'will_present_notification',
  Local: 'local_notification'
}

Libin Lu's avatar
Libin Lu committed
26
const RNFIRMessaging = NativeModules.RNFIRMessaging;
27

Goran Gajic's avatar
Goran Gajic committed
28
const FCM = {};
Libin Lu's avatar
init  
Libin Lu committed
29

Libin Lu's avatar
Libin Lu committed
30
FCM.getInitialNotification = () => {
Libin Lu's avatar
Libin Lu committed
31
    return RNFIRMessaging.getInitialNotification();
Libin Lu's avatar
Libin Lu committed
32 33
}

34
FCM.getFCMToken = () => {
Libin Lu's avatar
Libin Lu committed
35
    return RNFIRMessaging.getFCMToken();
Goran Gajic's avatar
Goran Gajic committed
36
};
Libin Lu's avatar
init  
Libin Lu committed
37

38
FCM.requestPermissions = () => {
Libin Lu's avatar
Libin Lu committed
39
    return RNFIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
40
};
Libin Lu's avatar
init  
Libin Lu committed
41

Libin Lu's avatar
Libin Lu committed
42
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
43
    details.id = details.id || new Date().getTime().toString()
Libin Lu's avatar
Libin Lu committed
44
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
45
    RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
46 47 48
};

FCM.scheduleLocalNotification = function(details) {
49 50 51
    if (!details.id) {
        throw new Error("id is required for scheduled notification");
    }
Libin Lu's avatar
Libin Lu committed
52
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
53
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
54 55 56
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
57
    return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
58 59 60
};

FCM.cancelLocalNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
61 62 63 64
    if(!notificationID){
		return;
	}
	RNFIRMessaging.cancelLocalNotification(notificationID);
Libin Lu's avatar
Libin Lu committed
65 66 67
};

FCM.cancelAllLocalNotifications = () => {
Libin Lu's avatar
Libin Lu committed
68
    RNFIRMessaging.cancelAllLocalNotifications();
Libin Lu's avatar
Libin Lu committed
69 70
};

Libin Lu's avatar
Libin Lu committed
71 72 73 74 75 76 77 78 79 80 81
FCM.removeDeliveredNotification = (notificationID) => {
	if(!notificationID){
		return;
	}
	RNFIRMessaging.removeDeliveredNotification(notificationID);
}

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

Libin Lu's avatar
Libin Lu committed
82 83
FCM.setBadgeNumber = (number) => {
    RNFIRMessaging.setBadgeNumber(number);
Libin Lu's avatar
Libin Lu committed
84 85 86
}

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
87
    return RNFIRMessaging.getBadgeNumber();
Libin Lu's avatar
Libin Lu committed
88 89
}

Libin Lu's avatar
Libin Lu committed
90 91 92 93 94 95 96

function finish(result){
  if(Platform.OS !== 'ios'){
    return;
  }
  if(!this._finishCalled && this._completionHandlerId){
    this._finishCalled = true;
Libin Lu's avatar
Libin Lu committed
97 98
    switch(this._notificationType){
      case NotificationType.Remote:
Libin Lu's avatar
Libin Lu committed
99 100 101 102 103 104
        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;
Libin Lu's avatar
Libin Lu committed
105
      case NotificationType.NotificationResponse:
Libin Lu's avatar
Libin Lu committed
106 107
        RNFIRMessaging.finishNotificationResponse(this._completionHandlerId);
        return;
Libin Lu's avatar
Libin Lu committed
108
      case NotificationType.WillPresent:
Libin Lu's avatar
Libin Lu committed
109 110 111 112 113 114 115 116 117 118 119 120
        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;
    }
  }
}

121
FCM.on = (event, callback) => {
Libin Lu's avatar
Libin Lu committed
122 123
    if (!Object.values(FCMEvent).includes(event)) {
        throw new Error(`Invalid FCM event subscription, use import {FCMEvent} from 'react-native-fcm' to avoid typo`);
124
    };
Libin Lu's avatar
Libin Lu committed
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140

    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
141
};
Libin Lu's avatar
init  
Libin Lu committed
142

143
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
144
    RNFIRMessaging.subscribeToTopic(topic);
145
};
146 147

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
148
    RNFIRMessaging.unsubscribeFromTopic(topic);
149 150
};

151 152 153 154
FCM.send = (senderId, payload) => {
    RNFIRMessaging.send(senderId, payload);
};

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