question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Android 12] Foreground Service Takes Time To Show Notification First Time

See original GitHub issue

On Android 12, Notifee’s Foreground Service notification is taking around 10 seconds to show up in status bar. Another problem is that, every notification update wakes up the screen when screen is locked & turned off.

Notifee Version: 4.0.1 React Native: 0.66.4

Here is my App.js file:

import React, {useEffect, useState} from 'react';
import {View, StyleSheet, Button} from 'react-native';
import notifee from '@notifee/react-native';

const NOTIFICATION = {ID: 'id', NAME: 'My Notifications'};

let interval = false;
let timer = 0;

const registerService = () => {
  notifee.createChannel({id: NOTIFICATION.ID, name: NOTIFICATION.NAME});
  notifee.registerForegroundService(notification => {
    return new Promise(() => {
      interval = setInterval(() => {
        timer = timer + 1;
        notifee.displayNotification({
          id: notification.id,
          body: `${timer.toString()}`,
          android: {
            ...notification.android,
          },
        });
      }, 1000);
    });
  });
};

const App = () => {
  const [isRunning, setRunning] = useState(false);

  useEffect(() => {
    registerService();
  }, []);

  const startPress = () => {
    setRunning(bool => !bool);
    notifee.displayNotification({
      title: 'Foreground service',
      body: 'This notification will exist for the lifetime of the service runner',
      android: {
        channelId: NOTIFICATION.ID,
        asForegroundService: true,
      },
    });
  };

  const onCancelPress = async () => {
    setRunning(bool => !bool);
    await notifee.stopForegroundService();
    clearInterval(interval);
  };

  return (
    <View style={styles.screen}>
      <Button
        title={isRunning ? 'Close Notification' : 'Show Notification'}
        onPress={isRunning ? onCancelPress : startPress}
      />
    </View>
  );
};

const styles = StyleSheet.create({
  screen: {flex: 1, justifyContent: 'center', alignItems: 'center'},
});

export default App;

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
helenafordcommented, Dec 23, 2021

@mikehardy oh really. wow. just scanned the docs:

Devices that run Android 12 (API level 31) or higher provide a streamlined experience for short-running foreground services. On these devices, the system waits 10 seconds before showing the notification associated with a foreground service. There are a few exceptions; several types of services always display a notification immediately.

a 10-second delay 😱 it states there are exceptions, so maybe there are ways to get around this 🤔 (ref: https://developer.android.com/guide/components/foreground-services#notification-immediate)

1reaction
mikehardycommented, Dec 21, 2021

Very interesting! Android 12 added something specific about foreground notifications for background services, they put a delay in there as users were apparently irritated by notifications briefly appearing and disappearing when background services did a foreground service but were in fact short lived (though long-lived enough they had to do a notification or face a system kill). So this might be related to the new delay they added. I wonder if we need to add a toggle or something saying “no really, show it immediately” ? I haven’t looked at the APIs closely I just remember hearing about the delay they added

Read more comments on GitHub >

github_iconTop Results From Across the Web

Android Foreground Service Notification Delayed
On these devices, the system waits 10 seconds before showing the notification associated with a foreground service.
Read more >
Foreground services - Android Developers
On these devices, the system waits 10 seconds before showing the notification associated with a foreground service. There are a few exceptions; several...
Read more >
Foreground service launch restrictions-Android12
Android 12 or higher provides a smooth experience for short-running foreground services, the system waits 10 seconds before showing the notification associated ...
Read more >
Auto-Start Foreground Service in Android - Medium
Today I'm going to teach you about restartable foreground service even phone will turn off and turn on again or even rebooted the...
Read more >
How to Implement a Foreground Service with Notifications in ...
For example, if we are developing an Android application to keep track of the pomodoro technique, we want to show the user the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found