Swift read userInfo of remote notification

后端 未结 7 1609
庸人自扰
庸人自扰 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条回答
  • 2020-12-08 03:10

    Swift 5

    struct Push: Decodable {
        let aps: APS
        
        struct APS: Decodable {
            let alert: Alert
            
            struct Alert: Decodable {
                let title: String
                let body: String
            }
        }
        
        init(decoding userInfo: [AnyHashable : Any]) throws {
            let data = try JSONSerialization.data(withJSONObject: userInfo, options: .prettyPrinted)
            self = try JSONDecoder().decode(Push.self, from: data)
        }
    }
    

    Usage:

    guard let push = try? Push(decoding: userInfo) else { return }
    let alert = UIAlertController(title: push.aps.alert.title, message: push.aps.alert.body, preferredStyle: .alert)
    
    0 讨论(0)
提交回复
热议问题