index.js 6.05 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

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
export const NotificationCategoryOption = {
  CustomDismissAction: 'UNNotificationCategoryOptionCustomDismissAction',
  AllowInCarPlay: 'UNNotificationCategoryOptionAllowInCarPlay',
  PreviewsShowTitle: 'UNNotificationCategoryOptionHiddenPreviewsShowTitle',
  PreviewsShowSubtitle: 'UNNotificationCategoryOptionHiddenPreviewsShowSubtitle',
  None: 'UNNotificationCategoryOptionNone'
};

export const NotificationActionOption = {
  AuthenticationRequired: 'UNNotificationActionOptionAuthenticationRequired',
  Destructive: 'UNNotificationActionOptionDestructive',
  Foreground: 'UNNotificationActionOptionForeground',
  None: 'UNNotificationActionOptionNone',
};

export const NotificationActionType = {
  Default: 'UNNotificationActionTypeDefault',
  TextInput: 'UNNotificationActionTypeTextInput',
};

Libin Lu's avatar
Libin Lu committed
49
const RNFIRMessaging = NativeModules.RNFIRMessaging;
50

Goran Gajic's avatar
Goran Gajic committed
51
const FCM = {};
Libin Lu's avatar
init  
Libin Lu committed
52

Libin Lu's avatar
Libin Lu committed
53
FCM.getInitialNotification = () => {
Libin Lu's avatar
Libin Lu committed
54 55 56 57 58 59 60 61 62 63 64 65
  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
66

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

71 72 73 74
FCM.getEntityFCMToken = () => {
  return RNFIRMessaging.getEntityFCMToken();
}

75 76
FCM.deleteEntityFCMToken = () => {
  return RNFIRMessaging.deleteEntityFCMToken();
77 78
}

79 80 81 82
FCM.deleteInstanceId = () =>{
  return RNFIRMessaging.deleteInstanceId();
};

Libin Lu's avatar
Libin Lu committed
83 84 85 86
FCM.getAPNSToken = () => {
  if (Platform.OS === 'ios') {
    return RNFIRMessaging.getAPNSToken();
  }
Goran Gajic's avatar
Goran Gajic committed
87
};
Libin Lu's avatar
init  
Libin Lu committed
88

89
FCM.requestPermissions = () => {
Libin Lu's avatar
Libin Lu committed
90
  return RNFIRMessaging.requestPermissions();
Goran Gajic's avatar
Goran Gajic committed
91
};
Libin Lu's avatar
init  
Libin Lu committed
92

Libin Lu's avatar
Libin Lu committed
93
FCM.presentLocalNotification = (details) => {
Libin Lu's avatar
Libin Lu committed
94 95 96
  details.id = details.id || new Date().getTime().toString();
  details.local_notification = true;
  RNFIRMessaging.presentLocalNotification(details);
Libin Lu's avatar
Libin Lu committed
97 98 99
};

FCM.scheduleLocalNotification = function(details) {
Libin Lu's avatar
Libin Lu committed
100 101 102 103 104
  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
105 106 107
};

FCM.getScheduledLocalNotifications = function() {
Libin Lu's avatar
Libin Lu committed
108
  return RNFIRMessaging.getScheduledLocalNotifications();
Libin Lu's avatar
Libin Lu committed
109 110 111
};

FCM.cancelLocalNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
112 113 114 115
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.cancelLocalNotification(notificationID);
Libin Lu's avatar
Libin Lu committed
116 117 118
};

FCM.cancelAllLocalNotifications = () => {
Libin Lu's avatar
Libin Lu committed
119
  RNFIRMessaging.cancelAllLocalNotifications();
Libin Lu's avatar
Libin Lu committed
120 121
};

Libin Lu's avatar
Libin Lu committed
122
FCM.removeDeliveredNotification = (notificationID) => {
Libin Lu's avatar
Libin Lu committed
123 124 125 126 127
  if (!notificationID) {
    return;
  }
  RNFIRMessaging.removeDeliveredNotification(notificationID);
};
Libin Lu's avatar
Libin Lu committed
128 129

FCM.removeAllDeliveredNotifications = () => {
Libin Lu's avatar
Libin Lu committed
130 131
  RNFIRMessaging.removeAllDeliveredNotifications();
};
Libin Lu's avatar
Libin Lu committed
132

Libin Lu's avatar
Libin Lu committed
133
FCM.setBadgeNumber = (number) => {
Libin Lu's avatar
Libin Lu committed
134 135
  RNFIRMessaging.setBadgeNumber(number);
};
Libin Lu's avatar
Libin Lu committed
136 137

FCM.getBadgeNumber = () => {
Libin Lu's avatar
Libin Lu committed
138 139
  return RNFIRMessaging.getBadgeNumber();
};
Libin Lu's avatar
Libin Lu committed
140

Libin Lu's avatar
Libin Lu committed
141 142
function finish(result) {
  if (Platform.OS !== 'ios') {
Libin Lu's avatar
Libin Lu committed
143 144
    return;
  }
Libin Lu's avatar
Libin Lu committed
145
  if (!this._finishCalled && this._completionHandlerId) {
Libin Lu's avatar
Libin Lu committed
146
    this._finishCalled = true;
Libin Lu's avatar
Libin Lu committed
147
    switch (this._notificationType) {
Libin Lu's avatar
Libin Lu committed
148
      case NotificationType.Remote:
Libin Lu's avatar
Libin Lu committed
149
        result = result || RemoteNotificationResult.NoData;
Libin Lu's avatar
Libin Lu committed
150
        if (!Object.values(RemoteNotificationResult).includes(result)) {
Libin Lu's avatar
Libin Lu committed
151 152 153 154
          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
155
      case NotificationType.NotificationResponse:
Libin Lu's avatar
Libin Lu committed
156 157
        RNFIRMessaging.finishNotificationResponse(this._completionHandlerId);
        return;
Libin Lu's avatar
Libin Lu committed
158
      case NotificationType.WillPresent:
Libin Lu's avatar
Libin Lu committed
159
        result = result || (this.show_in_foreground ? WillPresentNotificationResult.All : WillPresentNotificationResult.None);
Libin Lu's avatar
Libin Lu committed
160
        if (!Object.values(WillPresentNotificationResult).includes(result)) {
Libin Lu's avatar
Libin Lu committed
161 162 163 164 165 166 167 168 169 170
          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;
    }
  }
}

171
FCM.on = (event, callback) => {
Libin Lu's avatar
Libin Lu committed
172 173 174 175 176 177 178 179
  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
180
        await callback(data);
Libin Lu's avatar
Libin Lu committed
181 182 183 184 185 186 187 188 189 190
      } 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
191
};
Libin Lu's avatar
init  
Libin Lu committed
192

193
FCM.subscribeToTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
194
  RNFIRMessaging.subscribeToTopic(topic);
195
};
196 197

FCM.unsubscribeFromTopic = (topic) => {
Libin Lu's avatar
Libin Lu committed
198
  RNFIRMessaging.unsubscribeFromTopic(topic);
199 200
};

201
FCM.send = (senderId, payload) => {
Libin Lu's avatar
Libin Lu committed
202
  RNFIRMessaging.send(senderId, payload);
203 204
};

205
FCM.setNotificationCategories = (categories) => {
206 207 208
  if (Platform.OS === 'ios') {
    RNFIRMessaging.setNotificationCategories(categories);
  }
209 210
}

Libin Lu's avatar
Libin Lu committed
211
export default FCM;
212 213

export {};