index.js 5.09 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 66 67 68 69 70
FCM.createNotificationChannel = (channel) => {
  if (Platform.OS === 'android') {
    return RNFIRMessaging.createNotificationChannel();
  }
}

Libin Lu's avatar
Libin Lu committed
71
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
72 73 74
  details.id = details.id || new Date().getTime().toString();
  details.local_notification = true;
  RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
75 76 77
};

FCM.scheduleLocalNotification = function(details) {
Libin Lu's avatar
Libin Lu committed
78 79 80 81 82
  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
83 84 85
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
86
  return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
87 88 89
};

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

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

Libin Lu's avatar
Libin Lu committed
100
FCM.removeDeliveredNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
101 102 103 104 105
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.removeDeliveredNotification(notificationID);
};
Libin Lu's avatar
Libin Lu committed
106 107

FCM.removeAllDeliveredNotifications = () => {
Libin Lu's avatar
Libin Lu committed
108 109
  RNFIRMessaging.removeAllDeliveredNotifications();
};
Libin Lu's avatar
Libin Lu committed
110

Libin Lu's avatar
Libin Lu committed
111
FCM.setBadgeNumber = (number) => {
Libin Lu's avatar
Libin Lu committed
112 113
  RNFIRMessaging.setBadgeNumber(number);
};
Libin Lu's avatar
Libin Lu committed
114 115

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
116 117
  return RNFIRMessaging.getBadgeNumber();
};
Libin Lu's avatar
Libin Lu committed
118

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

149
FCM.on = (event, callback) => {
Libin Lu's avatar
Libin Lu committed
150 151 152 153 154 155 156 157
  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
158
        await callback(data);
Libin Lu's avatar
Libin Lu committed
159 160 161 162 163 164 165 166 167 168
      } 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
169
};
Libin Lu's avatar
init  
Libin Lu committed
170

171
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
172
  RNFIRMessaging.subscribeToTopic(topic);
173
};
174 175

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
176
  RNFIRMessaging.unsubscribeFromTopic(topic);
177 178
};

179
FCM.send = (senderId, payload) => {
Libin Lu's avatar
Libin Lu committed
180
  RNFIRMessaging.send(senderId, payload);
181 182
};

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