3

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,

TheRealRonDez
  • 2,807
  • 2
  • 30
  • 40

1 Answers1

4

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];
Mika
  • 5,807
  • 6
  • 38
  • 83
  • you were right - you need to create the relationship between the user and it's installation. Then, get the user that you'd like to send the notification and do a check if that user exists in the Installation. You can do so in the inner query [query whereKey:@"user" matchesQuery: someUser]; - finally got it to work thank you! – TheRealRonDez May 26 '15 at 01:37