Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notification Click not working when iOS app is closed #84

Open
sereden opened this issue Oct 11, 2024 · 6 comments
Open

Notification Click not working when iOS app is closed #84

sereden opened this issue Oct 11, 2024 · 6 comments

Comments

@sereden
Copy link

sereden commented Oct 11, 2024

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
Copy link
Owner

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
Copy link
Owner

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

@sereden
Copy link
Author

sereden commented Oct 14, 2024

@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
Copy link
Author

sereden commented Nov 25, 2024

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
                    )
                )
            }
  1. 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
Copy link
Owner

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

@sereden
Copy link
Author

sereden commented Nov 29, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants