ios - 当我的应用程序处于后台或关闭状态时,远程推送通知到达时从负载获取数据

标签 ios swift swift3

当收到远程推送通知时,我需要从有效负载中捕获数据,在 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/

相关文章:

ios - 您如何以 YYYY-MM-DD 格式快速获取上周日期?

ios - NSString 删除重复的子串

Swift 嵌套泛型类型不符合协议(protocol)

macos - 在 Swift 中运行 shell 命令

ios - 应用内购买 : dynamically add non consumable items

ios - 如何使用 Swift 创建 "hyperlink"?

swift - 在后台访问 healthkit 锻炼

ios - Swift 3 解析 youtube 视频不显示结果并不断加载 - IOS 10

ios - 在 UITextField swift 3.0 中使用数字键盘以 dd-MM-yyyy 格式输入日期

swift - 字符串插值和字符串连接之间的区别