2020-01-29 20:20:36 +00:00
|
|
|
import NotificationsIOS, { NotificationAction, NotificationCategory } from 'react-native-notifications';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2019-08-20 19:24:22 +00:00
|
|
|
import reduxStore from '../../lib/createStore';
|
2020-01-29 20:20:36 +00:00
|
|
|
import I18n from '../../i18n';
|
|
|
|
|
|
|
|
const replyAction = new NotificationAction({
|
|
|
|
activationMode: 'background',
|
|
|
|
title: I18n.t('Reply'),
|
|
|
|
textInput: {
|
|
|
|
buttonTitle: I18n.t('Reply'),
|
|
|
|
placeholder: I18n.t('Type_message')
|
|
|
|
},
|
|
|
|
identifier: 'REPLY_ACTION'
|
|
|
|
});
|
2019-08-20 19:24:22 +00:00
|
|
|
|
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
|
|
|
});
|
|
|
|
|
2020-01-29 20:20:36 +00:00
|
|
|
const actions = [];
|
|
|
|
actions.push(new NotificationCategory({
|
|
|
|
identifier: 'MESSAGE',
|
|
|
|
actions: [replyAction]
|
|
|
|
}));
|
|
|
|
NotificationsIOS.requestPermissions(actions);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|