index.js 4.38 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 53 54
    if (!details.fire_date) {
        throw new Error("fire_date is required for scheduled notification");
    }
Libin Lu's avatar
Libin Lu committed
55
    details.local_notification = true;
Libin Lu's avatar
Libin Lu committed
56
    RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
57 58 59
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
60
    return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
61 62 63
};

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

FCM.cancelAllLocalNotifications = () => {
Libin Lu's avatar
Libin Lu committed
71
    RNFIRMessaging.cancelAllLocalNotifications();
Libin Lu's avatar
Libin Lu committed
72 73
};

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

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

Libin Lu's avatar
Libin Lu committed
85 86
FCM.setBadgeNumber = (number) => {
    RNFIRMessaging.setBadgeNumber(number);
Libin Lu's avatar
Libin Lu committed
87 88 89
}

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
90
    return RNFIRMessaging.getBadgeNumber();
Libin Lu's avatar
Libin Lu committed
91 92
}

Libin Lu's avatar
Libin Lu committed
93 94 95 96 97 98 99

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

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

    if(event === FCMEvent.Notification){
      return DeviceEventEmitter.addListener(event, async(data)=>{
        data.finish = finish;
Libin Lu's avatar
Libin Lu committed
132
        await callback(data);
Libin Lu's avatar
Libin Lu committed
133 134 135 136 137 138
        if(!data._finishCalled){
          data.finish();
        }
      })
    }
    return DeviceEventEmitter.addListener(event, callback);
Goran Gajic's avatar
Goran Gajic committed
139
};
Libin Lu's avatar
init  
Libin Lu committed
140

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

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

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

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