Rocket.Chat.ReactNative/app/lib/notifications/videoConf/backgroundNotificationHandl...

126 lines
3.5 KiB
TypeScript
Raw Normal View History

import notifee, { AndroidCategory, AndroidFlags, AndroidImportance, AndroidVisibility, Event } from '@notifee/react-native';
2023-11-24 12:46:58 +00:00
import messaging from '@react-native-firebase/messaging';
import AsyncStorage from '@react-native-async-storage/async-storage';
import ejson from 'ejson';
import { deepLinkingClickCallPush } from '../../../actions/deepLinking';
import i18n from '../../../i18n';
import { BACKGROUND_PUSH_COLOR } from '../../constants';
import { store } from '../../store/auxStore';
const VIDEO_CONF_CHANNEL = 'video-conf-call';
const VIDEO_CONF_TYPE = 'videoconf';
interface Caller {
_id?: string;
name?: string;
}
interface NotificationData {
notificationType?: string;
status?: number;
rid?: string;
caller?: Caller;
}
const createChannel = () =>
notifee.createChannel({
id: VIDEO_CONF_CHANNEL,
name: 'Video Call',
lights: true,
vibration: true,
importance: AndroidImportance.HIGH,
sound: 'ringtone'
});
const handleBackgroundEvent = async (event: Event) => {
const { pressAction, notification } = event.detail;
const notificationData = notification?.data;
if (
typeof notificationData?.caller === 'object' &&
(notificationData.caller as Caller)?._id &&
(event.type === 1 || event.type === 2)
) {
if (store?.getState()?.app.ready) {
store.dispatch(deepLinkingClickCallPush({ ...notificationData, event: pressAction?.id }));
} else {
AsyncStorage.setItem('pushNotification', JSON.stringify({ ...notificationData, event: pressAction?.id }));
}
await notifee.cancelNotification(
`${notificationData.rid}${(notificationData.caller as Caller)._id}`.replace(/[^A-Za-z0-9]/g, '')
);
}
};
const backgroundNotificationHandler = () => {
notifee.onBackgroundEvent(handleBackgroundEvent);
};
const displayVideoConferenceNotification = async (notification: NotificationData) => {
const id = `${notification.rid}${notification.caller?._id}`.replace(/[^A-Za-z0-9]/g, '');
const actions = [
{
title: i18n.t('accept'),
pressAction: {
id: 'accept',
launchActivity: 'default'
}
},
{
title: i18n.t('decline'),
pressAction: {
id: 'decline',
launchActivity: 'default'
}
}
];
await notifee.displayNotification({
id,
title: i18n.t('conference_call'),
body: `${i18n.t('Incoming_call_from')} ${notification.caller?.name}`,
data: notification as { [key: string]: string | number | object },
android: {
channelId: VIDEO_CONF_CHANNEL,
category: AndroidCategory.CALL,
visibility: AndroidVisibility.PUBLIC,
importance: AndroidImportance.HIGH,
smallIcon: 'ic_notification',
color: BACKGROUND_PUSH_COLOR,
actions,
lightUpScreen: true,
loopSound: true,
sound: 'ringtone',
autoCancel: false,
ongoing: true,
pressAction: {
id: 'default',
launchActivity: 'default'
},
flags: [AndroidFlags.FLAG_NO_CLEAR]
2023-11-24 12:46:58 +00:00
}
});
};
const setBackgroundNotificationHandler = () => {
createChannel();
messaging().setBackgroundMessageHandler(async message => {
feat: mobile troubleshoot notifications (#5330) * feat: troubleshoot notification (#5198) * navigation done * create the icon inside roomslistview, navigation to push troubleshot and layout push troubleshoot * custom header * fix the rooms list view header icon * layout done * update the pt-br i18n * tweak on colors * feat: create notification in room view (#5250) * button and simple navigation done, missing master detail * navigation * add withTheme and colors to rightuttons * fix e2e test * feat: add troubleshooting to notifications pages (#5276) * feat: add troubleshooting to notifications pages * fix e2e test * feat: device notification settings (#5277) * iOS go to device notification setting to change the configuration * go to notification settings with android * add notifee * add the reducer and action * saga request done * add the setInAlert action * tweak at name and add focus to dispatch the request * use the foreground inside pushTroubleShoot to request the notification and fix the icon color * add the request at roomslistview didmount * remove the notification modulo from android * add patch * minor tweak * feat: test push notification (#5329) * feat: test push notification * restApi and definition * push.info and change properly the troubleshootingNotification * use the finally at try/catch * minor tweak * alert and push.info just for 6.6 * fix the react-native.config * minor tweaks * minor tweak * push.test as rest api * change the name from inAlertNotification to highlightTroubleshooting * feat: push quota * refactor the percentage state * removed the push quota feature * minor tweaks * update the link to push notification * the notification icon in the room header will appear if notifications are disabled or highlight troubleshoot is true * remove push quota texts * updated some of the push quota texts * chore: rename highlightTroubleshooting * chore: better prop naming * wip * chore: fix function name * chore: fix colors * fix: copy * chore: 💅 * chore: use fork * chore: naming * chore: fix init * chore: naming * chore: naming * Comment CE code * Use put on troubleshooting saga * Add db column * fix: check notification payload * action: organized translations * fix: push init --------- Co-authored-by: GleidsonDaniel <gleidson10daniel@hotmail.com> Co-authored-by: Diego Mello <diegolmello@gmail.com> Co-authored-by: GleidsonDaniel <GleidsonDaniel@users.noreply.github.com>
2024-03-04 11:27:24 +00:00
if (message?.data?.ejson) {
const notification: NotificationData = ejson.parse(message?.data?.ejson as string);
if (notification?.notificationType === VIDEO_CONF_TYPE) {
if (notification.status === 0) {
await displayVideoConferenceNotification(notification);
} else if (notification.status === 4) {
const id = `${notification.rid}${notification.caller?._id}`.replace(/[^A-Za-z0-9]/g, '');
await notifee.cancelNotification(id);
}
2023-11-24 12:46:58 +00:00
}
}
return null;
});
};
setBackgroundNotificationHandler();
backgroundNotificationHandler();