vol*_*757 1 notifications dictionary ios parse-platform swift
我正在尝试访问userInfo字典,application:didReceiveRemoteNotification()以便我可以根据收到的推送类型/是否收到任何推送来选择segue.我已经尝试过这两种方式(其他一些方式,我敢肯定,我现在还记不起来).
if let info = userInfo as? Dictionary<String,String> {
var notificationType = info["notificationType"]
}
if let info: String = userInfo["notificationType"] as? String {
//do stuff
}
Run Code Online (Sandbox Code Playgroud)
我没有得到任何错误,我根本没有得到任何东西.如果我打印userInfo字典,它只有一个成员["aps"],其中包含显示给用户的推送消息,所以即使我可以访问它,我也可以使用它进行条件化.
我尝试按照Parse API写入userData:
let data = ["notificationType" : "coffee"]
push.setData(data)
Run Code Online (Sandbox Code Playgroud)
所以一个问题是这似乎没有设置任何东西,但更大的问题是我无法获取任何userInfo数据.
你怎么访问这本字典?
编辑
一些打印声明结果:
println(userInfo["aps"]) => {alert = "You've Been Invited To A Coffee Order";}
println(userInfo) => [aps: {alert = "You've Been Invited To A Coffee Order";}, type: coffee]
println(userInfo[0]) => nil
let info = userInfo as? Dictionary<String,String>
println(info) => nil
Run Code Online (Sandbox Code Playgroud)
所以似乎警报键被困在这个'aps'数组中?字典?我没有创建它,它只是自动的一部分userInfo
编辑2
这是我设置数据并发送推送的代码.我userInfo从这里发送出现的数据:
func notifyUserOfCoffeeOrder(){
var uQuery:PFQuery = PFUser.query()
uQuery.whereKey("username", equalTo: usernameLabel.text)
var pushQuery:PFQuery = PFInstallation.query()
pushQuery.whereKey("user", matchesQuery: uQuery)
var push:PFPush = PFPush()
push.setQuery(pushQuery)
let data = ["type" : "coffee",
"alert" : "You've Been Invited To A Coffee Order"]
push.setData(data)
push.sendPush(nil)
println("push sent")
}
Run Code Online (Sandbox Code Playgroud)
这是来自AppDelegate.swift的整个函数,我将收到的数据拉出来:
func application(application: UIApplication, didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
NSNotificationCenter.defaultCenter().postNotificationName("getMessage", object: nil)
if let info = userInfo["type"] as? String {
self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var initialViewController = storyboard.instantiateViewControllerWithIdentifier("coffeeVC") as UIViewController
self.window?.rootViewController = initialViewController
self.window?.makeKeyAndVisible()
}
}
Run Code Online (Sandbox Code Playgroud)
hol*_*roy 11
编辑:我知道重建这个答案符合给出的评论.请注意,我目前无法访问Apples推送通知服务,因此无法在与OP相同的环境中对此进行测试
似乎APN(或parse.com)在userInfo构建它时会在这里和那里添加一点点.因此,我最好的建议是使用以下代码从您收到的双字典中打印所有内容.我还提供了代码来打开在字典中查找内容时出现的选项.
所以这是我修改过的示例代码:
func myApplicationFunc(didReceiveRemoteNotification userInfo: [NSObject : AnyObject]) {
// NSNotificationCenter.defaultCenter().postNotificationName("getMessage", object: nil)
// Default printout of userInfo
println("All of userInfo:\n\( userInfo)\n")
// Print all of userInfo
for (key, value) in userInfo {
println("userInfo: \(key) —> value = \(value)")
}
if let info = userInfo["aps"] as? Dictionary<String, AnyObject> {
// Default printout of info = userInfo["aps"]
println("All of info: \n\(info)\n")
for (key, value) in info {
println("APS: \(key) —> \(value)")
}
if let myType = info["type"] as? String {
// Printout of (userInfo["aps"])["type"]
println("\nFrom APS-dictionary with key \"type\": \( myType)")
// Do your stuff?
}
}
}
var userInfo : [ NSObject: AnyObject] =
[
"aps" : [
"alert" : "Coffee is not the answer",
"type" : "coffee"
],
"anotherType" : 123
]
myApplicationFunc(didReceiveRemoteNotification: userInfo)
Run Code Online (Sandbox Code Playgroud)
此代码生成以下输出:
All of userInfo:
[anotherType: 123, aps: {
alert = "Coffee is not the answer";
type = coffee;
}]
userInfo: anotherType —> value = 123
userInfo: aps —> value = {
alert = "Coffee is not the answer";
type = coffee;
}
All of info:
[alert: Coffee is not the answer, type: coffee]
APS: alert —> Coffee is not the answer
APS: type —> coffee
From APS-dictionary with key "type": coffee
Run Code Online (Sandbox Code Playgroud)
您应该能够在您的application(...)方法中添加我的代码,并希望您将看到如何解开双字典以及实际传输给您的信息.
PS!在另一篇关于他如何setMessage扰乱他的帖子的回答中setData,这可能也很有趣,如果有一些代码会影响你的结果.我的示例代码仍应列出收到的所有内容userInfo