index.ios.js 3.91 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 13
export const DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT = "notificationReceivedForeground";
export const DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT = "notificationReceivedBackground";
export const DEVICE_NOTIFICATION_OPENED_EVENT = "notificationOpened";
14
export const DEVICE_NOTIFICATION_ACTION_RECEIVED = "notificationActionReceived";
Lidan Hifi's avatar
Lidan Hifi committed
15

16
let _notificationHandlers = new Map();
17 18
let _actionHandlers = new Map();
let _actionListener;
Lidan Hifi's avatar
Lidan Hifi committed
19

20 21 22 23 24 25 26 27 28 29 30 31 32
export class NotificationAction {
  constructor(options: Object, handler: Function) {
    this.options = options;
    this.handler = handler;
  }
}

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

33
export default class NotificationsIOS {
Lidan Hifi's avatar
Lidan Hifi committed
34 35 36 37 38 39 40 41 42 43 44 45 46 47
  /**
   * Attaches a listener to remote notification events while the app is running
   * in the foreground or the background.
   *
   * Valid events are:
   *
   * - `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) {
    if (type === DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT ||
        type === DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT ||
        type === DEVICE_NOTIFICATION_OPENED_EVENT) {
48
      let listener = DeviceEventEmitter.addListener(
Lidan Hifi's avatar
Lidan Hifi committed
49
        type,
50
        notification => handler(new IOSNotification(notification))
Lidan Hifi's avatar
Lidan Hifi committed
51
      );
Lidan Hifi's avatar
Lidan Hifi committed
52

Lidan Hifi's avatar
Lidan Hifi committed
53
      _notificationHandlers.set(handler, listener);
Lidan Hifi's avatar
Lidan Hifi committed
54 55 56 57 58 59 60 61 62 63 64
    }
  }

  /**
   * Removes the event listener. Do this in `componentWillUnmount` to prevent
   * memory leaks
   */
  static removeEventListener(type: string, handler: Function) {
    if (type === DEVICE_NOTIFICATION_RECEIVED_FOREGROUND_EVENT ||
        type === DEVICE_NOTIFICATION_RECEIVED_BACKGROUND_EVENT ||
        type === DEVICE_NOTIFICATION_OPENED_EVENT) {
65
      let listener = _notificationHandlers.get(handler);
Lidan Hifi's avatar
Lidan Hifi committed
66 67 68
      if (!listener) {
        return;
      }
Lidan Hifi's avatar
Lidan Hifi committed
69

Lidan Hifi's avatar
Lidan Hifi committed
70 71
      listener.remove();
      _notificationHandlers.delete(handler);
Lidan Hifi's avatar
Lidan Hifi committed
72 73
    }
  }
74

75 76 77 78
  static _actionHandlerDispatcher(action: Object) {
    let actionHandler = _actionHandlers.get(action.identifier);

    if (actionHandler) {
79 80 81
      action.notification = new IOSNotification(action.notification);

      actionHandler(action, () => { NativeRNNotifications.completionHandler(); });
82
    }
83 84 85 86 87 88 89 90 91 92
  }

  /**
   * Sets the notification categories
   */
   /* eslint-disable no-unused-vars */
  static setCategories(categories: Array<NotificationCategory>) {
    let notificationCategories = [];

    if (categories) {
93
      // subscribe once for all actions
94
      _actionListener = NativeAppEventEmitter.addListener(DEVICE_NOTIFICATION_ACTION_RECEIVED, this._actionHandlerDispatcher.bind(this));
95

96 97 98 99
      notificationCategories = categories.map(category => {
        return Object.assign({}, category.options, {
          actions: category.options.actions.map(action => {
            // subscribe to action event
100
            _actionHandlers.set(action.options.identifier, action.handler);
101 102 103 104 105 106 107 108 109

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

    NativeRNNotifications.updateNotificationCategories(notificationCategories);
  }
110 111 112 113 114 115 116 117 118 119 120 121

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

    _actionHandlers.clear();
  }
122 123 124 125

  static log(message) {
    NativeRNNotifications.log(message);
  }
Lidan Hifi's avatar
Lidan Hifi committed
126
}