index.android.js 2.2 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;
    }
  }

44 45 46 47
  static async isRegisteredForRemoteNotifications() {
    return await RNNotifications.isRegisteredForRemoteNotifications();
  }

d4vidi's avatar
d4vidi committed
48 49 50
  static refreshToken() {
    RNNotifications.refreshToken();
  }
51 52

  static localNotification(notification: Object) {
d4vidi's avatar
d4vidi committed
53
    const id = Math.random() * 100000000 | 0; // Bitwise-OR forces value onto a 32bit limit
54 55 56 57 58 59 60
    RNNotifications.postLocalNotification(notification, id);
    return id;
  }

  static cancelLocalNotification(id) {
    RNNotifications.cancelLocalNotification(id);
  }
61 62
}

63
export class PendingNotifications {
d4vidi's avatar
d4vidi committed
64 65 66
  static getInitialNotification() {
    return RNNotifications.getInitialNotification()
      .then((rawNotification) => {
67
        return rawNotification ? new NotificationAndroid(rawNotification) : undefined;
d4vidi's avatar
d4vidi committed
68
      });
69 70
  }
}