在Swift中访问userInfo字典

时间:2015-03-09 20:41:33

标签: ios swift dictionary parse-platform notifications

我试图访问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
}

我没有收到任何错误,我根本没有得到任何错误。如果我打印userInfo字典,它只有一个成员,[&#34; aps&#34;],其中包含显示给用户的推送消息,所以即使我可以访问它,我也可以使用条件它。

我尝试按照Parse API写入userData:

let data = ["notificationType" : "coffee"]
push.setData(data)

所以一个问题是,这似乎没有设置任何东西,但更大的问题是我无法访问任何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

所以似乎警报密钥卡在这个'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")

}

以及来自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()
    }

}

1 个答案:

答案 0 :(得分: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)

此代码产生以下输出:

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

您应该能够在application(...)方法中添加我的代码,希望您能够看到如何打开双字典以及实际传输给您的信息。

PS!在answer to another post讨论他的setMessage如何扰乱他的setData时,如果有一些代码会影响你的结果,这也可能会很有趣。我的示例代码仍应列出userInfo

中收到的所有内容