invertase / notifee

⚛️ A feature rich notifications library for React Native.
https://notifee.app
Apache License 2.0
1.87k stars 226 forks source link

Local push notifications not working when app is in background #830

Closed sarakovacheska closed 10 months ago

sarakovacheska commented 1 year ago

I've implemented local notifications in the app, but they are not working when the app is in the background. Only pop-up when the app is in the foreground. Is this a bug?

tjbo commented 1 year ago

iOS cuts off your app after a minute or from background processing. Pretty sure it's not a Notifee thing.

I got around it by scheduling notifications in the future using triggered notifications:

https://notifee.app/react-native/reference/createtriggernotification

JayJiangCT commented 1 year ago

I'm having the same problem on my Android phone, I only get notifications when I switch the app to the foreground, which seems very weird!

JayJiangCT commented 1 year ago
Mobile Model Operating System Issue Description Steps to Reproduce
Samsung A71 Android 13 Location notification doesn't appear when the app is in the background 1. Allow notifications.
2. Continuously and uninterruptedly create a Notification every 5 seconds and display it.
3. It could appear when the app is in the foreground.
But it never appeared when the app was in the background until I switched the app to the foreground again
MRMayllo commented 1 year ago

I'm having the same problem on my Android phone, I only get notifications when I switch the app to the foreground, which seems very weird!

try to add priority high in your FCM/GCM payload like this and send the body or title in the "data" not in the "notification". { priority: "high", data: { "whateverKe"y: "whateverValue", } }

or like here in the issue https://github.com/invertase/notifee/issues/813

JayJiangCT commented 1 year ago

I'm having the same problem on my Android phone, I only get notifications when I switch the app to the foreground, which seems very weird!

try to add priority high in your FCM/GCM payload like this and send the body or title in the "data" not in the "notification". { priority: "high", data: { "whateverKe"y: "whateverValue", } }

or like here in the issue https://github.com/invertase/notifee/issues/813

const channelId = await notifee.createChannel({
                id: this.LOCAL_MESSAGE_CHANNEL_ID,
                name: this.LOCAL_MESSAGE_CHANNEL_NAME,
                bypassDnd: true,
                sound: "voyager",
                importance: AndroidImportance.HIGH,
                visibility: AndroidVisibility.PUBLIC,
            });
            await notifee.displayNotification({
                id: notificationIds.new_message,
                title: "YOU HAVE A NEW MESSAGE",
                body: "XxxxxxxX",
                android: {
                    channelId,
                },
                ...
            });

This is my current code for wakeup notifications, I'll open a scheduling job when the app starts, and every once in a while, create a local notification. But on Android 13, when the app is placed in the background, this notification doesn't show up immediately until I switch the app to the foreground. The same code works fine on Android 12.

ennbou commented 1 year ago

I have encountered the same issue on the latest Android versions (13 and 14) with FCM Data messages type.

that problem was like that when your app is on FG the notification displayed normally as expected also on Killed mode, but on BG mode the OS keep the notification until the app backs to the FG and display it.

I have tried that :

So fore Android version <=11 (API level <=30) the notifications of Data message type work fine on all modes (FG, BG and Killed mode)

But for Android version >=12(API level >= 31) the notifications of Data message type do not work on BG mode, I think it's Notifee issue related to how they are implementing NotificationManagerCompat and NotificationManager. for more info.

karthikeyancontus commented 1 year ago

@ennbou me too getting same problem kindly let me know if found any solution from the core team

shahrazeahmad07 commented 1 year ago

I also faced the same issue, I even disabled battery optimization for the app, but the notifications still not come. I also have noticed one more thing, if there is a notification (generated by the app with notifee) which you have not removed, and is still in the notification menu, and you kill the app and lock your phone for some time. When you unlock you phone again after some time, the notification would have been automatically dismissed from the notification menu.

Kusu5Dec commented 1 year ago

any solution ?

shahrazeahmad07 commented 1 year ago

I Left React Native Development😂

ennbou commented 1 year ago

@shahrazeahmad07 the same things here 😂

github-actions[bot] commented 11 months ago

Hello 👋, to help manage issues we automatically close stale issues.

This issue has been automatically marked as stale because it has not had activity for quite some time.Has this issue been fixed, or does it still require attention?

This issue will be closed in 15 days if no further activity occurs.

Thank you for your contributions.

MianIbrarHussain commented 10 months ago

I have encountered kind of same issue the difference is my notifications work properly in debug app but as i create assemble Release i dont get any notification when app is terminated... also even in debug app when i click on notification it does not open app... Anybody got any solution...?!?!?! @ennbou

ennbou commented 10 months ago

for the click, try this :

import notifee, { EventType } from '@notifee/react-native';

notifee.onBackgroundEvent(async ({ type, detail }) => {
  const { notification } = detail;
  if (type === EventType.PRESS) {
    // what you want to do
  }
});
sungyupjoo commented 9 months ago

I was suffering from the same issue, I just fixed this by implementing react-native-notifications API(https://github.com/wix/react-native-notifications). I am developing with react-native and fcm to send/receive push notifications and notifee API to display notifications.

Notifee perfectly worked when the app is in foreground, but although using messaging().setBackgroundMessageHandler(async ...), notifee just couldn't display/notify the data-only messages. The aforementioned react-native-notifications API helpmed me to solve this, I suggest you try it!

jasim678 commented 9 months ago

@shahrazeahmad07 @ennbou I Left React Native Development😂 , the same things here 😂 you guys are joking right....?

pep108 commented 9 months ago

I was suffering from the same issue, I just fixed this by implementing react-native-notifications API(https://github.com/wix/react-native-notifications).

@sungyupjoo Can you confirm that scheduling a local notification with react-native-notifications for a future time will work even if the app has been in a killed state for a number of hours.

I'm running into this limitation with notifee and am already using react-native-notifications for remote notification handling with success.

remberluyckx commented 9 months ago

Having the same issue with notifee, anyone has a solution? Using the local notifications of react-native-notifications works for iOS only, they haven't implemented it yet for Android

sungyupjoo commented 9 months ago

Having the same issue with notifee, anyone has a solution? Using the local notifications of react-native-notifications works for iOS only, they haven't implemented it yet for Android

As far as I know, at this point, the local notifications with data-only message won't show if the app is in background and the recipient cannot manage the local notification by onMessage in this case(I think this is the sad but true thing for all of us to accept). So the inevitable conclusion is that we have to use notification message instead of data-only message to make the recipient get push notifications when their app is in background state.

Given this conclusion, I could get a work-around solution in my case. I don't believe my following case will be saving every other dev's case because we all came here for the same reason but from different circumstances, nevertheless, I'll share my case for some who might need:

In my case, I was making a chatting function in my app and I wanted the users to choose either their phones to get alarm from new chat or not. I first tried the local notification and wanted to recipients side to either show message or not, but this clearly didn't work when the app is in background state. So I had to make the sender's side to choose from either sending notification message or data-only message, by reading if the recipient wants to get alarm or not and when the recipient wants get alarm, send notification message and if not, vice versa.

Hope everyone gets their best.

vaniyokk commented 4 months ago

This issue is very disappointing, especially in case when you don't control the sending server. https://github.com/WalletConnect/walletconnect-monorepo/issues/5181

LeCiel commented 3 months ago

Not sure you're still in the problem, but I think I figured out why it did not work - "notifee.createChannel()" is pending during the app is background. I guess it needs to be run on the foreground to create a channel. It will display notification as expected if you get and pass channelId in the different way.

I'm having the same problem on my Android phone, I only get notifications when I switch the app to the foreground, which seems very weird!

try to add priority high in your FCM/GCM payload like this and send the body or title in the "data" not in the "notification". { priority: "high", data: { "whateverKe"y: "whateverValue", } } or like here in the issue https://github.com/invertase/notifee/issues/813

const channelId = await notifee.createChannel({
                id: this.LOCAL_MESSAGE_CHANNEL_ID,
                name: this.LOCAL_MESSAGE_CHANNEL_NAME,
                bypassDnd: true,
                sound: "voyager",
                importance: AndroidImportance.HIGH,
                visibility: AndroidVisibility.PUBLIC,
            });
            await notifee.displayNotification({
                id: notificationIds.new_message,
                title: "YOU HAVE A NEW MESSAGE",
                body: "XxxxxxxX",
                android: {
                    channelId,
                },
                ...
            });

This is my current code for wakeup notifications, I'll open a scheduling job when the app starts, and every once in a while, create a local notification. But on Android 13, when the app is placed in the background, this notification doesn't show up immediately until I switch the app to the foreground. The same code works fine on Android 12.

RoyChuang commented 2 months ago

I enabled the 'display over other apps' setting on my phone. When a local notification is sent, it brings the app to the foreground. This is the only solution I have for now.

pep108 commented 2 months ago

I enabled the 'display over other apps' setting on my phone. When a local notification is sent, it brings the app to the foreground. This is the only solution I have for now.

that's hardly a solution. you can't depend on users to change settings on their phone

abhayciitizen commented 1 month ago

any update on this ?