【问题标题】:Parse: Sending push notication to a user解析:向用户发送推送通知
【发布时间】:2014-07-10 01:59:33
【问题描述】:

我正在尝试向用户或所有用户的列表发送推送通知。我可以使用这个 sn-p 在 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!"];

但是这个 sn-p 不起作用:

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 仪表板的更多详细信息:

【问题讨论】:

    标签: ios parse-platform


    【解决方案1】:

    您必须像您正在做的那样使用 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.. )。

    【讨论】:

    • 我都尝试使用 findObjectsInBackgroundWithBlock,获取返回对象中的第一个对象,然后将其输入 getObjectInBackgroundWithId:aUser.objectId。并直接使用 getFirstObjectInBackgroundWithBlock ,但仍然无法正常工作。仪表板中的“订阅者”状态何时会显示 0 以外的数字?
    • 从仪表板添加了更多详细信息。
    【解决方案2】:

    事实证明,要接收个人推送通知,接收方应用程序也需要执行此操作(登录后):

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多