加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 04:29:36 所属栏目:百科 来源:网络整理
导读:我使用以下库来生成推送通知. https://github.com/edamov/pushok 我得到了推送通知,但我不知道如何在Swift 3中提取响应. 这就是我所拥有的. // Push notification received func application(_ application: UIApplication,didReceiveRemoteNotification dat
我使用以下库来生成推送通知.

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)
    }

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

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

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

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

解决方法

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

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)")

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读