2018-07-10 13:40:32 +00:00
|
|
|
import NotificationsIOS from 'react-native-notifications';
|
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
import reduxStore from '../../lib/createStore';
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
class PushNotification {
|
|
|
|
constructor() {
|
|
|
|
this.onRegister = null;
|
|
|
|
this.onNotification = null;
|
|
|
|
this.deviceToken = null;
|
|
|
|
|
|
|
|
NotificationsIOS.addEventListener('remoteNotificationsRegistered', (deviceToken) => {
|
|
|
|
this.deviceToken = deviceToken;
|
|
|
|
});
|
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
NotificationsIOS.addEventListener('notificationOpened', (notification, completion) => {
|
|
|
|
const { background } = reduxStore.getState().app;
|
|
|
|
if (background) {
|
|
|
|
this.onNotification(notification);
|
|
|
|
}
|
|
|
|
completion();
|
2018-07-10 13:40:32 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
NotificationsIOS.requestPermissions();
|
|
|
|
}
|
|
|
|
|
|
|
|
getDeviceToken() {
|
|
|
|
return this.deviceToken;
|
|
|
|
}
|
|
|
|
|
2018-10-31 18:40:39 +00:00
|
|
|
setBadgeCount = (count = 0) => {
|
|
|
|
NotificationsIOS.setBadgesCount(count);
|
|
|
|
}
|
|
|
|
|
2019-06-26 12:33:56 +00:00
|
|
|
async configure(params) {
|
2018-07-10 13:40:32 +00:00
|
|
|
this.onRegister = params.onRegister;
|
|
|
|
this.onNotification = params.onNotification;
|
|
|
|
|
2019-06-26 12:33:56 +00:00
|
|
|
const initial = await NotificationsIOS.getInitialNotification();
|
2019-08-20 19:24:22 +00:00
|
|
|
// NotificationsIOS.consumeBackgroundQueue();
|
2019-06-26 12:33:56 +00:00
|
|
|
return Promise.resolve(initial);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
export default new PushNotification();
|