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

'handleNotification' not working from killed app state

Open hypnocill opened this issue 3 years ago • 7 comments
trafficstars

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!

hypnocill avatar Jun 21 '22 13:06 hypnocill

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.

leonardo409188 avatar Jun 22 '22 15:06 leonardo409188

@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 avatar Jun 22 '22 16:06 hypnocill

@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.

hypnocill avatar Jun 25 '22 08:06 hypnocill

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

leonardo409188 avatar Jun 27 '22 18:06 leonardo409188

@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.

hypnocill avatar Jun 27 '22 22:06 hypnocill

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

leonardo409188 avatar Jun 28 '22 21:06 leonardo409188

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

p4ever avatar Jun 14 '23 12:06 p4ever