简体   繁体   中英

iOS/Parse - Sending Push Notifications to Specific User

I am working on an app that allows "current user" to view an event posted by another user. When the current user join the event I'd like to send a push notification to the user who posted the event. I have set up push notification for my app with Parse. I am able to send push notifications to all users via a channel, but I still cannot figure out how to send a push notification to the specific user. I am able to receive the push notifications on my phone.

I tried to associate the device with a user with the following code:

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

Unfortunately - this makes my app crash. Not sure why there is not error message.

I was thinking of using the following code to send the push notification to a specific user. (This is code i got from Parse Documentation)

// 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. How do you associate the installation with the current user?
  2. Am I in the right track?
  3. If you know of a good tutorial, please let me know or if you have implemented something similar and could help me with this problem.

Thank you,

You first need to create a relationship between a user and it's installation. Remember that notifications on iOS are send to devices as the Apple notification system knows nothing about your users.

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

Now your code is easier to use:

// 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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM