我有一个应用程序可以接收远程推送通知。
我以这种方式实现了 didReceiveRemoteNotification
:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
print("PUSH NOTIFICATION is coming")
let state: UIApplicationState = UIApplication.shared.applicationState
let inBackground = state == .background
let dizionario = userInfo["aps"]! as! NSDictionary
let alert = dizionario["alert"]! as! String
print(alert)
if(!inBackground){
print("APP IN FOREGROUND")
//show alert view and the if user tap 'ok' show webview
}
else{
print("APP IS BACKGROUND")
//SHOW WEBVIEW
}
}
在这两种情况下(当应用程序处于前台和后台时)我必须显示像 subview 一样添加到 Root View (tabbar Controller )的 webview,但是如果应用程序处于前台,那么我必须在之前显示一个警报 View 。
我的问题是,如果应用程序在前台我没有问题,但如果应用程序在后台 didReceiveRemoteNotification
不调用(我没有看到打印“PUSH NOTIFICATION is coming”)并且我不明白为什么。
你能帮帮我吗?
N.B 用于测试我使用 APN 测试器(https://itunes.apple.com/it/app/apn-tester-free/id626590577?mt=12)发送推送通知
最佳答案
didReceiveRemoteNotification
旨在在应用事件 时使用。
当应用程序处于后台或处于非事件状态时,您可以通过按远程通知上的操作按钮来激活它。实现 userNotificationCenter(_:didReceive:withCompletionHandler:)在您的应用委托(delegate)中。
关于ios - 应用程序在后台时的远程推送通知 swift 3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40475694/