react-native-notifications icon indicating copy to clipboard operation
react-native-notifications copied to clipboard

[ANDROID] registerNotificationReceivedBackground is not triggered when the app is killed.

Open Shane-Loong opened this issue 6 years ago • 18 comments

Notifications.events().registerNotificationReceivedBackground() is triggered when the app is in the foreground and in the background but not when the app is killed.

'react-native': 0.61
'react-native-notificiations': 3.1.2

Shane-Loong avatar Feb 20 '20 20:02 Shane-Loong

When App is killed you need to obtain the payload with this:

async componentDidMount() {
    const notification: Notification = await Notifications.getInitialNotification()
    console.log('initial', notification)
  }

siliconsocket avatar Feb 20 '20 20:02 siliconsocket

@siliconsocket Thanks for the quick reply!

How do you trigger the app to wake up and start processing the notification? Adding that line of code does not seem to be doing anything.

From the libary, PushNotification.java I see that onReceived() is triggered when a notification comes in while the app is killed. However, I can't seem to figure out how to get my app to start processing the notification and wake up to display the notification :/

Shane-Loong avatar Feb 20 '20 20:02 Shane-Loong

@siliconsocket how do you trigger the app to wake up and start processing the notification?

I just received the push notification even when app is killed, then tap on it and app starts again and I recover the payload with componentDidMount() method in my main component.

siliconsocket avatar Feb 20 '20 21:02 siliconsocket

I see. I don't receive push notifications when the app is killed, and only when the app is killed.

Shane-Loong avatar Feb 20 '20 21:02 Shane-Loong

When App is killed you need to obtain the payload with this:

async componentDidMount() {
    const notification: Notification = await Notifications.getInitialNotification()
    console.log('initial', notification)
  }

In my case notification object was always thrown undefined... 😢😢😢😢

My Code

useEffect(()=>{
 Notifications.getInitialNotification().then(notification=>{
  console.log(notification);
 }).catch(error=>{
 console.log(error);
});
},[]);

karriz-dev avatar Mar 17 '20 09:03 karriz-dev

When App is killed you need to obtain the payload with this:

async componentDidMount() {
    const notification: Notification = await Notifications.getInitialNotification()
    console.log('initial', notification)
  }

In my case notification object was always thrown undefined... 😢😢😢😢

My Code

useEffect(()=>{
 Notifications.getInitialNotification().then(notification=>{
  console.log(notification);
 }).catch(error=>{
 console.log(error);
});
},[]);

I solved this problem !

don't call getInitialNoitification() function in useEffect()

You must be called to componentDidMount() check up react-native app lifecycle.

karriz-dev avatar Mar 18 '20 02:03 karriz-dev

How did you resolve this issue @ldh1428a ? I have the same, getInitialNotitication always resolves me to undefined.. no matter if i call it from componentDidMount

ferprez avatar Mar 18 '20 23:03 ferprez

How did you resolve this issue @ldh1428a ? I have the same, getInitialNotitication always resolves me to undefined.. no matter if i call it from componentDidMount

I same result undefined if app was been killed..

Application was background state getInitialNotification() return notification data, but app was dead(killed) state always return to undefined... 😢

karriz-dev avatar Mar 20 '20 04:03 karriz-dev

Any solutions ? I always get undefined too from getInitialNotification(); and registerNotificationOpened(),registerNotificationReceivedForeground() did not trigger when it is called I have no idea what to do now. I'm using the latest version.

roshan-p avatar Apr 03 '20 11:04 roshan-p

@alexbuicescu you did great job regarding channels in Android, may be you can help us with this problem? I also can't get something from getInitialNotification() when app in dead(killed) mode.

anisimov74 avatar Apr 24 '20 14:04 anisimov74

Hi, from my knowledge, you can't process normal notifications while the app is killed. For that you need to send silent notifications (or data notifications, they are the same thing). Also, at the moment react-native-notifications does not support this. I use react-native-firebase for this.

Regarding the getInitialNotification, I'm not sure how to debug this issue, but I can look into it. It also bothers me that this is not working.

0x2539 avatar Apr 24 '20 15:04 0x2539

@alexbuicescu thank you for the quick response! Did I understand you right that for the particular case when app is killed/dead you are using react-native-firebase and this library for others?

anisimov74 avatar Apr 24 '20 15:04 anisimov74

Exactly

0x2539 avatar Apr 24 '20 15:04 0x2539

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

stale[bot] avatar May 29 '20 03:05 stale[bot]

The issue has been closed for inactivity.

stale[bot] avatar Jun 05 '20 05:06 stale[bot]

is this issue resolved?

DovletAmanov avatar Oct 12 '20 19:10 DovletAmanov

@alexbuicescu I'm trying to use @react-native-firebase alongside this library to handle notifs when the app is killed (as you mentioned you did). I've noticed that when I install @react-native-firebase it doesn't allow react-native-notifications to handle the notifications, because only one "com.google.firebase.MESSAGING_EVENT" service will get triggered and it essentially stops at the @react-native-firebase project. How did you get around this? Did you have to implement something like this?

ryanmcclure4 avatar Oct 13 '20 23:10 ryanmcclure4

@ryanmcclure4, did you ever find a solution to this problem? I need to be able to have the app launched from a notification in Android too.

jpmunoz9 avatar Oct 13 '21 17:10 jpmunoz9