ios - 解析 : Sending push notication to a user

标签 ios parse-platform

我正在尝试向用户或所有用户列表发送推送通知。我可以使用以下代码段在 iOS 设备上成功发送(和接收):

// Create our Installation query
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"deviceType" equalTo:@"ios"];

// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
                               withMessage:@"Hello World!"];

但是这段代码不起作用:

PFQuery *userQuery = [PFUser query];
NSLog(@"findObjects: %@",[userQuery findObjects]);

[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    NSLog(@"findObjectsInBackgroundWithBlock error: %@", error);
    NSLog(@"findObjectsInBackgroundWithBlock objects: %@", objects);   //this log indicated I do have one user named "user name"
}];

    PFQuery *pushQuery = [PFInstallation query];
//    [pushQuery whereKey:@"user" matchesQuery:userQuery];
    [pushQuery whereKey:@"username" containsString:@"user name"];   //neither one of these works


// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
                               withMessage:@"for you only"];

在 parse.com 网站的推送通知仪表板上,它的状态为“完成”,但订阅者为 0。

有什么建议吗?

编辑:来自 Parse 仪表板的更多详细信息:

enter image description here

最佳答案

您必须像现在一样使用 PFUser 查询来查询您的用户。一旦找到您要向其发送推送的用户,您就必须使用该“PFUser 对象”来执行安装查询。 (您应该使用整个 PFUser 对象作为安装查询的键)

如果您想向一组用户发送推送,您必须设置 Parse 的“分割”并将用户分配到该分割。阅读 Parse iOS 推送文档 https://parse.com/docs/push_guide#top/iOS

这是向单个用户发送推送的示例:

PFQuery *qry = [PFUser query];
                [qry getObjectWithId: THE USERS OBJECT ID HERE ];

                PFQuery *pushQuery = [PFInstallation query];
                [pushQuery whereKey:@"user" matchesQuery:qry];

                // Send push notification to query
                PFPush *push = [[PFPush alloc] init];
                [push setQuery:pushQuery]; // Set our Installation query
                NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"A New Push Message was Put HERE!", @"alert",
                                      @"ursound.caf", @"sound",
                                      @"Increment", @"badge",
                                      @"Optionally a type was set", @"type",
                                      nil];
                [push setData:data];


                [push sendPushInBackground];

编辑 - 哦,假设我示例中的那些初始查询已经在后台任务中 -> 使用 getObjectInBackgroundWithId:^(bla bla.. ) 如果不在后台任务中运行它。

关于ios - 解析 : Sending push notication to a user,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24642671/

相关文章:

ios - 在 iOS 的 Parse 中处理一对多关系

iOS 7/Xcode 5 : Access device launch images programmatically

ios - 使用 Alamofire 从 Firebase 检索 JSON

ios - 如何使用字符串扩展

ios - 如何观察图像旋转?

android - 解析 sdk android 应用程序未在 5.0 中加载内容

ios - AWS Cognito 注册 : Error 37

ios - 搜索栏不响应 iOS

javascript - 将 Parse 的 JavaScript 框架与 Google Apps 脚本结合使用

android - 解析查询仅适用于一台设备