【问题标题】:App is not launching when push notification is tapped on iOS, Swift在 iOS、Swift 上点击推送通知时应用程序未启动
【发布时间】:2022-01-15 21:45:53
【问题描述】:

我现在使用 FirebaseNotifications 在我的应用中集成了推送通知。为此,我使用 NotificationServiceExtension 进行处理(CoreData,...)和常用方法,例如 AppDelegate 中的 didReceiveRemoteNotification、willPresent 或 didReceive。

内容的处理和推送通知的显示在应用程序的所有状态下都能完美运行 - 在后台和前台。

如果应用程序在前台或后台,但没有被杀死,点击推送通知也可以,并且调用 didReceive 方法,我可以导航到所需的内容。

如果应用程序被完全终止,几秒钟后点击推送通知会启动应用程序,但不会调用通常的启动屏幕(启动屏幕)并且不会发生其他任何事情 - 应用程序保持在 白色屏幕

起初我怀疑我忘记调用completionHandler,但它们都被存储了,我没有在任何地方发现错误。

有人可以帮我吗,在哪里可能仍然存在启动屏幕,然后是 AppDelegate 和我的普通 ViewController 被调用的问题。

谢谢 - 这是我在 AppDelegate 中的代码。不幸的是,我没有日志,因为该应用程序必须被完全杀死,因此我在 Xcode 控制台中没有输出。

func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any],
                 fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    completionHandler(UIBackgroundFetchResult.newData)
}

func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([[.alert, .badge, .sound]])
}

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    
    defer {
        completionHandler()
    }
    
    if response.actionIdentifier == "friends" {
        print("Open button was pressed.")
        
        // TODO: - Deep link to friends
    }
    completionHandler()
}

【问题讨论】:

    标签: ios swift firebase push-notification notifications


    【解决方案1】:

    要解决这个问题,您需要在 didFinishLaunchingWithOptions 中添加功能

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    
        if let launchOptions = launchOptions {
            if let info = launchOptions[UIApplication.LaunchOptionsKey.remoteNotification] {
                let dic = info as! NSDictionary
                print("dic from closed app",dic)
                PushNotificationResponse(Data: dic)
            }
        }
    
        return true
    
    }
    

    didReceiveRemoteNotification 有以下代码。

    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
        // Print notification payload data
        print("Push notification received: \(data)==\(application.applicationState)")
        
        switch application.applicationState {
            case .active:
                print("active mode")
                // PushNotificationResponse(Data:data as NSDictionary)
                break
            case .inactive:
                print("inactive mode")
                PushNotificationResponse(Data:data as NSDictionary)
                break
            case .background:
                print("background mode")
                PushNotificationResponse(Data:data as NSDictionary)
                break
            default:
                break
        }
    }
    

    这是您在一个地方处理推送点击事件的地方。

    func PushNotificationResponse(Data:NSDictionary){
        print("PushNotificationResponse\(Data)")
        // handle push code here
    }
    

    【讨论】:

    • 感谢您的代码和处理水龙头的可能性。不幸的是,我的问题是,当点击通知并且应用程序被完全杀死时,点击后什么也没有发生 - 只有一个白屏。
    • @froeschlb : 添加 UIAlertView 以进行测试并打印您获得的数据并查看您在 uialertview 中获得的内容以进行测试...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多