b8ne / react-native-pusher-push-notifications

Manage pusher interest subscriptions and notification events in Javascript.
MIT License
96 stars 85 forks source link

'handleNotification' not working from killed app state #97

Open hypnocill opened 2 years ago

hypnocill commented 2 years ago

When I receive a notification on Android, 'handleNotification' is called when app is in foreground and in background but when the app is killed, 'handleNotification' is never called.

I have a SplashScreen and I followed the README. The intent extras seem to be passed correctly. This is the SplashScreeActivity where I log "Log.d("intent URI", intent.toUri(0));' and the output contains the notification data I sent. Yet the 'handleNotification' is never called. Screenshot 2022-06-21 at 15 57 27

Update: I removed the Splash Screen and it still doesn't work

Also, when (if) this works, how to differentiate between when this function is called (if app was killed or in foreground), relates to: #87

Please, @Humni let me know if you have any idea how to get this to work since it's important to get the data from the notification in order to navigate in the app based on that.

Update: Calling .setInstanceId() at a very early stage of the app (first thing in App.tsx) seems to fix that for Android. For iOS, the issue remains

Thank you!

leonardo409188 commented 2 years ago

For me too, I'm using Expo bare workflow and when I get the notification with the app killed, it just opens the App and doesn't call the RNPusherPushNotifications.on function.

hypnocill commented 2 years ago

@leonardo409188 Ah, I see. As mentioned, I logged the notification data and it's present when the app is opened from killed state. When I have enough time, I'll try to debug it further and see if I can understand why the notificationEvent is not emitted from native to js.

Meanwhile, any clues will be helpful. Thank you @codynguyen @b8ne

hypnocill commented 2 years ago

@leonardo409188 I found what the problem was, seems like a race condition. The 'notification' event from native when opening the app from killed state via notification was fired before the 'notification' event listener was added in JS.

I had to add directly DeviceEventEmitter.addListener("notification", callback) for Android only at very early stage in my app to catch the notification, then add a timeout to make sure my app is properly loaded, then emit a custom event for this particular case like DeviceEventEmitter.emit('android_notification') and for Android, in react-native-pusher-push-notifications JS 'notification' listener, for Android I'd only listen to iOS.

Seems like a nasty workaround though and I'd be happy to have a better way of dealing with this.

leonardo409188 commented 2 years ago

@hypnocill cool, can you show me how you did it? because i don't understand much of native code =/

hypnocill commented 2 years ago

@leonardo409188 The last thing I did was calling just .setInstanceId and then on('notification', ...) listener as early as possible -first thing in App.js and it seems to catch the notification from killed state for Android.

leonardo409188 commented 2 years ago

@hypnocill it worked for me, before i was using it inside a stack navigation, now i put it on the splash screen, thanks.

p4ever commented 1 year ago

On iOS the problem seems to persist even putting the listener as the first instruction in the App.js