繁体   English   中英

iOS推送通知-didReceiveRemoteNotification:fetchCompletionHandler:从不在前台调用

[英]iOS push notifications - didReceiveRemoteNotification:fetchCompletionHandler: never called in foreground

我已经在我的应用中实现了远程通知,但是didReceiveRemoteNotification:fetchCompletionHandler:遇到了问题。

手机屏幕锁定后,我会收到通知。 如果用户滑动通知,应用程序将返回到前台并调用didReceiveRemoteNotification:fetchCompletionHandler:

但是,如果应用程序在前台运行,则永远不会调用didReceiveRemoteNotification:fetchCompletionHandler: 可能是什么原因?

我正在使用iOS 10.3.2

您正在使用哪个iOS版本?

iOS 9和iOS 10有不同的方法。

以下是iOS 10的

    //Called when a notification is delivered to a foreground app.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {

    }


    //Called to let your app know which action was selected by the user for a given notification.
    @available(iOS 10.0, *)
    func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

    }

对于iOS 9,请确保在didBecomeActive中注册您的通知

UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()

并在您的didReceiveRemoteNotification中

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {

if application.applicationState == .active {
//foreground state
 }

}

最好的方法是将调试器放入didReceiveRemoteNotification中,然后进行进一步检查。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM