Skip to content

Instantly share code, notes, and snippets.

@Salakar
Created June 23, 2020 15:59
Show Gist options
  • Save Salakar/87a776f78f8886f9f9e6dadc1d43a532 to your computer and use it in GitHub Desktop.
Save Salakar/87a776f78f8886f9f9e6dadc1d43a532 to your computer and use it in GitHub Desktop.
Testing foreground service notifications in Notifee with progress indicators.
Notifee.registerForegroundService(notification => {
console.warn('Foreground service started.');
return new Promise(resolve => {
/**
* Cancel the notification and resolve the service promise so the Headless task quits.
*/
async function stopService(): Promise<void> {
console.warn('Stopping service.');
if (notification?.id) {
await Notifee.cancelNotification(notification?.id);
}
return resolve();
}
/**
* Cancel our long running task if the user presses the 'stop' action.
*/
async function handleStopActionEvent({ type, detail }: Event): Promise<void> {
if (type != EventType.ACTION_PRESS) return;
if (detail?.pressAction?.id === 'stop') {
console.warn('Stop action was pressed');
await stopService();
}
}
Notifee.onForegroundEvent(handleStopActionEvent);
Notifee.onBackgroundEvent(handleStopActionEvent);
// A fake progress updater.
let current = 1;
const interval = setInterval(async () => {
notification.android.progress.current = current;
await Notifee.displayNotification(notification);
current++;
}, 125);
setTimeout(async () => {
clearInterval(interval);
console.warn('Background work has completed.');
await stopService();
}, 15000);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment