简体   繁体   English

快速将带有解析的通知推送给收件人

[英]push notification with parse to recipient with swift

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. 我在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. 我已经包括了必需的框架。

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){让安装= PFInstallation.currentInstallation()println(“已向设备令牌(deviceToken)注册”)对象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" 我正在进入“成功”

Where are you running this code from? 您在哪里运行此代码? Also, what is "device_id"? 另外,“ device_id”是什么? Is that another column you added to the Installation class? 那是您添加到Installation类的另一列吗?

You probably will want to add a pointer to the current user inside of the Installation class. 您可能需要在Installation类中添加指向当前用户的指针。 That way you can query for the installations of particular users in the future. 这样,您就可以查询将来特定用户的安装。

You also don't need to use a for loop to go through all of the objectIds. 您也不需要使用for循环来遍历所有objectIds。 All of this can be handled using one query using the following: 所有这些都可以使用以下查询通过一个查询来处理:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM