index.ios.js 8.88 KB
Newer Older
Lidan Hifi's avatar
Lidan Hifi committed
1 2 3
/**
 * @flow
 */
yogevbd's avatar
yogevbd committed
4
'use strict';
yogevbd's avatar
yogevbd committed
5
import { NativeModules, DeviceEventEmitter } from 'react-native';
yogevbd's avatar
yogevbd committed
6 7
import Map from 'core-js/library/es6/map';
import uuid from 'uuid';
yogevbd's avatar
WIP  
yogevbd committed
8
const NativeRNNotifications = NativeModules.RNBridgeModule; // eslint-disable-line no-unused-vars
yogevbd's avatar
yogevbd committed
9
import IOSNotification from './notification.ios';
Lidan Hifi's avatar
Lidan Hifi committed
10

yogevbd's avatar
yogevbd committed
11 12 13 14 15
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT = 'remoteNotificationsRegistered';
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT = 'remoteNotificationsRegistrationFailed';
export const DEVICE_PUSH_KIT_REGISTERED_EVENT = 'pushKitRegistered';
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = 'notificationReceivedForeground';
export const DEVICE_NOTIFICATION_OPENED_EVENT = 'notificationOpened';
yogevbd's avatar
yogevbd committed
16 17
export const DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT = 'pushKitNotificationReceived';

Lidan Hifi's avatar
Lidan Hifi committed
18

19 20
const _exportedEvents = [
  DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
21
  DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
22 23
  DEVICE_PUSH_KIT_REGISTERED_EVENT,
  DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT,
yogevbd's avatar
yogevbd committed
24 25
  DEVICE_NOTIFICATION_OPENED_EVENT,
  DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT
26
];
Leon Mok's avatar
Leon Mok committed
27 28
const _notificationHandlers = new Map();
const _actionHandlers = new Map();
Lidan Hifi's avatar
Lidan Hifi committed
29

30
export class NotificationAction {
mlanter's avatar
mlanter committed
31 32
  options: Object;

yogevbd's avatar
WIP  
yogevbd committed
33
  constructor(options: Object) {
34 35 36 37 38
    this.options = options;
  }
}

export class NotificationCategory {
mlanter's avatar
mlanter committed
39 40
  options: Object;

41 42 43 44 45
  constructor(options: Object) {
    this.options = options;
  }
}

46
export default class NotificationsIOS {
Lidan Hifi's avatar
Lidan Hifi committed
47 48 49 50 51 52
  /**
   * Attaches a listener to remote notification events while the app is running
   * in the foreground or the background.
   *
   * Valid events are:
   *
53
   * - `remoteNotificationsRegistered` : Fired when the user registers for remote notifications. The handler will be invoked with a hex string representing the deviceToken.
Lidan Hifi's avatar
Lidan Hifi committed
54 55
   * - `notificationReceivedForeground` : Fired when a notification (local / remote) is received when app is on foreground state.
   * - `notificationOpened`: Fired when a notification (local / remote) is opened.
yogevbd's avatar
yogevbd committed
56
   * - `pushKitNotificationReceived` : Fired when a pushKit notification received when app is both on foreground and background state.
Lidan Hifi's avatar
Lidan Hifi committed
57 58
   */
  static addEventListener(type: string, handler: Function) {
59 60
    if (_exportedEvents.indexOf(type) !== -1) {
      let listener;
yogevbd's avatar
yogevbd committed
61
      
62
      if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT) {
63
        listener = DeviceEventEmitter.addListener(
64 65 66
          DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
          registration => handler(registration.deviceToken)
        );
67
      } else if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT) {
68
        listener = DeviceEventEmitter.addListener(
69 70 71
          DEVICE_REMOTE_NOTIFICATIONS_REGISTRATION_FAILED_EVENT,
          error => handler(error)
        );
72
      } else if (type === DEVICE_PUSH_KIT_REGISTERED_EVENT) {
73
        listener = DeviceEventEmitter.addListener(
74 75 76
          DEVICE_PUSH_KIT_REGISTERED_EVENT,
          registration => handler(registration.pushKitToken)
        );
yogevbd's avatar
WIP  
yogevbd committed
77
      } else if (type === DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT) {
78
        listener = DeviceEventEmitter.addListener(
79
          type,
yogevbd's avatar
WIP  
yogevbd committed
80 81 82
          ({payload, identifier}) => handler(new IOSNotification(payload), (presentingOptions) => {
            NativeRNNotifications.finishPresentingNotification(identifier, presentingOptions);
          })
83
        );
yogevbd's avatar
WIP  
yogevbd committed
84 85 86
      } else if (type === DEVICE_NOTIFICATION_OPENED_EVENT) {
        listener = DeviceEventEmitter.addListener(
          type,
87
          ({payload, identifier, action}) => handler(new IOSNotification(payload), () => {
yogevbd's avatar
WIP  
yogevbd committed
88 89 90
            NativeRNNotifications.finishHandlingAction(identifier);
          }, action)
        );
yogevbd's avatar
yogevbd committed
91 92 93 94 95
      } else if (type === DEVICE_PUSH_KIT_NOTIFICATION_RECEIVED_EVENT) {
        listener = DeviceEventEmitter.addListener(
          type,
          (payload) => handler(new IOSNotification(payload))
        );
96
      }
Lidan Hifi's avatar
Lidan Hifi committed
97

Lidan Hifi's avatar
Lidan Hifi committed
98
      _notificationHandlers.set(handler, listener);
Lidan Hifi's avatar
Lidan Hifi committed
99 100 101 102 103 104 105 106
    }
  }

  /**
   * Removes the event listener. Do this in `componentWillUnmount` to prevent
   * memory leaks
   */
  static removeEventListener(type: string, handler: Function) {
107
    if (_exportedEvents.indexOf(type) !== -1) {
Leon Mok's avatar
Leon Mok committed
108 109 110 111
      const listener = _notificationHandlers.get(handler);
      if (listener) {
        listener.remove();
        _notificationHandlers.delete(handler);
Lidan Hifi's avatar
Lidan Hifi committed
112
      }
Lidan Hifi's avatar
Lidan Hifi committed
113 114 115

    }
  }
116 117 118 119

  /**
   * Sets the notification categories
   */
120
  static requestPermissions(categories: Array<NotificationCategory>) {
121 122 123 124 125 126 127
    let notificationCategories = [];

    if (categories) {
      notificationCategories = categories.map(category => {
        return Object.assign({}, category.options, {
          actions: category.options.actions.map(action => {
            // subscribe to action event
128
            _actionHandlers.set(action.options.identifier, action.handler);
129 130 131 132 133 134 135

            return action.options;
          })
        });
      });
    }

136
    NativeRNNotifications.requestPermissionsWithCategories(notificationCategories);
137 138 139 140 141 142
  }

  /**
   * Unregister for all remote notifications received via Apple Push Notification service.
   */
  static abandonPermissions() {
143
    NativeRNNotifications.abandonPermissions();
144
  }
145 146 147 148 149 150 151 152

  /**
   * Removes the event listener. Do this in `componentWillUnmount` to prevent
   * memory leaks
   */
  static resetCategories() {
    _actionHandlers.clear();
  }
153

154
  static getBadgesCount(callback: Function) {
155
    NativeRNNotifications.getBadgesCount(callback);
156 157
  }

158
  static setBadgesCount(count: number) {
159
    NativeRNNotifications.setBadgesCount(count);
160 161
  }

162
  static registerPushKit() {
163
    NativeRNNotifications.registerPushKit();
164 165 166
  }

  static backgroundTimeRemaining(callback: Function) {
167
    NativeRNNotifications.backgroundTimeRemaining(callback);
168 169
  }

170
  static log(message: string) {
171
    NativeRNNotifications.log(message);
172
  }
173

174
  static async getInitialNotification() {
175
    const notification = await NativeRNNotifications.getInitialNotification();
176 177 178 179 180 181 182
    if (notification) {
      return new IOSNotification(notification);
    } else {
      return undefined;
    }
  }

183 184 185 186 187
  /**
   * Presenting local notification
   *
   * notification is an object containing:
   *
yogevbd's avatar
WIP  
yogevbd committed
188 189
   * - `body` : The message displayed in the notification alert.
   * - `title` : The message title displayed in the notification.
yogevbd's avatar
yogevbd committed
190
   * - `alertAction` : The 'action' displayed beneath an actionable notification. Defaults to 'view';
yogevbd's avatar
WIP  
yogevbd committed
191
   * - `sound` : The sound played when the notification is fired (optional).
192
   * - `silent`    : If true, the notification sound will be suppressed (optional).
193 194 195 196 197
   * - `category`  : The category of this notification, required for actionable notifications (optional).
   * - `userInfo`  : An optional object containing additional notification data.
   * - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
   */
  static localNotification(notification: Object) {
Leon Mok's avatar
Leon Mok committed
198
    const notificationId = uuid.v4();
199
    NativeRNNotifications.localNotification(notification, notificationId);
200 201 202 203 204

    return notificationId;
  }

  static cancelLocalNotification(notificationId: String) {
205
    NativeRNNotifications.cancelLocalNotification(notificationId);
206 207 208
  }

  static cancelAllLocalNotifications() {
209
    NativeRNNotifications.cancelAllLocalNotifications();
210
  }
211

Ran Greenberg's avatar
Ran Greenberg committed
212
  static isRegisteredForRemoteNotifications() {
213
    return NativeRNNotifications.isRegisteredForRemoteNotifications();
214
  }
Ryan Eberhardt's avatar
Ryan Eberhardt committed
215 216

  static checkPermissions() {
217
    return NativeRNNotifications.checkPermissions();
Ryan Eberhardt's avatar
Ryan Eberhardt committed
218
  }
219

220 221 222 223
  /**
   * Remove all delivered notifications from Notification Center
   */
  static removeAllDeliveredNotifications() {
224
    return NativeRNNotifications.removeAllDeliveredNotifications();
225 226 227 228 229 230 231
  }

  /**
   * Removes the specified notifications from Notification Center
   *
   * @param identifiers Array of notification identifiers
   */
232
  static removeDeliveredNotifications(identifiers: Array<String>) {
233
    return NativeRNNotifications.removeDeliveredNotifications(identifiers);
234 235 236 237 238 239 240 241 242 243
  }

  /**
   * Provides you with a list of the app’s notifications that are still displayed in Notification Center
   *
   * @param callback Function which receive an array of delivered notifications
   *
   *  A delivered notification is an object containing:
   *
   * - `identifier`  : The identifier of this notification.
yogevbd's avatar
WIP  
yogevbd committed
244 245
   * - `body` : The message displayed in the notification alert.
   * - `title` : The message title displayed in the notification.
246 247 248 249 250
   * - `category`  : The category of this notification, if has one.
   * - `userInfo`  : An optional object containing additional notification data.
   * - `thread-id`  : The thread identifier of this notification, if has one.
   * - `fireDate` : The date and time when the system should deliver the notification. if not specified, the notification will be dispatched immediately.
   */
251
  static getDeliveredNotifications(callback: (notifications: Array<Object>) => void) {
252
    return NativeRNNotifications.getDeliveredNotifications(callback);
253
  }
Lidan Hifi's avatar
Lidan Hifi committed
254
}