react-native-push-notification / ios

React Native Push Notification API for iOS.
MIT License
734 stars 282 forks source link

localNotification handler does not complete when woken from background #297

Open edwinckc opened 3 years ago

edwinckc commented 3 years ago

When a notification is received, didReceiveRemoteNotification is called with a completion handler, which is then passed to RN. However, if the user performs an action on the notification, that is handled by didReceiveNotificationResponse, which does not have a completion handler.

The RN listener for localNotification ends up being called, but the device does not wait for it to complete. This is okay when the app is in the foreground. If the app is in the background, the app gets woken up and the RN listener is started, but the RN handler does not complete before app goes to sleep again.

braandl commented 3 years ago

Not very nice, but without changing the lib, you can use this little 'hack' to make it work:

https://gist.github.com/braandl/306640e2aa98392df5b654b2b35facad

best

edwinckc commented 3 years ago

Thanks @braandl, really appreciate the help! It looks promising debugging in the simulator, but only seems to work on the second try because the first time it wakes up it's loading a new bundle from Metro. Facing some other issues with release build, will report back once I'm able to test it out on release. At the very least the initial notifications come through on cold start now thanks to your code 💯

braandl commented 3 years ago

On a real device, this works for me also after re-bundling the app. From what I saw in the debugger, the native modules get invoked just normally and then the JS execution is paused until bundling is complete. Therefore I can then fetch the registered notification. But I never tested in any simulator.

anishtr4 commented 3 years ago

@braandl @edwinckc Does this work for both local notification and remote notification?? Is this the fix for getInitialNotification return null always when app launch from Coldboot?

I have tried the above gist and the app crashes with a log"libc++abi.dylib: terminating with uncaught exception of type NSException". I am no a native guy it will be great if you can help me out with this.

Appdelegate.m: https://gist.github.com/anishtr4/1d9c6aea15190aeb8a7f852f5c747be8 Appdelegate.h:https://gist.github.com/anishtr4/551d1380fcfaf9d08b854bac960e16a0

Error log:https://gist.github.com/anishtr4/8c87afbaff8ea7f5c5f917eb3af99a7d

braandl commented 2 years ago

I am sorry, I do not have cloud messaging in my App, so I can only speak for local notifications.

But according to Apples documentation (https://developer.apple.com/documentation/usernotificationsui/unnotificationcontentextension/1845197-didreceivenotificationresponse) it will work for all of them.