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
|
|
|
|
2022-06-06 14:17:51 +00:00
|
|
|
import { INotification } from '../../definitions';
|
|
|
|
import { isIOS } from '../methods/helpers';
|
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
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
export let deviceToken = '';
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
export const setNotificationsBadgeCount = (count = 0): void => {
|
|
|
|
if (isIOS) {
|
|
|
|
Notifications.ios.setBadgeCount(count);
|
|
|
|
}
|
|
|
|
};
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
export const pushNotificationConfigure = (onNotification: (notification: INotification) => void): Promise<any> => {
|
|
|
|
if (isIOS) {
|
|
|
|
// init
|
|
|
|
Notifications.ios.registerRemoteNotifications();
|
|
|
|
// setCategories
|
|
|
|
const notificationAction = new NotificationAction('REPLY_ACTION', 'background', I18n.t('Reply'), true, {
|
|
|
|
buttonTitle: I18n.t('Reply'),
|
|
|
|
placeholder: I18n.t('Type_message')
|
2022-01-12 18:42:37 +00:00
|
|
|
});
|
2022-05-13 14:37:02 +00:00
|
|
|
const notificationCategory = new NotificationCategory('MESSAGE', [notificationAction]);
|
|
|
|
Notifications.setCategories([notificationCategory]);
|
|
|
|
} else {
|
|
|
|
// init
|
|
|
|
Notifications.android.registerRemoteNotifications();
|
|
|
|
}
|
2022-04-07 19:14:04 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
Notifications.events().registerRemoteNotificationsRegistered((event: Registered) => {
|
|
|
|
deviceToken = event.deviceToken;
|
|
|
|
});
|
2022-04-07 19:14:04 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
Notifications.events().registerRemoteNotificationsRegistrationFailed((event: RegistrationError) => {
|
|
|
|
// TODO: Handle error
|
|
|
|
console.log(event);
|
|
|
|
});
|
2022-04-07 19:14:04 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
Notifications.events().registerNotificationReceivedForeground(
|
|
|
|
(notification: Notification, completion: (response: NotificationCompletion) => void) => {
|
|
|
|
completion({ alert: false, sound: false, badge: false });
|
|
|
|
}
|
|
|
|
);
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
Notifications.events().registerNotificationOpened((notification: Notification, completion: () => void) => {
|
2022-05-03 11:53:18 +00:00
|
|
|
if (isIOS) {
|
2022-05-13 14:37:02 +00:00
|
|
|
const { background } = reduxStore.getState().app;
|
|
|
|
if (background) {
|
|
|
|
onNotification(notification);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
onNotification(notification);
|
2022-04-07 19:14:04 +00:00
|
|
|
}
|
2022-05-13 14:37:02 +00:00
|
|
|
completion();
|
|
|
|
});
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
Notifications.events().registerNotificationReceivedBackground(
|
|
|
|
(notification: Notification, completion: (response: any) => void) => {
|
|
|
|
completion({ alert: true, sound: true, badge: false });
|
|
|
|
}
|
|
|
|
);
|
2022-01-12 18:42:37 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
return Notifications.getInitialNotification();
|
|
|
|
};
|