ios - 应用程序在后台时的远程推送通知 swift 3

标签 ios iphone swift push-notification apple-push-notifications

我有一个应用程序可以接收远程推送通知。 我以这种方式实现了 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/

相关文章:

iphone - 为什么 UIWebView 实例不调用 scrollViewDidScroll?

swift - ARKit 的图像识别是否可以对同一张图像进行两次或更多次检测?

php - 在 JS 网页中模拟/模拟 iPhone Sprinboard 行为

iphone - 将签名的应用程序部署到越狱 iPhone

ios - RxSwift : Receive events immediately, 除非最后一个事件在某个时间间隔内被处理

ios - 如何在 Swift 中获取 NSMutableArray 中元素的索引?

ios - setImageWith Url 完成 block swift ios

ios - 在 AVAudioSession 中使用蓝牙播放

ios - 核心数据 : Memory not released after manually faulting managed objects

ios - 如果没有图像,更改行单元格高度