Sending push notification to all users in query

2019-09-01 06:34发布

问题:

I am creating an app that uses Parse cloud. I am trying to send a message from a user to all others. After writing the message in a textfield and pressing send, the delegate calls the following method and the push is handled as shown below:

- (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;}

What's happening is when I send the message, the sender (in this case me) is receiving the push notification as well. What I tried to do is get the current user's username and then create a user query that queries for all users whose usernames are not equal to the current user's username.

However, it didn't work, the message was also being sent to all users including the sender.

Note: I also tried using [query whereKey:@"username" notEqualTo:currentUsername]; just for debugging, and when I try to send the message, neither the sender nor any other device receives it. (when actually no one should receive it except the sender).

Any help would be greatly appreciated. Thank you.

回答1:

Your problem is that a PFPush can't take any query, it needs a PFInstallation query. When you store your PFInstallation for each user, you could add a field that points to the current user, something like:

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

Then, do an installation query like this:

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

Then, continue with your push using this query:

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


回答2:

In theory you send push notifications to all users that are subscribed to some channel ok. Now you have a table with all users and all channels. Some users are subscribed other are not. First create a query for the installation, than find users that are not current user.

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

Create a Push object and use this query.

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

In pushQuery you can use other key, example: deviceID,installationID, deviceType etc.. I use Parse Cloud but i never use push notification so you need to try this code.