2022-04-07 19:14:04 +00:00
|
|
|
import {
|
|
|
|
Notifications,
|
|
|
|
Registered,
|
|
|
|
RegistrationError,
|
|
|
|
NotificationCompletion,
|
|
|
|
Notification,
|
|
|
|
NotificationAction,
|
|
|
|
NotificationCategory
|
|
|
|
} from 'react-native-notifications';
|
2022-01-12 18:42:37 +00:00
|
|
|
|
|
|
|
import { INotification } from '../../definitions/INotification';
|
2022-04-07 19:14:04 +00:00
|
|
|
import { isIOS } from '../../utils/deviceInfo';
|
2022-04-12 16:37:28 +00:00
|
|
|
import { store as reduxStore } from '../store/auxStore';
|
2022-04-07 19:14:04 +00:00
|
|
|
import I18n from '../../i18n';
|
2022-01-12 18:42:37 +00:00
|
|
|
|
|
|
|
class PushNotification {
|
2022-04-07 19:14:04 +00:00
|
|
|
onNotification: (notification: any) => void;
|
2022-01-12 18:42:37 +00:00
|
|
|
deviceToken: string;
|
|
|
|
constructor() {
|
|
|
|
this.onNotification = () => {};
|
|
|
|
this.deviceToken = '';
|
2022-04-07 19:14:04 +00:00
|
|
|
if (isIOS) {
|
|
|
|
// init
|
|
|
|
Notifications.ios.registerRemoteNotifications();
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-04-07 19:14:04 +00:00
|
|
|
// setCategories
|
|
|
|
const notificationAction = new NotificationAction('REPLY_ACTION', 'background', I18n.t('Reply'), true, {
|
|
|
|
buttonTitle: I18n.t('Reply'),
|
|
|
|
placeholder: I18n.t('Type_message')
|
|
|
|
});
|
|
|
|
const notificationCategory = new NotificationCategory('MESSAGE', [notificationAction]);
|
|
|
|
Notifications.setCategories([notificationCategory]);
|
|
|
|
} else {
|
|
|
|
// init
|
|
|
|
Notifications.android.registerRemoteNotifications();
|
|
|
|
}
|
|
|
|
|
|
|
|
Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => {
|
|
|
|
this.deviceToken = event.deviceToken;
|
2022-01-12 18:42:37 +00:00
|
|
|
});
|
|
|
|
|
2022-04-07 19:14:04 +00:00
|
|
|
Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => {
|
|
|
|
// TODO: Handle error
|
|
|
|
console.log(event);
|
2022-01-12 18:42:37 +00:00
|
|
|
});
|
2022-04-07 19:14:04 +00:00
|
|
|
|
|
|
|
Notifications.events().registerNotificationReceivedForeground(
|
|
|
|
(notification: Notification, completion: (response: NotificationCompletion) => void) => {
|
2022-04-14 18:24:04 +00:00
|
|
|
completion({ alert: false, sound: false, badge: false });
|
2022-04-07 19:14:04 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
Notifications.events().registerNotificationOpened((notification: Notification, completion: () => void) => {
|
|
|
|
if (isIOS) {
|
|
|
|
const { background } = reduxStore.getState().app;
|
|
|
|
if (background) {
|
|
|
|
this.onNotification(notification);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.onNotification(notification);
|
|
|
|
}
|
|
|
|
completion();
|
|
|
|
});
|
|
|
|
|
|
|
|
Notifications.events().registerNotificationReceivedBackground(
|
|
|
|
(notification: Notification, completion: (response: any) => void) => {
|
|
|
|
completion({ alert: true, sound: true, badge: false });
|
|
|
|
}
|
|
|
|
);
|
2022-01-12 18:42:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getDeviceToken() {
|
|
|
|
return this.deviceToken;
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:14:04 +00:00
|
|
|
setBadgeCount = (count?: number) => {
|
|
|
|
if (isIOS && count) {
|
|
|
|
Notifications.ios.setBadgeCount(count);
|
|
|
|
}
|
|
|
|
};
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-04-07 19:14:04 +00:00
|
|
|
configure(onNotification: (notification: INotification) => void): Promise<any> {
|
2022-01-12 18:42:37 +00:00
|
|
|
this.onNotification = onNotification;
|
2022-04-07 19:14:04 +00:00
|
|
|
return Notifications.getInitialNotification();
|
2022-01-12 18:42:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default new PushNotification();
|