Skip to content

Instantly share code, notes, and snippets.

@guns2410
Created December 17, 2016 05:43
Show Gist options
  • Save guns2410/01813b90a5ecd1a40741022b983efaaa to your computer and use it in GitHub Desktop.
Save guns2410/01813b90a5ecd1a40741022b983efaaa to your computer and use it in GitHub Desktop.
import PushNotification from 'react-native-push-notification';
import OneSignal from 'react-native-onesignal';
import { store, Settings } from './';
import { Notifications } from '../actions';
class NotificationController {
constructor () {
this.store = store;
this.token = null;
this.userId = null;
this.device = {};
this._listeners = [];
this.configurePushNotification();
this.configureOneSignal();
OneSignal.enableVibrate(true);
OneSignal.enableSound(true);
OneSignal.enableNotificationsWhenActive(true);
OneSignal.setSubscription(true);
}
configurePushNotification () {
const self = this;
PushNotification.configure({
onRegister(token) {
console.log('Registered pushNotificationToken:', token);
self.token = token;
self.store.dispatch(Notifications.saveDeviceToken(token));
},
onNotification(notification) {
console.log("Received pushNotification", notification);
let notificationData = notification.data;
if (notificationData && notificationData.remote === true && notificationData.custom && notificationData.custom.a && notificationData.custom.a.type) {
let message = {};
switch (notificationData.custom.a.type) {
case 'alert':
message = {
id: notificationData.notificationId,
title: notificationData.custom.a.title,
message: notificationData.custom.a.content,
playSound: true,
category: 'TEST'
};
break
default:
message = null;
}
console.log('Pushing Notification:', message);
if (message) PushNotification.localNotification(message);
PushNotification.localNotificationSchedule({
message: "My Notification Message", // (required)
date: new Date(Date.now() + (5 * 1000)) // in 60 secs
});
}
self._listeners.forEach((listener) => {
try {
listener(notification);
} catch (err) {
console.warn("Listener failed to process push notification", err);
}
});
},
senderID: Settings.notifications.gcm,
permissions: {
alert: true,
badge: true,
sound: true
},
popInitialNotification: true,
requestPermissions: true,
});
}
configureOneSignal () {
const self = this;
OneSignal.configure({
onIdsAvailable (device) {
console.log('UserId = ', device.userId);
console.log('PushToken = ', device.pushToken);
self.userId = device.userId;
self.device = device;
self.store.dispatch(Notifications.saveNotificationUserId(device.userId));
},
onNotification (notification) {
console.log('Notification from OneSignal', notification);
},
onNotificationOpened (message, data, isActive) {
console.log('MESSAGE: ', message);
console.log('DATA: ', data);
console.log('ISACTIVE: ', isActive);
// Do whatever you want with the objects here
// _navigator.to('main.post', data.title, { // If applicable
// article: {
// title: data.title,
// link: data.url,
// action: data.actionSelected
// }
// });
}
});
}
addListener(listener) {
this._listeners.push(listener);
return () => {
this._listeners = this._listeners.filter((l) => l !== listener);
}
}
token() {
return this.token;
}
userId() {
return this.userId;
}
device() {
return this.device;
}
popInitial() {
PushNotification.configure({
popInitialNotification: true,
});
}
}
export default new NotificationController();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment