我需要在重新启动远程推送通知时从有效负载中捕获数据,在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()
}
第二个功能仅在用户触摸推送时执行
当我的应用处于后台甚至关闭时,我需要在推送通知到达时捕获数据。
的问候。 问候
答案 0 :(得分:2)
当收到推送通知且您的应用未运行时,您需要在didFinishLaunchingWithOptions中实现您的通知逻辑:
{{1}}
答案 1 :(得分:1)
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
// Capture payload here something like:
let appState = userInfo["appState"] as? String
print(appState!)
// Do something for every state
if (application.applicationState == UIApplicationState.active){
print("Active")
}else if (application.applicationState == UIApplicationState.background){
print("Background")
}else if (application.applicationState == UIApplicationState.inactive){
print("Inactive")
}
completionHandler(.noData)
}
答案 2 :(得分:0)
尝试使用iOS10中的didReceiveRemoteNotification函数:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any]) {
print(userInfo)
}