index.js 4.95 KB
Newer Older
Libin Lu's avatar
Libin Lu committed
1 2
import { NativeModules, NativeEventEmitter, Platform } from 'react-native';

Libin Lu's avatar
Libin Lu committed
3
const EventEmitter = new NativeEventEmitter(NativeModules.RNFIRMessaging || {});
Libin Lu's avatar
init  
Libin Lu committed
4

Libin Lu's avatar
Libin Lu committed
5 6
export const FCMEvent = {
  RefreshToken: 'FCMTokenRefreshed',
Libin Lu's avatar
Libin Lu committed
7 8 9
  Notification: 'FCMNotificationReceived',
  DirectChannelConnectionChanged: 'FCMDirectChannelConnectionChanged'
};
Libin Lu's avatar
Libin Lu committed
10 11 12 13 14

export const RemoteNotificationResult = {
  NewData: 'UIBackgroundFetchResultNewData',
  NoData: 'UIBackgroundFetchResultNoData',
  ResultFailed: 'UIBackgroundFetchResultFailed'
Libin Lu's avatar
Libin Lu committed
15
};
Libin Lu's avatar
Libin Lu committed
16 17 18 19

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

Libin Lu's avatar
Libin Lu committed
22 23 24 25 26
export const NotificationType = {
  Remote: 'remote_notification',
  NotificationResponse: 'notification_response',
  WillPresent: 'will_present_notification',
  Local: 'local_notification'
Libin Lu's avatar
Libin Lu committed
27
};
Libin Lu's avatar
Libin Lu committed
28

Libin Lu's avatar
Libin Lu committed
29
const RNFIRMessaging = NativeModules.RNFIRMessaging;
30

Goran Gajic's avatar
Goran Gajic committed
31
const FCM = {};
Libin Lu's avatar
init  
Libin Lu committed
32

Libin Lu's avatar
Libin Lu committed
33
FCM.getInitialNotification = () => {
Libin Lu's avatar
Libin Lu committed
34 35 36 37 38 39 40 41 42 43 44 45
  return RNFIRMessaging.getInitialNotification();
};

FCM.enableDirectChannel = () => {
  if (Platform.OS === 'ios') {
    return RNFIRMessaging.enableDirectChannel();
  }
};

FCM.isDirectChannelEstablished = () => {
  return Platform.OS === 'ios' ? RNFIRMessaging.isDirectChannelEstablished() : Promise.resolve(true);
};
Libin Lu's avatar
Libin Lu committed
46

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

51 52 53 54
FCM.deleteInstanceId = () =>{
  return RNFIRMessaging.deleteInstanceId();
};

Libin Lu's avatar
Libin Lu committed
55 56 57 58
FCM.getAPNSToken = () => {
  if (Platform.OS === 'ios') {
    return RNFIRMessaging.getAPNSToken();
  }
Goran Gajic's avatar
Goran Gajic committed
59
};
Libin Lu's avatar
init  
Libin Lu committed
60

61
FCM.requestPermissions = () => {
Libin Lu's avatar
Libin Lu committed
62
  return RNFIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
63
};
Libin Lu's avatar
init  
Libin Lu committed
64

Libin Lu's avatar
Libin Lu committed
65
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
66 67 68
  details.id = details.id || new Date().getTime().toString();
  details.local_notification = true;
  RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
69 70 71
};

FCM.scheduleLocalNotification = function(details) {
Libin Lu's avatar
Libin Lu committed
72 73 74 75 76
  if (!details.id) {
    throw new Error('id is required for scheduled notification');
  }
  details.local_notification = true;
  RNFIRMessaging.scheduleLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
77 78 79
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
80
  return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
81 82 83
};

FCM.cancelLocalNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
84 85 86 87
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.cancelLocalNotification(notificationID);
Libin Lu's avatar
Libin Lu committed
88 89 90
};

FCM.cancelAllLocalNotifications = () => {
Libin Lu's avatar
Libin Lu committed
91
  RNFIRMessaging.cancelAllLocalNotifications();
Libin Lu's avatar
Libin Lu committed
92 93
};

Libin Lu's avatar
Libin Lu committed
94
FCM.removeDeliveredNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
95 96 97 98 99
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.removeDeliveredNotification(notificationID);
};
Libin Lu's avatar
Libin Lu committed
100 101

FCM.removeAllDeliveredNotifications = () => {
Libin Lu's avatar
Libin Lu committed
102 103
  RNFIRMessaging.removeAllDeliveredNotifications();
};
Libin Lu's avatar
Libin Lu committed
104

Libin Lu's avatar
Libin Lu committed
105
FCM.setBadgeNumber = (number) => {
Libin Lu's avatar
Libin Lu committed
106 107
  RNFIRMessaging.setBadgeNumber(number);
};
Libin Lu's avatar
Libin Lu committed
108 109

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
110 111
  return RNFIRMessaging.getBadgeNumber();
};
Libin Lu's avatar
Libin Lu committed
112

Libin Lu's avatar
Libin Lu committed
113 114
function finish(result) {
  if (Platform.OS !== 'ios') {
Libin Lu's avatar
Libin Lu committed
115 116
    return;
  }
Libin Lu's avatar
Libin Lu committed
117
  if (!this._finishCalled && this._completionHandlerId) {
Libin Lu's avatar
Libin Lu committed
118
    this._finishCalled = true;
Libin Lu's avatar
Libin Lu committed
119
    switch (this._notificationType) {
Libin Lu's avatar
Libin Lu committed
120
      case NotificationType.Remote:
Libin Lu's avatar
Libin Lu committed
121
        result = result || RemoteNotificationResult.NoData;
Libin Lu's avatar
Libin Lu committed
122
        if (!Object.values(RemoteNotificationResult).includes(result)) {
Libin Lu's avatar
Libin Lu committed
123 124 125 126
          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
127
      case NotificationType.NotificationResponse:
Libin Lu's avatar
Libin Lu committed
128 129
        RNFIRMessaging.finishNotificationResponse(this._completionHandlerId);
        return;
Libin Lu's avatar
Libin Lu committed
130
      case NotificationType.WillPresent:
Libin Lu's avatar
Libin Lu committed
131
        result = result || (this.show_in_foreground ? WillPresentNotificationResult.All : WillPresentNotificationResult.None);
Libin Lu's avatar
Libin Lu committed
132
        if (!Object.values(WillPresentNotificationResult).includes(result)) {
Libin Lu's avatar
Libin Lu committed
133 134 135 136 137 138 139 140 141 142
          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;
    }
  }
}

143
FCM.on = (event, callback) => {
Libin Lu's avatar
Libin Lu committed
144 145 146 147 148 149 150 151
  if (!Object.values(FCMEvent).includes(event)) {
    throw new Error(`Invalid FCM event subscription, use import {FCMEvent} from 'react-native-fcm' to avoid typo`);
  };

  if (event === FCMEvent.Notification) {
    return EventEmitter.addListener(event, async(data) => {
      data.finish = finish;
      try {
Libin Lu's avatar
Libin Lu committed
152
        await callback(data);
Libin Lu's avatar
Libin Lu committed
153 154 155 156 157 158 159 160 161 162
      } catch (err) {
        console.error('Notification handler err', err);
        throw err;
      }
      if (!data._finishCalled) {
        data.finish();
      }
    });
  }
  return EventEmitter.addListener(event, callback);
Goran Gajic's avatar
Goran Gajic committed
163
};
Libin Lu's avatar
init  
Libin Lu committed
164

165
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
166
  RNFIRMessaging.subscribeToTopic(topic);
167
};
168 169

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
170
  RNFIRMessaging.unsubscribeFromTopic(topic);
171 172
};

173
FCM.send = (senderId, payload) => {
Libin Lu's avatar
Libin Lu committed
174
  RNFIRMessaging.send(senderId, payload);
175 176
};

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