index.ios.js 2.08 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
let 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";
Lidan Hifi's avatar
Lidan Hifi committed
14

15
let _notificationHandlers = new Map();
Lidan Hifi's avatar
Lidan Hifi committed
16

17
export default class NotificationsIOS {
Lidan Hifi's avatar
Lidan Hifi committed
18 19 20 21 22 23 24 25 26 27 28 29 30 31
  /**
   * 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) {
32
      let listener = DeviceEventEmitter.addListener(
Lidan Hifi's avatar
Lidan Hifi committed
33
        type,
34
        notification => handler(new IOSNotification(notification))
Lidan Hifi's avatar
Lidan Hifi committed
35
      );
Lidan Hifi's avatar
Lidan Hifi committed
36

Lidan Hifi's avatar
Lidan Hifi committed
37
      _notificationHandlers.set(handler, listener);
Lidan Hifi's avatar
Lidan Hifi committed
38 39 40 41 42 43 44 45 46 47 48
    }
  }

  /**
   * 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) {
49
      let listener = _notificationHandlers.get(handler);
Lidan Hifi's avatar
Lidan Hifi committed
50 51 52
      if (!listener) {
        return;
      }
Lidan Hifi's avatar
Lidan Hifi committed
53

Lidan Hifi's avatar
Lidan Hifi committed
54 55
      listener.remove();
      _notificationHandlers.delete(handler);
Lidan Hifi's avatar
Lidan Hifi committed
56 57 58
    }
  }
}