[英]iOS push notifications - didReceiveRemoteNotification:fetchCompletionHandler: never called in foreground
I have implemented remote notifications in my app, however I have problems with didReceiveRemoteNotification:fetchCompletionHandler:
. 我已经在我的应用中实现了远程通知,但是didReceiveRemoteNotification:fetchCompletionHandler:
遇到了问题。
When the phone's screen is locked, I am getting the notification. 手机屏幕锁定后,我会收到通知。 If the user swipes the notification, app comes back to foreground and didReceiveRemoteNotification:fetchCompletionHandler:
is called. 如果用户滑动通知,应用程序将返回到前台并调用didReceiveRemoteNotification:fetchCompletionHandler:
But if the app is running in the foreground, didReceiveRemoteNotification:fetchCompletionHandler:
is never called. 但是,如果应用程序在前台运行,则永远不会调用didReceiveRemoteNotification:fetchCompletionHandler:
What might be the reason? 可能是什么原因?
I'm working on iOS 10.3.2 我正在使用iOS 10.3.2
Which iOS version you are working on ? 您正在使用哪个iOS版本?
There are different methods for iOS 9 and iOS 10. iOS 9和iOS 10有不同的方法。
Below are the ones for 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) {
}
For iOS 9, make sure to register your notification in didBecomeActive 对于iOS 9,请确保在didBecomeActive中注册您的通知
UIApplication.shared.registerUserNotificationSettings(UIUserNotificationSettings(types: [.sound, .alert, .badge], categories: nil))
UIApplication.shared.registerForRemoteNotifications()
and in your didReceiveRemoteNotification 并在您的didReceiveRemoteNotification中
func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
if application.applicationState == .active {
//foreground state
}
}
Best approach would be to put a debugger in didReceiveRemoteNotification and check further. 最好的方法是将调试器放入didReceiveRemoteNotification中,然后进行进一步检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.