index.android.js 1.67 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 setRegistrationTokenUpdateListener(listener) {
Lidan Hifi's avatar
Lidan Hifi committed
12
    registrationTokenUpdateListener = DeviceEventEmitter.addListener("remoteNotificationsRegistered", listener);
13 14 15 16 17 18 19 20 21 22
  }

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

  static setNotificationOpenedListener(listener) {
Lidan Hifi's avatar
Lidan Hifi committed
23
    notificationOpenedListener = DeviceEventEmitter.addListener("notificationOpened", (notification) => listener(new NotificationAndroid(notification)));
24 25 26 27
  }


  static setNotificationReceivedListener(listener) {
Lidan Hifi's avatar
Lidan Hifi committed
28
    notificationReceivedListener = DeviceEventEmitter.addListener("notificationReceived", (notification) => listener(new NotificationAndroid(notification)));
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
  }

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

  static clearNotificationReceivedListener() {
    if (notificationReceivedListener) {
      notificationReceivedListener.remove();
      notificationReceivedListener = null;
    }
  }
d4vidi's avatar
d4vidi committed
44 45 46 47

  static refreshToken() {
    RNNotifications.refreshToken();
  }
48 49
}

50
export class PendingNotifications {
51 52 53 54
  static async getInitialNotification() {
    return new NotificationAndroid(await RNNotifications.getInitialNotification());
  }
}