我已启用后台模式的远程通知。
因此我需要处理一个应用程序在前台并且通知到达的情况。因此,只有当用户点击通知时才应该……
对我来说这个方法之前被称为
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { completionHandler(.newData) }
因为我没有设置代表 的 UNUserNotificationCenter 强> 在AppDelegate中。
UNUserNotificationCenter.current().delegate = self
作为 的 @Dimitris 强> 建议我已添加然后调用
的 willPresent 强> &安培; 的 didReceive 强> APNS委托方法(iOS 10中引入的新委托方法)
因此,现在我能够处理APNS上的用户点击操作,因为点击它会触发didReceive委托方法。
当您收到通知时,您正在调用的方法将被调用,该应用程序将在应用程序为后台或前台时启动。
但是,当用户点击通知时,情况并非如此。 您需要分配一个委托 UNUserNotificationCenter.current().delegate 然后实现这个方法 userNotificationCenter(_:didReceive:withCompletionHandler:)
UNUserNotificationCenter.current().delegate
userNotificationCenter(_:didReceive:withCompletionHandler:)
class NotificationHandler: NSObject, UNUserNotificationCenterDelegate { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { Logger.log("didReceive notification: \(response.notification.request.content.userInfo)") if response.actionIdentifier == UNNotificationDefaultActionIdentifier { let userInfo = response.notification.request.content.userInfo // Process the user info 锟斤拷 } completionHandler() } } // In `application(_ application:,didFinishLaunchingWithOptions:)` // Note this is for clarity - you'd need to keep a strong reference to the NotificationHandler() or will get deallocated immediately. UNUserNotificationCenter.current().delegate = NotificationHandler()