hello, I have a problem, it turns out that I am running a foreground service to create a timer that alerts every certain time interval (this is dynamic), everything works fine when the cell phone is connected to power, even when the screen is locked , but when I try the service in the foreground disconnected from the electrical current, everything works fine until I lock the screen (after about 6 seconds the service pauses) and then resumes when I turn on the screen, it should be noted that I followed all the steps of background restrictions, disable battery optimization when the app is in the background, but this doesn't seem to have any effect, my device is samsung API 12.
PS: I also tried it on a xaomi device, but there the service only works when the app is in the foreground, if I exit the app, it immediately pauses.
PD2: sorry for my bad english.
PD3: I am using version 3.0.4 for compatibility issues with other packages
Here is my code snippet:
const very = async () => {
const batteryOptimizationEnabled = await notifee.isBatteryOptimizationEnabled();
if (batteryOptimizationEnabled) {
Alert.alert(
'Restrictions Detected',
'To ensure notifications are delivered, please disable battery optimization for the app.',
[
{
text: 'OK, open settings',
onPress: async () => await notifee.openBatteryOptimizationSettings(),
},
{
text: "Cancel",
onPress: () => console.log("Cancel Pressed"),
style: "cancel"
},
],
{ cancelable: false }
);
};
}
very();
notifee.registerForegroundService(async (notification) => {
const template = await getTemplateStandards();
return new Promise(() => {
let i = 0;
let executionTime = 0;
const setIntervalId = setInterval(() => {
const startTime = performance.now();
if (i !== 0 && (i + 10) % 60 === 0) {
template.forEach((period) => {
const minute = (i + 10)/60;
if (minute >= period.firstMinute && minute <= period.lastMinute && minute % period.intervals === 0) {
playSound();
Vibration.vibrate(2000);
}
});
}
notifee.displayNotification({
id: notification.id,
body: formatSeconds(i),
android: {
...notification.android,
},
});
i++;
const endTime = performance.now();
executionTime = (endTime - startTime);
}, 1000 - executionTime);
notifee.onForegroundEvent(async ({ type, detail }) => {
if (type === EventType.ACTION_PRESS && detail.pressAction.id === 'stopnotification') {
await notifee.stopForegroundService()
clearInterval(setIntervalId);
}
});
});
});
const formatSeconds = (secs) => {
return moment.utc(secs*1000).format('HH:mm:ss');
}
const playSound = async () => {
const sound = new Sound('alert.mp3', Sound.MAIN_BUNDLE, (error) => {
if (error) {
console.log('failed to load the sound', error);
return;
}
sound.play((success) => {
if (success) {
console.log('successfully finished playing');
} else {
console.log('playback failed due to audio decoding errors');
}
});
});
}
// Mi notification:
const displayNotification = async () => {
const channelId = await notifee.createChannel({
id: 'serviceBackground',
name: 'Notification Channel',
importance: AndroidImportance.HIGH,
});
await notifee.displayNotification({
id: 'serviciosegundoplano',
title: 'Cronometro',
body: '00:00:00',
android: {
channelId,
asForegroundService: true,
importance: AndroidImportance.HIGH,
pressAction: {
id: 'default',
},
actions: [
{
title: 'Stop',
pressAction: {
id: 'stopnotification',
},
},
],
},
});
}
hello, I have a problem, it turns out that I am running a foreground service to create a timer that alerts every certain time interval (this is dynamic), everything works fine when the cell phone is connected to power, even when the screen is locked , but when I try the service in the foreground disconnected from the electrical current, everything works fine until I lock the screen (after about 6 seconds the service pauses) and then resumes when I turn on the screen, it should be noted that I followed all the steps of background restrictions, disable battery optimization when the app is in the background, but this doesn't seem to have any effect, my device is samsung API 12.
PS: I also tried it on a xaomi device, but there the service only works when the app is in the foreground, if I exit the app, it immediately pauses.
PD2: sorry for my bad english.
PD3: I am using version 3.0.4 for compatibility issues with other packages
Here is my code snippet: