【问题标题】:remote push Notification when app is in background swift 3应用程序在后台时的远程推送通知 swift 3
【发布时间】:2016-11-07 22:18:07
【问题描述】:

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

              }
           }

在这两种情况下(当应用程序处于前台和后台时),我必须显示将类似子项添加到根视图(标签栏控制器)的 webview,但如果应用程序处于前台,那么我必须在之前显示警报视图。 我的问题是,如果应用程序在前台我没有问题,但如果应用程序在后台didReceiveRemoteNotification 不会调用(我没有看到打印“推送通知即将到来”)而且我不明白为什么.

你能帮帮我吗?

注意测试我使用 APN Tester(https://itunes.apple.com/it/app/apn-tester-free/id626590577?mt=12) 发送推送通知

【问题讨论】:

  • 您是否在应用的后台模式功能中指定了“远程通知”?此外,当您的应用处于后台时,您无法显示 Web 视图或以其他方式强制您的应用进入前台。
  • 推送有两种类型,一种是一直发送给用户,一种是在用户看不到的情况下发送给应用程序。如果您的应用程序在后台,您可以使用第二种类型的推送。但是,正如 Paulw11 指出的那样,鉴于您对为什么要使用它的解释,这是没有意义的,因为您无法强制后台应用程序进入前台,即后台应用程序无法显示任何 GUI(除了发布本地通知)。

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


【解决方案1】:

didReceiveRemoteNotification 应在应用活动时使用。

当应用程序处于后台或处于非活动状态时,您可以通过按远程通知上的操作按钮来激活它。在您的应用委托中实现userNotificationCenter(_:didReceive:withCompletionHandler:)

【讨论】:

  • 这是新的 iOS 10 方法,但是 OP 显示的方法虽然在 iOS 10 上已弃用,但对于早期版本的 iOS 是必需的,并且仍然适用于 iOS 10。
【解决方案2】:

在 AppDelegate.swift 中:

 func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    // make your function call
 }

didReceive的意思是:当你的应用程序是后台时,你点击通知didReceive is working

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-16
    • 2015-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-30
    相关资源
    最近更新 更多