我正在尝试向特定收件人发送推送通知。我现在正在练习一个硬编码的对象id数组,它存在于我的解析后端。
我在appDelegate中调用代码。我的证书和个人资料中有正确的权利。在解析仪表板中,它表示通知正在发送,但测试设备上未收到通知。测试设备可以接收从解析仪表板发送的通知。我已经包含了所需的框架。
代码: 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))")
}
}
}
}
我正在进入“成功”
答案 0 :(得分:0)
您从哪里运行此代码?另外,什么是" device_id"?是您添加到Installation类的另一列吗?
您可能希望在Installation类中添加指向当前用户的指针。这样,您可以在将来查询特定用户的安装。
您也不需要使用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))")
}
}