发送推送通知给查询中的所有用户

我正在创build一个使用Parse云的应用程序。 我正试图从用户发送一条消息给其他人。 在文本字段中写入消息并按发送之后,委托调用以下方法,并按如下所示处理推送:

- (BOOL)textFieldShouldReturn:(UITextField *)textField { //hide keyboard [textField resignFirstResponder]; NSString *message = self.broadcastText.text; //create a user query PFQuery *query = [PFUser query]; PFUser *current = [PFUser currentUser]; NSString *currentUsername = current[@"username"]; [query whereKey:@"username" notEqualTo:currentUsername]; //send push in background PFPush *push = [[PFPush alloc] init]; [push setQuery:query]; [push setMessage:message]; [push sendPushInBackground]; //clear text field textField.text = @""; return YES;} 

发生什么事是我发送消息的时候,发送者(在这个例子中是我)也接收到推送通知。 我试图做的是获取当前用户的用户名,然后创build一个用户查询,查询用户名不等于当前用户的用户名的所有用户。

然而,它没有工作,该消息也被发送给包括发件人在内的所有用户。

注意:我也尝试使用[查询whereKey:@“username”notEqualTo:currentUsername]; 只是为了debugging,当我尝试发送消息时,发送者和任何其他设备都不会收到消息。 (实际上除了寄件人以外没有人收到)。

任何帮助将不胜感激。 谢谢。

你的问题是PFPush不能进行任何查询,它需要一个PFInstallation查询。 当您为每个用户存储PFInstallation ,可以添加一个指向当前用户的字段,如下所示:

 PFInstallation *currentInstallation = [PFInstallation currentInstallation]; currentInstallation[@"user"] = [PFUser currentUser]; [currentInstallation saveInBackground]; 

然后,像这样做一个安装查询:

 PFQuery *installationQuery = [PFInstallation query]; PFUser *current = [PFUser currentUser]; [installationQuery whereKey:@"user" notEqualTo:current]; 

然后,继续使用此查询进行推送:

 PFPush *push = [[PFPush alloc] init]; [push setQuery:installationQuery]; // <<< Notice query change here [push setMessage:message]; [push sendPushInBackground]; 

从理论上讲,您可以向所有订阅某个频道的用户发送推送通知。 现在你有一个所有用户和所有频道的表格。 有些用户订阅其他没有。 首先为安装创build一个查询,然后查找不是当前用户的用户。

 PFQuery *pushQuery = [PFInstallation query]; [pushQuery whereKey:"user" notEqualTo:[PFUser currentUser]]; 

创build一个Push对象并使用此查询。

 PFPush *push = [[PFPush alloc] init]; [push setQuery:pushQuery]; // Set our Installation query [push setMessage:@"Ciao."]; [push sendPushInBackground]; 

在pushQuery中,您可以使用其他键,例如:deviceID,installationID,deviceType等。我使用Parse Cloud,但是我从不使用推送通知,因此您需要尝试此代码。