我已在我的应用中实施了远程通知,但我遇到了didReceiveRemoteNotification:fetchCompletionHandler:
的问题。
当手机的屏幕被锁定时,我收到通知。如果用户滑动通知,应用程序将返回到前台并调用didReceiveRemoteNotification:fetchCompletionHandler:
。
但如果应用程序在前台运行,则永远不会调用didReceiveRemoteNotification:fetchCompletionHandler:
。可能是什么原因?
我正在使用iOS 10.3.2
答案 0 :(得分:1)
您正在使用哪个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 并进一步检查。