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

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 51 52 53 54
  return RNFIRMessaging.getFCMToken();
};

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

57
FCM.requestPermissions = () => {
Libin Lu's avatar
Libin Lu committed
58
  return RNFIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
59
};
Libin Lu's avatar
init  
Libin Lu committed
60

Libin Lu's avatar
Libin Lu committed
61
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
62 63 64
  details.id = details.id || new Date().getTime().toString();
  details.local_notification = true;
  RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
65 66 67
};

FCM.scheduleLocalNotification = function(details) {
Libin Lu's avatar
Libin Lu committed
68 69 70 71 72
  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
73 74 75
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
76
  return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
77 78 79
};

FCM.cancelLocalNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
80 81 82 83
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.cancelLocalNotification(notificationID);
Libin Lu's avatar
Libin Lu committed
84 85 86
};

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

Libin Lu's avatar
Libin Lu committed
90
FCM.removeDeliveredNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
91 92 93 94 95
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.removeDeliveredNotification(notificationID);
};
Libin Lu's avatar
Libin Lu committed
96 97

FCM.removeAllDeliveredNotifications = () => {
Libin Lu's avatar
Libin Lu committed
98 99
  RNFIRMessaging.removeAllDeliveredNotifications();
};
Libin Lu's avatar
Libin Lu committed
100

Libin Lu's avatar
Libin Lu committed
101
FCM.setBadgeNumber = (number) => {
Libin Lu's avatar
Libin Lu committed
102 103
  RNFIRMessaging.setBadgeNumber(number);
};
Libin Lu's avatar
Libin Lu committed
104 105

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
106 107
  return RNFIRMessaging.getBadgeNumber();
};
Libin Lu's avatar
Libin Lu committed
108

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

139
FCM.on = (event, callback) => {
Libin Lu's avatar
Libin Lu committed
140 141 142 143 144 145 146 147
  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
148
        await callback(data);
Libin Lu's avatar
Libin Lu committed
149 150 151 152 153 154 155 156 157 158
      } 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
159
};
Libin Lu's avatar
init  
Libin Lu committed
160

161
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
162
  RNFIRMessaging.subscribeToTopic(topic);
163
};
164 165

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

169
FCM.send = (senderId, payload) => {
Libin Lu's avatar
Libin Lu committed
170
  RNFIRMessaging.send(senderId, payload);
171 172
};

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