首页 文章

Swift读取远程通知的userInfo

提问于
浏览
40

当我收到这样的远程通知时,我实现了一个打开AlertView的函数:

func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]){
        var notifiAlert = UIAlertView()
        var NotificationMessage : AnyObject? =  userInfo["alert"]
        notifiAlert.title = "TITLE"
        notifiAlert.message = NotificationMessage as? String
        notifiAlert.addButtonWithTitle("OK")
        notifiAlert.show()
}

但NotificationMessage总是零 .

我的json有效负载如下所示:

{"aps":{"alert":"Testmessage","badge":"1"}}

我正在使用Xcode 6,Swift和我正在为iOS8开发 . 我现在搜索了几个小时,但没有找到任何有用的信息 . 通知工作完美..如果我点击它,则会打开alertview . 我的问题是,我无法从userInfo中获取数据 .

4 回答

  • 0

    方法(swift4):

    func extractUserInfo(userInfo: [AnyHashable : Any]) -> (title: String, body: String) {
        var info = (title: "", body: "")
        guard let aps = userInfo["aps"] as? [String: Any] else { return info }
        guard let alert = aps["alert"] as? [String: Any] else { return info }
        let title = alert["title"] as? String ?? ""
        let body = alert["body"] as? String ?? ""
        info = (title: title, body: body)
        return info
    }
    

    用法:

    let info = self.extractUserInfo(userInfo: userInfo)
    print(info.title)
    print(info.body)
    
  • 1

    userInfo 字典的根级别项是 "aps" ,而不是 "alert" .

    请尝试以下方法:

    if let aps = userInfo["aps"] as? NSDictionary {
        if let alert = aps["alert"] as? NSDictionary {
            if let message = alert["message"] as? NSString {
               //Do stuff
            }
        } else if let alert = aps["alert"] as? NSString {
            //Do stuff
        }
    }
    

    Push Notification Documentation

  • 4

    应用程序处于活动状态时应显示警报 . 所以检查状态是否有效 .

    func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
        if application.applicationState == .active {
          if let aps = userInfo["aps"] as? NSDictionary {
            if let alertMessage = aps["alert"] as? String {
              let alert = UIAlertController(title: "Notification", message: alertMessage, preferredStyle: UIAlertControllerStyle.alert)
              let action = UIAlertAction(title: "Ok", style: .default, handler: nil)
              alert.addAction(action)
              self.window?.rootViewController?.present(alert, animated: true, completion: nil)
            }
          }
        }
        completionHandler(.newData)
      }
    

    如果用户需要消息,那么他可以获得警报消息 .

  • 88

    对我来说,当我从Accengage发送消息时,以下代码有效 -

    private func extractMessage(fromPushNotificationUserInfo userInfo:[NSObject: AnyObject]) -> String? {
        var message: String?
        if let aps = userInfo["aps"] as? NSDictionary {
            if let alert = aps["alert"] as? NSDictionary {
                if let alertMessage = alert["body"] as? String {
                    message = alertMessage              
                }
            }
        }
        return message
    }
    

    与Craing Stanford的答案唯一不同的是 key 我曾经从 alert 实例中提取消息,这是 body ,这是不同的 . 请参阅下文以获得更多清晰度 -

    if let alertMessage = alert["message"] as? NSString
    

    VS

    if let alertMessage = alert["body"] as? String
    

相关问题