如何从推送通知响应Swift 3/iOS获取数据

Jos*_*han 4 push-notification apple-push-notifications ios swift3 ios10

我使用以下库来生成推送通知.

https://github.com/edamov/pushok

我得到了推送通知,但我不知道如何在Swift 3中提取响应.

这就是我所拥有的.

// Push notification received
    func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {
        // Print notification payload data
        print("Push notification received: \(data)")

        let aps = data[AnyHashable("aps")]!

        print(aps)
    }
Run Code Online (Sandbox Code Playgroud)

我可以创建推送通知和控制台消息工作但打印出来...

Push notification received: [AnyHashable("samplekey"): samplevalue, AnyHashable("aps"): {
    alert =     {
        body = hey;
        title = "Hello!";
    };
    sound = default;
}]
{
    alert =     {
        body = hey;
        title = "Hello!";
    };
    sound = default;
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是我如何访问'body'和'title'的alert中的数据?

我尝试访问变量,但我不断收到错误,因为我不确定我应该如何访问它,并且在任何教程中找不到关于此主题的任何文档.

ffa*_*bri 18

我认为这是一种更安全的方式来做约瑟夫发现的方式.

guard
    let aps = data[AnyHashable("aps")] as? NSDictionary,
    let alert = aps["alert"] as? NSDictionary,
    let body = alert["body"] as? String,
    let title = alert["title"] as? String
    else {
        // handle any error here
        return
    }

print("Title: \(title) \nBody:\(body)")
Run Code Online (Sandbox Code Playgroud)


dir*_*nee 5

我希望您尝试找到一种避免使用强制展开的方法。当您执行以下操作时:

 let alert = aps["alert"]! as! NSDictionary
 let body = alert["body"] as! String
 let title = alert["title"] as! String
Run Code Online (Sandbox Code Playgroud)

当缺少上述任何值时,应用程序将崩溃。

相反,首先让我们介绍一个通知模型。

class MTNotification {
    let alert: [String: AnyHashable]
    var title: String?
    var body: String?

    init(alert: [String: AnyHashable]) {
        self.alert = alert
    }
}
Run Code Online (Sandbox Code Playgroud)

使用一些东西来跟踪处理原始通知数据时可能发生的错误。如果你让它符合Error协议就更好了。

enum MTError: Error {
    // Observe your transformation and extend error cases
    case missingProperty(id: String)
}
Run Code Online (Sandbox Code Playgroud)

使用助手类不污染应用程序委托,您可以在其中处理数据到通知的转换。

class MTNotificationBuilder {

     /// Build a notification from raw data
     ///
     /// - Parameter data: Classic notification payload, received from app delegate
     /// - Returns: customized MTNotification
     /// - Throws: error while building a valid MTNotification object
    static func build(from data: [AnyHashable : Any]) throws -> MTNotification {
        guard let aps = data["aps"] as? [String: AnyHashable] else {
            // Do some error handlig
            throw MTError.missingProperty(id: "aps")
        }

        guard let alert = aps["alert"] as? [String: AnyHashable] else {
            // Do some error handlig
            throw MTError.missingProperty(id: "aps")
        }

        let notification = MTNotification(alert: alert)
        // Assign values read as optionals from alert dictionary
        notification.title = alert["title"] as? String
        notification.body = alert["body"] as? String

        return notification
    } 
}
Run Code Online (Sandbox Code Playgroud)

最后您需要做的就是调用 builder 函数并查看结果。您可以严格并在任何情况下引入错误,使用也将有助于以后的可维护性。

func application(_ application: UIApplication, didReceiveRemoteNotification data: [AnyHashable : Any]) {

    do {
        let notification = try MTNotificationBuilder.build(from: data)
        print(notification.alert)
    } catch let error {
        print(error)
    }
}
Run Code Online (Sandbox Code Playgroud)