2022-04-07 19:14:04 +00:00
|
|
|
import {
|
|
|
|
Notifications,
|
|
|
|
Registered,
|
|
|
|
RegistrationError,
|
|
|
|
NotificationCompletion,
|
|
|
|
Notification,
|
|
|
|
NotificationAction,
|
|
|
|
NotificationCategory
|
|
|
|
} from 'react-native-notifications';
|
2023-07-12 18:28:25 +00:00
|
|
|
import { PermissionsAndroid, Platform } from 'react-native';
|
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-12-21 16:07:17 +00:00
|
|
|
export const removeAllNotifications = (): void => {
|
|
|
|
Notifications.removeAllDeliveredNotifications();
|
|
|
|
};
|
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
export const pushNotificationConfigure = (onNotification: (notification: INotification) => void): Promise<any> => {
|
|
|
|
if (isIOS) {
|
|
|
|
// init
|
|
|
|
Notifications.ios.registerRemoteNotifications();
|
2023-11-28 20:13:45 +00:00
|
|
|
|
2022-05-13 14:37:02 +00:00
|
|
|
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
|
|
|
});
|
2023-11-28 20:13:45 +00:00
|
|
|
const acceptAction = new NotificationAction('ACCEPT_ACTION', 'foreground', I18n.t('accept'), true);
|
|
|
|
const rejectAction = new NotificationAction('DECLINE_ACTION', 'foreground', I18n.t('decline'), true);
|
2024-01-19 16:14:25 +00:00
|
|
|
|
|
|
|
const notificationCategory = new NotificationCategory('MESSAGE', [notificationAction]);
|
2023-11-28 20:13:45 +00:00
|
|
|
const videoConfCategory = new NotificationCategory('VIDEOCONF', [acceptAction, rejectAction]);
|
|
|
|
|
2024-01-19 16:14:25 +00:00
|
|
|
Notifications.setCategories([videoConfCategory, notificationCategory]);
|
2023-07-12 18:28:25 +00:00
|
|
|
} else if (Platform.OS === 'android' && Platform.constants.Version >= 33) {
|
2023-08-18 17:48:33 +00:00
|
|
|
// @ts-ignore
|
|
|
|
PermissionsAndroid.request('android.permission.POST_NOTIFICATIONS').then(permissionStatus => {
|
2023-07-12 18:28:25 +00:00
|
|
|
if (permissionStatus === 'granted') {
|
|
|
|
Notifications.registerRemoteNotifications();
|
|
|
|
} else {
|
|
|
|
// TODO: Ask user to enable notifications
|
|
|
|
}
|
|
|
|
});
|
2022-05-13 14:37:02 +00:00
|
|
|
} else {
|
2023-07-12 18:28:25 +00:00
|
|
|
Notifications.registerRemoteNotifications();
|
2022-05-13 14:37:02 +00:00
|
|
|
}
|
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();
|
|
|
|
};
|