Swift read userInfo of remote notification

后端 未结 7 1633
庸人自扰
庸人自扰 2020-12-08 02:11

I implemented a function to open an AlertView when I receive a remote notification like this:

func application(application: UIApplication, didReceiveRemoteNo         


        
7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-08 02:54

    Method (Swift 4):

    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
    }
    

    Usage:

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

提交回复
热议问题