index.android.js 2.72 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

let notificationReceivedListener;
7
let notificationReceivedInForegroundListener;
8 9 10 11 12
let notificationOpenedListener;
let registrationTokenUpdateListener;

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

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

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

27 28 29 30
  static setNotificationReceivedInForegroundListener(listener) {
    notificationReceivedInForegroundListener = DeviceEventEmitter.addListener("notificationReceivedInForeground", (notification) => listener(new NotificationAndroid(notification)));
  }

31 32 33 34 35 36
  static clearNotificationReceivedListener() {
    if (notificationReceivedListener) {
      notificationReceivedListener.remove();
      notificationReceivedListener = null;
    }
  }
d4vidi's avatar
d4vidi committed
37

38 39 40 41 42 43 44
  static clearNotificationReceivedInForegroundListener() {
    if (notificationReceivedInForegroundListener) {
      notificationReceivedInForegroundListener.remove();
      notificationReceivedInForegroundListener = null;
    }
  }

d4vidi's avatar
d4vidi committed
45 46 47 48 49 50 51 52 53 54 55
  static setRegistrationTokenUpdateListener(listener) {
    registrationTokenUpdateListener = DeviceEventEmitter.addListener("remoteNotificationsRegistered", listener);
  }

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

56 57 58 59
  static async isRegisteredForRemoteNotifications() {
    return await RNNotifications.isRegisteredForRemoteNotifications();
  }

d4vidi's avatar
d4vidi committed
60 61 62
  static refreshToken() {
    RNNotifications.refreshToken();
  }
63 64

  static localNotification(notification: Object) {
d4vidi's avatar
d4vidi committed
65
    const id = Math.random() * 100000000 | 0; // Bitwise-OR forces value onto a 32bit limit
66 67 68 69 70 71 72
    RNNotifications.postLocalNotification(notification, id);
    return id;
  }

  static cancelLocalNotification(id) {
    RNNotifications.cancelLocalNotification(id);
  }
73 74
}

75
export class PendingNotifications {
d4vidi's avatar
d4vidi committed
76 77 78
  static getInitialNotification() {
    return RNNotifications.getInitialNotification()
      .then((rawNotification) => {
79
        return rawNotification ? new NotificationAndroid(rawNotification) : undefined;
d4vidi's avatar
d4vidi committed
80
      });
81 82
  }
}