mirzemehdi / KMPNotifier

Kotlin Multiplatform Push Notification Library targetting android, iOS, Desktop and Web (JS and Wasm)
http://mirzemehdi.com/KMPNotifier/
Apache License 2.0
343 stars 23 forks source link

Notification Click not working when iOS app is closed #84

Open sereden opened 1 month ago

sereden commented 1 month ago

KMPNotifier and Kotlin version: kmpnotifier version: 1.2.1, kotlin version: 2.0.20, iOS 18 (device and simulator)

Hi! I have the following setup:

import SwiftUI
import Foundation
import FirebaseCore
import FirebaseCrashlytics
import FirebaseMessaging
import ComposeApp

class AppDelegate: NSObject, UIApplicationDelegate {
    func application(_ application: UIApplication,
                   didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        FirebaseApp.configure()
        NSLog("initialize")
        NotifierManager.shared.initialize(configuration: NotificationPlatformConfigurationIos(showPushNotification: true, askNotificationPermissionOnStart: true, notificationSoundName: nil))

        if let infoDictionary = Bundle.main.infoDictionary {
            if let buildType = infoDictionary["BuildType"] as? String{
                Crashlytics.crashlytics().setCrashlyticsCollectionEnabled(buildType == "release")
            }
        }
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
    }

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) async -> UIBackgroundFetchResult {
        NSLog("onApplicationDidReceiveRemoteNotification")
         NotifierManager.shared.onApplicationDidReceiveRemoteNotification(userInfo: userInfo)
         return UIBackgroundFetchResult.newData
    }
}

onApplicationDidReceiveRemoteNotification is not called when the app was closed. However, initialize is always called. Also, added the following capabilities: image

Any ideas, on what could be an issue of the onNotificationClicked is not invoked?

mirzemehdi commented 1 month ago

hello @sereden. After you clicking the notifcation, you don't see "onApplicationDidReceiveRemoteNotification" in the logs?

Not sure exactly, why this would happen. But from stackoverflow that's what I found:

https://stackoverflow.com/questions/59876964/didreceiveremotenotification-not-called-in-ios-13-3-when-app-is-in-background

mirzemehdi commented 1 month ago

Also do you send notification from firebase interface, or using an API?

sereden commented 1 month ago

@mirzemehdi Thank you for the response!

I saw a stackoverflow thread, but the issue occurs for the release build across the different devices. The push messages are sent via API

sereden commented 3 days ago

I've found 3 cases when it could happen:

  1. When the NotifierManager.addListener is not invoked in AppDelegate. In our case, it was done in DI, initialized with ComposeView creation.
  2. When you disable askNotificationPermissionOnStart to shift it to the latter stage. The workaround is the following:
    UNUserNotificationCenter.currentNotificationCenter()
    .getNotificationSettingsWithCompletionHandler { settings ->
                val notificationsAllowed = settings?.authorizationStatus == UNAuthorizationStatusAuthorized
                NotifierManager.initialize(
                    // askNotificationPermissionOnStart = true breaks opening a notification from the background.
                    // But it should be disabled to shift a request to the dashboard screen
                    // This is a workaround that handles both cases
                    configuration = NotificationPlatformConfiguration.Ios(
                        askNotificationPermissionOnStart = notificationsAllowed
                    )
                )
            }
  3. When you invoke network requests when push arrives (e.g. to refresh or fetch some data associated with push). I haven't found a solution yet.
mirzemehdi commented 1 day ago

Thank you @sereden for providing these cases. Will check it out

sereden commented 5 hours ago

Thank you, @mirzemehdi . Regarding 3rd case, I haven't found the root cause, but the side effect is that in my case userNofiicationCenter(didReceiveNotificationResponse: UNNotificationResponse) was not invoked. However, the KMPNofifier SDK has been initialized. The workaround was to fork a repo, make those methods public, and implement delegate on the iOS level.

NotifierManager.onUserNotification(notificationContent)
NotifierManager.onNotificationClicked(notificationContent)
if (NotifierManager.shouldShowNotification(notificationContent)) withCompletionHandler()

I still assume that exists some initializing issues from my side (maybe due to workaround with requesting notification permission).