Parse, how to send push notification to targeted u

2019-03-02 17:46发布

问题:

I have successfully setup parse push notifications and in my installation table I have both an installation, and device token. What I'm really trying to do is send a push notification to certain users, rather than certain devices. How do I bing the installations table to the uses table, so that I can make a query by users and get back a deviceid to push to

回答1:

From https://parse.com/docs/push_guide#top/iOS, section "Using Advanced Targeting".

You can even create relationships between your Installation objects and other classes saved on Parse. To associate a PFInstallation with a particular user, for example, you can simply store the current user on the PFInstallation.

// Associate the device with a user
PFInstallation *installation = [PFInstallation currentInstallation];
installation[@"user"] = [PFUser currentUser];
[installation saveInBackground];

Now you can create a query on the installation table, where "user" is the user you want to send a push notification to.

Finally, use that query when constructing the push object.

Example in Objective-C (adjust accordingly, if you're sending the push in some other language):

PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"user" equalTo:someUser]; // where some user is the user object that is to receive a push notification

PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery];
[push setMessage:@"Hi there!"];
[push sendPushInBackground];