havesource / cordova-plugin-push

Register and receive push notifications
MIT License
150 stars 283 forks source link

iOS notification crashes when its opened #94

Open pablop94 opened 3 years ago

pablop94 commented 3 years ago

Hi, how are you?

I'm experiencing a weird issue in iOS build. When the user receives a notification an taps on it, the app is opened and crashes. I'm not able to reproduce the issue in the simulator, notifications are working as expected.

Expected Behaviour

The app is opened

Actual Behaviour

The app crashes.

Platform and Version (eg. Android 5.0 or iOS 9.2.1)

iPhone12,8

cordova info Printout

Ionic:

Ionic CLI : 6.12.2 (/usr/local/lib/node_modules/@ionic/cli) Ionic Framework : @ionic/angular 5.6.4 @angular-devkit/build-angular : 0.901.15 @angular-devkit/schematics : 9.1.15 @angular/cli : 9.1.15 @ionic/angular-toolkit : 2.3.3

Cordova:

Cordova CLI : 10.0.0 Cordova Platforms : 5.0.4, android 9.1.0, browser, ios 6.1.1 Cordova Plugins : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0, (and 10 other plugins)

Utility:

cordova-res (update available: 0.15.3) : 0.15.1 native-run : 1.3.0

System:

ios-deploy : 1.10.0 ios-sim : 8.0.2 NodeJS : v12.13.0 (/usr/local/bin/node) npm : 6.12.0 OS : macOS Catalina Xcode : Xcode 12.4 Build version 12D4e

Logs taken while reproducing problem

I see this crashlog in TestFlight, attached. crashlog.txt

Let me know if you have any doubts

cyptus commented 2 years ago

same issue here with

Application Specific Information:
BUG IN CLIENT OF LIBDISPATCH: Unbalanced call to dispatch_group_leave()

starting with iOS 14. could you solve this @pablop94 ?

Thread 0 name:
Thread 0 Crashed:
0   libdispatch.dylib               0x00000001aa9807d8 dispatch_group_leave.cold.1 + 36 (semaphore.c:0)
1   libdispatch.dylib               0x00000001aa94dc00 dispatch_group_leave + 140 (semaphore.c:0)
2   libdispatch.dylib               0x00000001aa94b24c _dispatch_call_block_and_release + 32 (init.c:1454)
3   libdispatch.dylib               0x00000001aa94cdb0 _dispatch_client_callout + 20 (object.m:559)
4   libdispatch.dylib               0x00000001aa95a7ac _dispatch_main_queue_callback_4CF + 836 (inline_internal.h:2548)
5   CoreFoundation                  0x00000001aacd411c __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 16 (CFRunLoop.c:1790)
6   CoreFoundation                  0x00000001aacce120 __CFRunLoopRun + 2508 (CFRunLoop.c:3118)
7   CoreFoundation                  0x00000001aaccd21c CFRunLoopRunSpecific + 600 (CFRunLoop.c:3242)
8   GraphicsServices                0x00000001c27d1784 GSEventRunModal + 164 (GSEvent.c:2259)
9   UIKitCore                       0x00000001ad70bfe0 -[UIApplication _run] + 1072 (UIApplication.m:3253)
10  UIKitCore                       0x00000001ad711854 UIApplicationMain + 168 (UIApplication.m:4707)
11  Test PSA en Acción             0x00000001005a49c0 main + 56 (main.m:32)
12  libdyld.dylib                   0x00000001aa98d6b0 start + 4
cyptus commented 2 years ago

the code fails here after user clicks onto the push notification in iOS notification center: https://github.com/havesource/cordova-plugin-push/blob/2ba3a882f78a3daf635202cba4a9e5d6d8ffeaf9/src/ios/AppDelegate%2Bnotification.m#L103

image

log:

Push Plugin key: content-available
Push Plugin key: alert
Push Plugin key: category
Push Plugin key: sound
Push Plugin register success: {length = 32, bytes = ... }
Push Plugin finish called
Push Plugin stopBackgroundTask called
Push Plugin handlerObj
Push Plugin: stopBackgroundTask (remaining t: ...)

it seems like this handler is called multiple times, which leads into this error on the second time.

@erisu could you have a look into this?

jhlee8804 commented 2 years ago

@cyptus Don't set the "content-available" field to fcm payload unless you absolutely need background notifications. It seems to be called handler multiple times.

When set the "content-available" field: https://github.com/havesource/cordova-plugin-push/blob/master/src/ios/AppDelegate%2Bnotification.m#L99 https://github.com/havesource/cordova-plugin-push/blob/master/src/ios/AppDelegate%2Bnotification.m#L103

Call PushNotification.finish() https://github.com/havesource/cordova-plugin-push/blob/master/src/ios/PushPlugin.m#L583 https://github.com/havesource/cordova-plugin-push/blob/master/src/ios/PushPlugin.m#L604

igorsantos07 commented 1 year ago

Same issue here, causing serious disturbance on our app since background notifications are mandatory. Sad to see such an important plugin abandoned (instead of being part of the core of Cordova).

igorsantos07 commented 1 year ago

I hacked a dirty workaround for the issue: it counts how many times the completion handler was called, avoiding calling it multiple times. I'm not opening a PR because this is not a solution, and it potentially have two drawbacks:

  1. if you repeat the same notification body, it will probably have the same notId (it had in my test cases), and thus it will fail to call the completion handler, causing a warning. However, I guess that's better than a full application crash lol
  2. not sure if that could cause a memory leak or stuff like that... also, not sure if this arbitrarily long NSMutableDictionary could grow terribly large and cause issues; I hope your app doesn't get too many notifications in its lifetime

How to use it: I have no clue how to do it cleanly nowadays. My cordova plugin add randomly fails at me, and my best resort was to entirely re-clone our repo, manually change the plugin's version at package.json to github:igorsantos07/cordova-plugin-push#patch-1, then cordova add platform ios and run the application 🤷‍♂️ I also had some trouble with the compilation because, with the multiple reinstallation tries of the main plugin/fork, it included a new config key IOS_FIREBASE_MESSAGING_VERSION pointing to 6.32.2, which broke compilation.... But further tries moved that to 8.1.1 (no clue why/how), which seems to work (we didn't have this key before and it worked great, go wonder).