EddyVerbruggen / nativescript-plugin-firebase

:fire: NativeScript plugin for Firebase
https://firebase.google.com
MIT License
1.01k stars 448 forks source link

Push notifications crash when app is foreground in iOS #1763

Open JWiseCoder opened 3 years ago

JWiseCoder commented 3 years ago

nativescript/firebase 11.1.3 nativescript 7.1.2 nativescript/core 7.1.3 nativescript/ios 7.1.0

I set up push notifications in my app using firebase/messaging. I set everything up and called registerForPushNotifications, and I got the token back for sending push notifications to the app. However, when I pushed a notification with the app in the foreground, the app crashes with an EXC_BAD_ACCESS exception. (If the app is in the background, the push notification goes through fine).

Digging through it, I found that the source of the problem was in the userNotificationCenterWillPresentNotificationWithCompletionHandler function of the FirebaseNotificationDelegateObserverImpl class. The crash is occurring when the completionHandler is called.

Apparently, this callback comes from the @nativescript/shared-notification-delegate plugin. The userNotificationCenterWillPresentNotificationWithCompletionHandler function inside the UNUserNotificationCenterDelegateImpl class receives the message as the notification delegate, and it creates its own childHandler function that wraps the completionHandler passed in from the native framework. When the native completionHandler is called inside the childHandler, the app crashes.

I think this is due to the native completionHandler being released after the userNotificationCenterWillPresentNotificationWithCompletionHandler returns. The JS object that wraps the completionHandler is still there, but the underlying block is gone. I think this is the source of the EXC_BAD_ACCESS.

I think in order to fix it, the completionHandler somehow needs to be retained in @nativescript/shared-notification-delegate so it does not get released. (I think in Objective-C, completionHandler would need to be copied in order to be retained by another block)

jholmes12 commented 3 years ago

I'm experiencing the same issue with an app that was recently updated to NS7 and the latest nativescript/firebase plugin 11.1.3.

Notifications work great in the background, but foreground crashes app immediately with the exception mentioned above.

jholmes12 commented 3 years ago

Hey @JWiseCoder , you were correct that this had to do with the shared delegate plugin. @edusperoni just pushed a fix for this a few days ago and it resolved the issue for me! Thanks for such a quick fix on that Eduardo!

https://github.com/NativeScript/plugins/pull/83

I needed to specifically install and update to version 1.0.2 of @nativesript/shared-notification-delegate 👍