【问题标题】:iOS 10 push notification when app terminated?应用程序终止时的iOS 10推送通知?
【发布时间】:2016-11-03 10:44:47
【问题描述】:

升级到 iOS 10 后,我遇到了关于推送通知的问题(我使用的是 Swift3)。

在正常情况下,当应用程序打开或应用程序仍在后台时,一切正常(可以接收推送通知并按照我的逻辑更新数据)。

但是当应用程序终止时,我无法在应用程序激活时处理推送通知。

这是我的测试用例。

  1. 编辑方案以等待可执行文件启动。
  2. 双击主页按钮并向上滑动应用程序。
  3. 运行 Xcode 等到显示“等待应用程序启动”。
  4. 测试从服务器发送推送通知。
  5. 设备收到推送通知。
  6. 从应用程序图标启动应用程序。

在该应用程序启动后 didFinishLaunchingWithOptions 被调用但 launchOptions 为空 所以我无法处理推送通知(但如果我从通知中心的通知打开应用程序或弹出通知 launchOptions 不为空)

有人有办法检查这个问题吗?

提前谢谢你。

【问题讨论】:

    标签: apple-push-notifications swift3 ios10


    【解决方案1】:

    您需要通过点击通知托盘中的推送通知来打开应用程序。

    当您从图标启动应用程序时,launchOptions 将为 nil。从推送通知启动将为您提供launchOptions。

    https://developer.apple.com/reference/uikit/uiapplicationdelegate/1622921-application

    【讨论】:

      【解决方案2】:

      试试这个:

      在 AppDelegate 上添加委托。

      UNUserNotificationCenterDelegate
      

      还有..

      //Called when a notification is delivered to a foreground app.
      @available(iOS 10.0, *)
      func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification, withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
          print("User Info = ",notification.request.content.userInfo)
      
          completionHandler([.alert, .badge, .sound])
      }
      
      //Called to let your app know which action was selected by the user for a given notification.
      @available(iOS 10.0, *)
      func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
          print("ActionIdentifier = ",response.actionIdentifier)
          print("User Info = ",response.notification.request.content.userInfo)
      
          completionHandler()
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-03-25
        • 1970-01-01
        • 1970-01-01
        • 2021-06-06
        • 2016-05-05
        • 1970-01-01
        • 1970-01-01
        • 2020-09-12
        相关资源
        最近更新 更多