Notificare / notificare-sdk-ios

Notificare's iOS SDK
MIT License
6 stars 1 forks source link

🐞: NSInternalInconsistencyException crash when USER_NOTIFICATION_CENTER_DELEGATE_ENABLED is False #159

Open mikkoseppa opened 1 week ago

mikkoseppa commented 1 week ago

Is there an existing issue for this?

Describe the bug

We have followed the official instructions for disabling the auto-assigned UNUserNotificationCenterDelegate and implementing our own delegate methods exactly as described in the documentation. This worked perfectly with library v3, but after migrating to v4 the application crashes when tapping a notification when the app is in background.

I can temporarily fix the issue by ensuring that the completion handler is ran in the main thread. So for example instead of implementing the method like this (as instructed in the documentation):

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    Notificare.shared.push().userNotificationCenter(center, willPresent: notification, withCompletionHandler: completionHandler)
  }

I would do this:

  func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
      Notificare.shared.push().userNotificationCenter(center, willPresent: notification) { options in
        DispatchQueue.main.async {
          completionHandler(options)
        }
      }
    }

By quickly looking into the crash and the Notificare codebase, it seems like Notificare library is calling the completion handlers of the UNUserNotificationCenterDelegate methods in a background thread which might cause this crash. I'm not 100% sure about this though. Related code: https://github.com/Notificare/notificare-sdk-ios/blob/main/NotificarePushKit/Sources/Internals/NotificarePushImpl%2BUNUserNotificationCenterDelegate.swift#L16 https://github.com/Notificare/notificare-sdk-ios/blob/main/NotificarePushKit/Sources/Internals/NotificarePushImpl%2BUNUserNotificationCenterDelegate.swift#L27

Sentry screenshot: screenshot 2024-11-27 kello 23 20 40

The reason why we set USER_NOTIFICATION_CENTER_DELEGATE_ENABLED to False is because we need additional logic within the delegate methods, so it's absolutely must have for us to get this fixed.

Steps to reproduce

  1. Set USER_NOTIFICATION_CENTER_DELEGATE_ENABLED to false in NotificareOptions
  2. Implement UNUserNotificationCenterDelegate functions according to your documentation
  3. Send a push notification when the app is in background
  4. Tap the notification --> Application crashes

Expected behaviour

App does not crash when tapping a notification.

Relevant log output

No response

Library version

4.0

Operating system

iOS 18

Smartphone model

No response

Additional context

No response

hpinhal commented 6 days ago

Hi @mikkoseppa ,

Thanks for the detailed report. I'll request that the engineering team take a look at the issue.

For the time being, you can work around the issue like you described.