iOS/Parse - 向特定用户发送推送通知

3
我正在开发一款应用程序,允许“当前用户”查看其他用户发布的事件。当当前用户加入事件时,我希望向发布该事件的用户发送推送通知。我已经使用Parse为我的应用程序设置了推送通知。我能够通过一个频道向所有用户发送推送通知,但我仍然无法弄清如何向特定用户发送推送通知。我能够在手机上接收到推送通知。
我尝试使用以下代码将设备与用户关联:
PFInstallation *installation = [PFInstallation currentInstallation];
installation[@"user"] = [PFUser currentUser];
[installation saveInBackground];

很不幸 - 这会导致我的应用崩溃。不确定为什么没有错误消息。

我正在考虑使用以下代码向特定用户发送推送通知。(这是我从Parse文档中获取的代码)

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

// Send push notification to query
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery]; // Set our Installation query
[push setMessage:@"Willie Hayes injured by own pop fly."];
[push sendPushInBackground];
  1. 如何将安装与当前用户关联?
  2. 我是否在正确的轨道上?
  3. 如果您知道一个好的教程,请告诉我,或者如果您已经实现了类似的东西并能帮助我解决这个问题。

谢谢!

1个回答

4

首先,您需要创建用户与其安装之间的关系。请记住,iOS上的通知是发送到设备上的,因为苹果通知系统不知道您的用户。

[[PFInstallation currentInstallation] setObject:[PFUser currentUser] forKey:@"user"];
[[PFInstallation currentInstallation] saveEventually];

现在您的代码更易于使用:
// Create our Installation query
PFQuery *pushQuery = [PFInstallation query];
// only return Installations that belong to a User that
// matches the innerQuery
[query whereKey:@"user" matchesQuery: pushQuery];

// Send push notification to query
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery]; // Set our Installation query
[push setMessage:@"Willie Hayes injured by own pop fly."];
[push sendPushInBackground];

你是对的 - 你需要创建用户和它的安装之间的关系。然后,获取您想发送通知的用户,并检查该用户是否存在于安装中。您可以在内部查询中执行此操作[query whereKey:@"user" matchesQuery: someUser]; - 最终成功了,谢谢! - TheRealRonDez

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接