解析:向用户发送推送通知

时间:2014-07-08 22:44:12

标签: 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

2 个答案:

答案 0 :(得分:2)

您必须像使用PFUser查询一样查询您的用户。找到要发送推送到的用户后,您必须使用该PFUser对象'进行安装查询。 (您应该使用整个PFUser对象作为安装查询的密钥)

如果您想向一组用户发送推送,则必须设置Parse" Segments"并将用户分配给该细分。阅读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 ..)。

答案 1 :(得分:2)

事实证明,要接收个别推送通知,接收方应用程序也需要这样做(登录后):

[[PFInstallation currentInstallation] setObject:[PFUser currentUser] forKey:@"user"];
[[PFInstallation currentInstallation] saveEventually:^(BOOL succeeded, NSError *error) {
    //NSLog(@"saveeventually bool: %hhd error: %@", succeeded, error);
}];