问题描述
我有一个接收远程推送通知的应用程序.我以这种方式实现了 didReceiveRemoteNotification
:
I have an app that receives remote push notification.I have implemented didReceiveRemoteNotification
in this way:
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
}
}
在这两种情况下(当应用程序处于前台和后台时)我必须显示 webview 将子视图添加到根视图(tabbar 控制器)但如果应用程序在前台那么我必须在之前显示警报视图.我的问题是,如果应用程序在前台我没有问题,但如果应用程序在后台 didReceiveRemoteNotification
不会调用(我没有看到打印PUSH NOTIFICATION iscoming")并且我不明白为什么.
In both cases(when app is in foreground and background) I have to show webview that add like child to root view(tabbar controller) but if app is in foreground then I have to show , before , an alert view.My problem is that if app is in foreground I haven't problems , but if app is in background didReceiveRemoteNotification
doesn't call(I don't see the print "PUSH NOTIFICATION is coming" ) and I don't understand why.
你能帮我吗?
NB 测试我使用 APN Tester(https://itunes.apple.com/it/app/apn-tester-free/id626590577?mt=12) 用于发送推送通知
N.B for testing I use APN Tester(https://itunes.apple.com/it/app/apn-tester-free/id626590577?mt=12) for send push notification
推荐答案
didReceiveRemoteNotification
旨在在应用活动时使用.
当应用处于后台或非活动状态时,您可以通过按远程通知上的操作按钮来激活它.在您的应用委托中实施 userNotificationCenter(_:didReceive:withCompletionHandler:).
When the app is in the background or inactive, you can activate it by pressing the action button on the remote notification. Implement userNotificationCenter(_:didReceive:withCompletionHandler:) in your app delegate.
这篇关于应用程序在后台 swift 3 时的远程推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!