Files
react-native-notifications/example/notifications.android.js
2016-11-15 14:21:52 +02:00

71 lines
1.9 KiB
JavaScript

require('react');
import {NativeModules, DeviceEventEmitter} from 'react-native';
const RNNotifications = NativeModules.WixRNNotification;
let notificationReceivedListener;
let notificationOpenedListener;
let registrationTokenUpdateListener;
/** A wrapper to align Android with iOS in terms on notification structure. */
class NotificationAndroid {
constructor(notification) {
this.data = notification;
}
getData() {
return this.data;
}
getTitle() {
return this.data.title;
}
getMessage() {
return this.data.body;
}
}
export class NotificationsAndroid {
static setRegistrationTokenUpdateListener(listener) {
registrationTokenUpdateListener = DeviceEventEmitter.addListener('remoteNotificationsRegistered', listener);
}
static clearRegistrationTokenUpdateListener() {
if (registrationTokenUpdateListener) {
registrationTokenUpdateListener.remove();
registrationTokenUpdateListener = null;
}
}
static setNotificationOpenedListener(listener) {
notificationOpenedListener = DeviceEventEmitter.addListener('notificationOpened', (notification) => listener(new NotificationAndroid(notification)));
}
static setNotificationReceivedListener(listener) {
notificationReceivedListener = DeviceEventEmitter.addListener('notificationReceived', (notification) => listener(new NotificationAndroid(notification)));
}
static clearNotificationOpenedListener() {
if (notificationOpenedListener) {
notificationOpenedListener.remove();
notificationOpenedListener = null;
}
}
static clearNotificationReceivedListener() {
if (notificationReceivedListener) {
notificationReceivedListener.remove();
notificationReceivedListener = null;
}
}
}
export class PendingNotifications {
static async getInitialNotification() {
return new NotificationAndroid(await RNNotifications.getInitialNotification());
}
}