index.ios.js 3.32 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 6 7
"use strict";
import { NativeModules, DeviceEventEmitter } from "react-native";
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();
Lidan Hifi's avatar
Lidan Hifi committed
17

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

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

31
export default class NotificationsIOS {
Lidan Hifi's avatar
Lidan Hifi committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45
  /**
   * 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) {
46
      let listener = DeviceEventEmitter.addListener(
Lidan Hifi's avatar
Lidan Hifi committed
47
        type,
48
        notification => handler(new IOSNotification(notification))
Lidan Hifi's avatar
Lidan Hifi committed
49
      );
Lidan Hifi's avatar
Lidan Hifi committed
50

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

  /**
   * 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) {
63
      let listener = _notificationHandlers.get(handler);
Lidan Hifi's avatar
Lidan Hifi committed
64 65 66
      if (!listener) {
        return;
      }
Lidan Hifi's avatar
Lidan Hifi committed
67

Lidan Hifi's avatar
Lidan Hifi committed
68 69
      listener.remove();
      _notificationHandlers.delete(handler);
Lidan Hifi's avatar
Lidan Hifi committed
70 71
    }
  }
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102

  static _actionHandlerGenerator(identifier: string, handler: Function) {
    return (action) => {
      if (action.identifier === identifier) {
        handler(action);
      }
    };
  }

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

    if (categories) {
      notificationCategories = categories.map(category => {
        return Object.assign({}, category.options, {
          actions: category.options.actions.map(action => {
            // subscribe to action event
            DeviceEventEmitter.addListener(DEVICE_NOTIFICATION_ACTION_RECEIVED, this._actionHandlerGenerator(action.options.identifier, action.handler));

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

    NativeRNNotifications.updateNotificationCategories(notificationCategories);
  }
Lidan Hifi's avatar
Lidan Hifi committed
103
}