index.android.js 2.03 KB
Newer Older
Lidan Hifi's avatar
Lidan Hifi committed
1 2
import {NativeModules, DeviceEventEmitter} from "react-native";
import NotificationAndroid from "./notification";
3

4
const RNNotifications = NativeModules.WixRNNotifications;
5 6 7 8 9 10 11

let notificationReceivedListener;
let notificationOpenedListener;
let registrationTokenUpdateListener;

export class NotificationsAndroid {
  static setNotificationOpenedListener(listener) {
Lidan Hifi's avatar
Lidan Hifi committed
12
    notificationOpenedListener = DeviceEventEmitter.addListener("notificationOpened", (notification) => listener(new NotificationAndroid(notification)));
13 14 15 16 17 18 19 20 21
  }

  static clearNotificationOpenedListener() {
    if (notificationOpenedListener) {
      notificationOpenedListener.remove();
      notificationOpenedListener = null;
    }
  }

d4vidi's avatar
d4vidi committed
22 23 24 25
  static setNotificationReceivedListener(listener) {
    notificationReceivedListener = DeviceEventEmitter.addListener("notificationReceived", (notification) => listener(new NotificationAndroid(notification)));
  }

26 27 28 29 30 31
  static clearNotificationReceivedListener() {
    if (notificationReceivedListener) {
      notificationReceivedListener.remove();
      notificationReceivedListener = null;
    }
  }
d4vidi's avatar
d4vidi committed
32

d4vidi's avatar
d4vidi committed
33 34 35 36 37 38 39 40 41 42 43
  static setRegistrationTokenUpdateListener(listener) {
    registrationTokenUpdateListener = DeviceEventEmitter.addListener("remoteNotificationsRegistered", listener);
  }

  static clearRegistrationTokenUpdateListener() {
    if (registrationTokenUpdateListener) {
      registrationTokenUpdateListener.remove();
      registrationTokenUpdateListener = null;
    }
  }

d4vidi's avatar
d4vidi committed
44 45 46
  static refreshToken() {
    RNNotifications.refreshToken();
  }
47 48 49 50 51 52 53 54 55 56

  static localNotification(notification: Object) {
    const id = Date.now() | 0; // Bitwise-OR forces value onto a 32bit limit
    RNNotifications.postLocalNotification(notification, id);
    return id;
  }

  static cancelLocalNotification(id) {
    RNNotifications.cancelLocalNotification(id);
  }
57 58
}

59
export class PendingNotifications {
d4vidi's avatar
d4vidi committed
60 61 62 63 64
  static getInitialNotification() {
    return RNNotifications.getInitialNotification()
      .then((rawNotification) => {
        return new NotificationAndroid(rawNotification);
      });
65 66
  }
}