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

11 12
export const DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT = "remoteNotificationsRegistered";
export const DEVICE_PUSH_KIT_REGISTERED_EVENT = "pushKitRegistered";
13 14 15
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = "notificationReceivedForeground";
export const DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT = "notificationReceivedBackground";
export const DEVICE_NOTIFICATION_OPENED_EVENT = "notificationOpened";
Lidan Hifi's avatar
Lidan Hifi committed
16

17 18 19 20 21 22 23 24 25
const DEVICE_NOTIFICATION_ACTION_RECEIVED = "notificationActionReceived";

const _exportedEvents = [
  DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
  DEVICE_PUSH_KIT_REGISTERED_EVENT,
  DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT,
  DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT,
  DEVICE_NOTIFICATION_OPENED_EVENT
];
26
let _notificationHandlers = new Map();
27 28
let _actionHandlers = new Map();
let _actionListener;
Lidan Hifi's avatar
Lidan Hifi committed
29

30 31 32 33 34 35 36 37 38 39 40 41 42
export class NotificationAction {
  constructor(options: Object, handler: Function) {
    this.options = options;
    this.handler = handler;
  }
}

export class NotificationCategory {
  constructor(options: Object) {
    this.options = options;
  }
}

43
export default class NotificationsIOS {
Lidan Hifi's avatar
Lidan Hifi committed
44 45 46 47 48 49
  /**
   * Attaches a listener to remote notification events while the app is running
   * in the foreground or the background.
   *
   * Valid events are:
   *
50
   * - `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
51 52 53 54 55
   * - `notificationReceivedForeground` : Fired when a notification (local / remote) is received when app is on foreground state.
   * - `notificationReceivedBackground`: Fired when a background notification is received.
   * - `notificationOpened`: Fired when a notification (local / remote) is opened.
   */
  static addEventListener(type: string, handler: Function) {
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    if (_exportedEvents.indexOf(type) !== -1) {
      let listener;

      if (type === DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT) {
        listener = DeviceEventEmitter.addListener(
          DEVICE_REMOTE_NOTIFICATIONS_REGISTERED_EVENT,
          registration => handler(registration.deviceToken)
        );
      } else if (type === DEVICE_PUSH_KIT_REGISTERED_EVENT) {
        listener = DeviceEventEmitter.addListener(
          DEVICE_PUSH_KIT_REGISTERED_EVENT,
          registration => handler(registration.pushKitToken)
        );
      } else {
        listener = DeviceEventEmitter.addListener(
          type,
          notification => handler(new IOSNotification(notification))
        );
      }
Lidan Hifi's avatar
Lidan Hifi committed
75

Lidan Hifi's avatar
Lidan Hifi committed
76
      _notificationHandlers.set(handler, listener);
Lidan Hifi's avatar
Lidan Hifi committed
77 78 79 80 81 82 83 84
    }
  }

  /**
   * Removes the event listener. Do this in `componentWillUnmount` to prevent
   * memory leaks
   */
  static removeEventListener(type: string, handler: Function) {
85
    if (_exportedEvents.indexOf(type) !== -1) {
86
      let listener = _notificationHandlers.get(handler);
Lidan Hifi's avatar
Lidan Hifi committed
87 88 89
      if (!listener) {
        return;
      }
Lidan Hifi's avatar
Lidan Hifi committed
90

Lidan Hifi's avatar
Lidan Hifi committed
91 92
      listener.remove();
      _notificationHandlers.delete(handler);
Lidan Hifi's avatar
Lidan Hifi committed
93 94
    }
  }
95

96 97 98 99
  static _actionHandlerDispatcher(action: Object) {
    let actionHandler = _actionHandlers.get(action.identifier);

    if (actionHandler) {
100 101 102
      action.notification = new IOSNotification(action.notification);

      actionHandler(action, () => { NativeRNNotifications.completionHandler(); });
103
    }
104 105 106 107 108
  }

  /**
   * Sets the notification categories
   */
109
  static requestPermissions(categories: Array<NotificationCategory>) {
110 111 112
    let notificationCategories = [];

    if (categories) {
113
      // subscribe once for all actions
114
      _actionListener = NativeAppEventEmitter.addListener(DEVICE_NOTIFICATION_ACTION_RECEIVED, this._actionHandlerDispatcher.bind(this));
115

116 117 118 119
      notificationCategories = categories.map(category => {
        return Object.assign({}, category.options, {
          actions: category.options.actions.map(action => {
            // subscribe to action event
120
            _actionHandlers.set(action.options.identifier, action.handler);
121 122 123 124 125 126 127

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

128 129 130 131 132 133 134 135
    NativeRNNotifications.requestPermissionsWithCategories(notificationCategories);
  }

  /**
   * Unregister for all remote notifications received via Apple Push Notification service.
   */
  static abandonPermissions() {
    NativeRNNotifications.abandonPermissions();
136
  }
137 138 139 140 141 142 143 144 145 146 147 148

  /**
   * Removes the event listener. Do this in `componentWillUnmount` to prevent
   * memory leaks
   */
  static resetCategories() {
    if (_actionListener) {
      _actionListener.remove();
    }

    _actionHandlers.clear();
  }
149

150 151 152 153 154 155 156 157
  static registerPushKit() {
    NativeRNNotifications.registerPushKit();
  }

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

158 159 160
  static log(message) {
    NativeRNNotifications.log(message);
  }
Lidan Hifi's avatar
Lidan Hifi committed
161
}