当收到远程推送通知时,我需要从有效负载中捕获数据,在 IOS 9 上我使用 func 做到了这一点: didReceiveRemoteNotification 在使用 swift 3.0 的 IOS 10 上,我已经实现了这两个功能。
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void)
{
print("\(notification.request.content.userInfo)")
completionHandler([.alert, .badge, .sound])
}
@available(iOS 10.0, *)
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
print("User Info = ",response.notification.request.content.userInfo)
completionHandler()
}
第二个函数仅在用户触摸推送时执行
当我的应用程序处于后台甚至关闭时收到推送通知时,我需要捕获数据。
问候。 问候
最佳答案
当收到推送通知并且您的应用未运行时,您需要在 didFinishLaunchingWithOptions 中实现您的通知逻辑:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Called when a notification is tapped and the app wasn't running, not in foreground neither in background.
if let userInfo = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? [String: AnyObject]{
// your logic here!
}
}
关于ios - 当我的应用程序处于后台或关闭状态时,远程推送通知到达时从负载获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39899892/