使用swift向收件人解析通知

时间:2021-11-16 01:37:52

I am trying to send a push notification to a particular recipient. I am just practising for now with a hard coded array of object ids which exist on my parse back end.

我正在尝试向特定收件人发送推送通知。我现在正在练习一个硬编码的对象id数组,它存在于我的解析后端。

I am calling the code in the appDelegate. I have the correct entitlements in my certificates and profiles. In the parse dashboard, it says the notifications are sending but they are not being received on the test devices. The test devices can receive notifications that are sent from the parse dashboard. I have included the required frameworks.

我在appDelegate中调用代码。我的证书和个人资料中有正确的权利。在解析仪表板中,它表示通知正在发送,但测试设备上未收到通知。测试设备可以接收从解析仪表板发送的通知。我已经包含了所需的框架。

Code: func application(application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: NSData) { let installation = PFInstallation.currentInstallation() println("did register with device token (deviceToken)") installation.setDeviceTokenFromData(deviceToken) installation.saveInBackground() println("the object id may be (installation.objectId)")

代码:func应用程序(应用程序:UIApplication,didRegisterForRemoteNotificationsWithDeviceToken deviceToken:NSData){let installation = PFInstallation.currentInstallation()println(“确实注册了设备令牌(deviceToken)”)installation.setDeviceTokenFromData(deviceToken)installation.saveInBackground()println(“对象id可以是(installation.objectId)“)

    let query = PFQuery()
    let objectIDArray : [String] = ["uQfdVtB6pk","kUL0EeXjzY", "6uGdmKv599"]

    for object in objectIDArray {
        println("objectid is \(object)")

        let message: NSString = "test" as NSString

        var data = [ "title": "Some Title",
            "alert": message]

        var userQuery: PFQuery = PFUser.query()!
        userQuery.whereKey("objectId", equalTo: object)

        var query: PFQuery = PFInstallation.query()!
        query.whereKey("Installation", equalTo: installation)
        query.whereKey("device_id", equalTo: object)

        var push: PFPush = PFPush()
        push.setQuery(query)
        push.setData(data)
        push.sendPushInBackgroundWithBlock {
            (success: Bool, error: NSError?) -> Void in
            if success {
                println("IN success")
            } else {
                println("IN ERROR WITH \(error?.localizedDescription))")
            }
        }

    }

}

I am getting into the "IN SUCCESS"

我正在进入“成功”

1 个解决方案

#1


0  

Where are you running this code from? Also, what is "device_id"? Is that another column you added to the Installation class?

你在哪里运行这段代码?另外,什么是“device_id”?是您添加到Installation类的另一列吗?

You probably will want to add a pointer to the current user inside of the Installation class. That way you can query for the installations of particular users in the future.

您可能希望在Installation类中添加指向当前用户的指针。这样,您可以在将来查询特定用户的安装。

You also don't need to use a for loop to go through all of the objectIds. All of this can be handled using one query using the following:

您也不需要使用for循环来遍历所有objectIds。所有这些都可以使用以下一个查询来处理:

var query: PFQuery = PFInstallation.query()!
query.whereKey("objectId", containedIn: objectIDArray)

var push: PFPush = PFPush()
push.setQuery(query)
push.setData(data)
push.sendPushInBackgroundWithBlock {
    (success: Bool, error: NSError?) -> Void in
    if success {
        println("IN success")
    } else {
        println("IN ERROR WITH \(error?.localizedDescription))")
    }
}

#1


0  

Where are you running this code from? Also, what is "device_id"? Is that another column you added to the Installation class?

你在哪里运行这段代码?另外,什么是“device_id”?是您添加到Installation类的另一列吗?

You probably will want to add a pointer to the current user inside of the Installation class. That way you can query for the installations of particular users in the future.

您可能希望在Installation类中添加指向当前用户的指针。这样,您可以在将来查询特定用户的安装。

You also don't need to use a for loop to go through all of the objectIds. All of this can be handled using one query using the following:

您也不需要使用for循环来遍历所有objectIds。所有这些都可以使用以下一个查询来处理:

var query: PFQuery = PFInstallation.query()!
query.whereKey("objectId", containedIn: objectIDArray)

var push: PFPush = PFPush()
push.setQuery(query)
push.setData(data)
push.sendPushInBackgroundWithBlock {
    (success: Bool, error: NSError?) -> Void in
    if success {
        println("IN success")
    } else {
        println("IN ERROR WITH \(error?.localizedDescription))")
    }
}