wip: working android

This commit is contained in:
GleidsonDaniel 2023-10-05 15:11:59 -03:00
parent c3cd9fa701
commit bed57ba863
1 changed files with 65 additions and 54 deletions

View File

@ -1,60 +1,61 @@
import notifee, { AndroidCategory, AndroidImportance, AndroidVisibility } from '@notifee/react-native';
import messaging from '@react-native-firebase/messaging';
import ejson from 'ejson';
import { Alert } from 'react-native';
import i18n from '../../i18n';
import { BACKGROUND_PUSH_COLOR } from '../constants';
import { store } from '../store/auxStore';
import { deepLinkingClickCallPush } from '../../actions/deepLinking';
export const backgroundNotificationHandler = async (): Promise<void> => {
// 1. get info on the device and the Power Manager settings
const powerManagerInfo = await notifee.getPowerManagerInfo();
if (powerManagerInfo.activity) {
// 2. ask your users to adjust their settings
Alert.alert(
'Restrictions Detected',
'To ensure notifications are delivered, please adjust your settings to prevent the app from being killed',
[
// 3. launch intent to navigate the user to the appropriate screen
{
text: 'OK, open settings',
onPress: notifee.openPowerManagerSettings
},
{
text: 'Cancel',
onPress: () => {
// TODO: handle cancel
},
style: 'cancel'
}
],
{ cancelable: false }
);
}
// // 1. get info on the device and the Power Manager settings
// const powerManagerInfo = await notifee.getPowerManagerInfo();
// if (powerManagerInfo.activity) {
// // 2. ask your users to adjust their settings
// Alert.alert(
// 'Restrictions Detected',
// 'To ensure notifications are delivered, please adjust your settings to prevent the app from being killed',
// [
// // 3. launch intent to navigate the user to the appropriate screen
// {
// text: 'OK, open settings',
// onPress: notifee.openPowerManagerSettings
// },
// {
// text: 'Cancel',
// onPress: () => {
// // TODO: handle cancel
// },
// style: 'cancel'
// }
// ],
// { cancelable: false }
// );
// }
const batteryOptimizationEnabled = await notifee.isBatteryOptimizationEnabled();
if (batteryOptimizationEnabled) {
// 2. ask your users to disable the feature
Alert.alert(
'Restrictions Detected',
'To ensure notifications are delivered, please disable battery optimization for the app.',
[
// 3. launch intent to navigate the user to the appropriate screen
{
text: 'OK, open settings',
onPress: notifee.openBatteryOptimizationSettings
},
{
text: 'Cancel',
onPress: () => {
// TODO: handle cancel
},
style: 'cancel'
}
],
{ cancelable: false }
);
}
// const batteryOptimizationEnabled = await notifee.isBatteryOptimizationEnabled();
// if (batteryOptimizationEnabled) {
// // 2. ask your users to disable the feature
// Alert.alert(
// 'Restrictions Detected',
// 'To ensure notifications are delivered, please disable battery optimization for the app.',
// [
// // 3. launch intent to navigate the user to the appropriate screen
// {
// text: 'OK, open settings',
// onPress: notifee.openBatteryOptimizationSettings
// },
// {
// text: 'Cancel',
// onPress: () => {
// // TODO: handle cancel
// },
// style: 'cancel'
// }
// ],
// { cancelable: false }
// );
// }
// videoConf channel
await notifee.createChannel({
@ -62,13 +63,20 @@ export const backgroundNotificationHandler = async (): Promise<void> => {
name: 'Video Call',
lights: true,
vibration: true,
importance: AndroidImportance.HIGH
importance: AndroidImportance.HIGH,
sound: 'ringtone'
});
notifee.onBackgroundEvent(
event =>
new Promise(() => {
console.log('onBackgroundEvent', event);
console.log('event', event);
if (event.detail.pressAction?.id === 'accept' || event.detail.pressAction?.id === 'decline') {
store.dispatch(deepLinkingClickCallPush(event.detail?.notification?.data));
notifee.cancelNotification(
getNumbersAndLettersOnly(event.detail?.notification?.data.rid + event.detail?.notification?.data.caller._id)
);
}
})
);
};
@ -81,7 +89,6 @@ function getNumbersAndLettersOnly(inputString: string) {
const setBackgroundNotificationHandler = (): void => {
messaging().setBackgroundMessageHandler(async (n: any) => {
const notification = ejson.parse(n.data.ejson);
console.log('setBackgroundMessageHandler', n);
if (notification?.notificationType === 'videoconf') {
if (notification.status === 0) {
await notifee.displayNotification({
@ -95,7 +102,6 @@ const setBackgroundNotificationHandler = (): void => {
visibility: AndroidVisibility.PUBLIC,
importance: AndroidImportance.HIGH,
smallIcon: 'ic_notification',
timestamp: Date.now(),
color: BACKGROUND_PUSH_COLOR,
actions: [
{
@ -108,11 +114,16 @@ const setBackgroundNotificationHandler = (): void => {
{
title: i18n.t('decline'),
pressAction: {
id: 'decline'
id: 'decline',
launchActivity: 'default'
}
}
],
lightUpScreen: true
lightUpScreen: true,
loopSound: true,
sound: 'ringtone',
autoCancel: false,
ongoing: true
}
});
}