notifee: Notifications don't display when the app is in background. But it displays, when the app is in killed or foreground.

I am using Notifee with Firebase. Everything works when the app is in the foreground or killed state. But it doesn’t work when the app is in the background. I see the notifications come to devices with console.log but the DisplayNotificationData function doesn’t work until I open the app.

  • Set setBackgroundMessageHandler in ....Home\index.js
  • I send the only data notifications from Firebase as a solution for the notifications display twice in the killed state.
  • I checked and removed the Battery Optimization and Management restrictions.
// ....Home\index.js
messaging().setBackgroundMessageHandler(async remoteMessage => {
  console.log('Message handled in the background in index.js!', remoteMessage);

  // DisplayNotification(remoteMessage);
  DisplayNotificationData(remoteMessage);
}

Here is the displaying notifications function with Notifee

// android\app\src\utils\push_notification_helper.js
export async function DisplayNotificationData(remoteMessage) {
  // Required for iOS
  // See https://notifee.app/react-native/docs/ios/permissions
  await notifee.requestPermission();

  // Create a channel for android
  const channelId = await notifee.createChannel({
    id: 'important',
    name: 'Important Notifications',
    importance: AndroidImportance.HIGH,
  });

  // Access custom data
  const {title, body} = remoteMessage.data;

  // Display a notification using notifee
  await notifee.displayNotification({
    title: title,
    body: body,
    android: {
      channelId: channelId,
    },
  });

  console.log('DisplayNotificationData is worked!');
}

Here is the checking the restrictions:

// android\app\src\utils\push_notification_helper.js
// the function that check the Battery Optimization Enabled and it pops up turn off message.
export async function requestbatteryOptimizationTurnOff() {
  // 1. checks if battery optimization is enabled
  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: async () => await notifee.openBatteryOptimizationSettings(),
        },
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel',
        },
      ],
      {cancelable: false},
    );
  } else {
    console.log("Restrictions aren't detected for Battery Optimization");
  }
}

export async function requestPowerManagerSettingsTurnOff() {
  // 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: async () => await notifee.openPowerManagerSettings(),
        },
        {
          text: 'Cancel',
          onPress: () => console.log('Cancel Pressed'),
          style: 'cancel',
        },
      ],
      {cancelable: false},
    );
  } else {
    console.log("Restrictions aren't detected for Power Managment.");
  }
}

Thanks for the help

About this issue

  • Original URL
  • State: open
  • Created 6 months ago
  • Reactions: 2
  • Comments: 20

Most upvoted comments

the same problem was solved by excluding “await notifee.requestPermission();” from DisplayNotificationData in your case. Now data only push notifications come in any state of the application

I have same issue. My rootcause is I call method notifee.requestPermission without platform checking before method notifee.displayNotification.

@AhmetEkiz I’m facing the exact same problem.

I’m using notifee for dataSync and I’m using Foreground Service (asForegroundService: true) for this purpose. Here is my observation on different android versions

Android 12 and older:

  • Notification is not dismissable by user
  • Foreground service doesn’t stop when app is in background (minimized)
  • Foreground service doesn’t stop whenthe app is killed

Result: All works expected. This is my expected behaviour.

Android 13

  • Notification is not dismissable by user
  • Foreground service stops when app is in background (minimized)
  • Foreground service continues when app is opened from tray
  • When app is in background/minimized, Foreground service never starts when the app is killed directly from tray (without opening)
  • Foreground service doesn’t stop whenthe app is killed

Android 14

  • Notification is always dismissable by user. This is the design of Android 14 Foreground service
  • Foreground service stops when app is in background (minimized)
  • Foreground service continues when app is opened from tray
  • When app is in background/minimized, Foreground service never starts when the app is killed directly from tray (without opening)
  • Foreground service doesn’t stop when the app is killed

To me, the foreground service not running when app is in background or minimized is of concern.

I’m not sure if the workaround you suggested is applicable for my usecase (local notification). Any pointer would help.