EmilyJ
EmilyJ

Reputation: 892

Parse: Sending push notication to a user

I am trying to send a push notification to a user or list of all users. I can successfully send (and receive) on ios devices with this snippet:

// Create our Installation query
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"deviceType" equalTo:@"ios"];

// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
                               withMessage:@"Hello World!"];

But this snippet does not work:

PFQuery *userQuery = [PFUser query];
NSLog(@"findObjects: %@",[userQuery findObjects]);

[userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    NSLog(@"findObjectsInBackgroundWithBlock error: %@", error);
    NSLog(@"findObjectsInBackgroundWithBlock objects: %@", objects);   //this log indicated I do have one user named "user name"
}];

    PFQuery *pushQuery = [PFInstallation query];
//    [pushQuery whereKey:@"user" matchesQuery:userQuery];
    [pushQuery whereKey:@"username" containsString:@"user name"];   //neither one of these works


// Send push notification to query
[PFPush sendPushMessageToQueryInBackground:pushQuery
                               withMessage:@"for you only"];

On parse.com site's Push Notifications dashboard, it has a status of "done" but the Subscribers is 0.

Any suggestions?

Edit: More detail from Parse's dashboard:

enter image description here

Upvotes: 0

Views: 930

Answers (2)

EmilyJ
EmilyJ

Reputation: 892

As in turns out, to receive individual push notifications, the receiver app needed also do this (after signing in):

[[PFInstallation currentInstallation] setObject:[PFUser currentUser] forKey:@"user"];
[[PFInstallation currentInstallation] saveEventually:^(BOOL succeeded, NSError *error) {
    //NSLog(@"saveeventually bool: %hhd error: %@", succeeded, error);
}];

Upvotes: 2

JoshR604
JoshR604

Reputation: 155

You have to query for your user with a PFUser query like you are doing. Once the user that you want to send your push to is found you have to use that 'PFUser object' to do an installation query. (You should use the whole PFUser object as the key for the installation query)

If you want to send a push to a group of users you have to set up Parse's "Segments" and assign users to that segment. Read up on Parse iOS push docs https://parse.com/docs/push_guide#top/iOS

Here's an example for sending a push to a single user:

PFQuery *qry = [PFUser query];
                [qry getObjectWithId: THE USERS OBJECT ID HERE ];

                PFQuery *pushQuery = [PFInstallation query];
                [pushQuery whereKey:@"user" matchesQuery:qry];

                // Send push notification to query
                PFPush *push = [[PFPush alloc] init];
                [push setQuery:pushQuery]; // Set our Installation query
                NSDictionary *data = [NSDictionary dictionaryWithObjectsAndKeys:
                                      @"A New Push Message was Put HERE!", @"alert",
                                      @"ursound.caf", @"sound",
                                      @"Increment", @"badge",
                                      @"Optionally a type was set", @"type",
                                      nil];
                [push setData:data];


                [push sendPushInBackground];

Edit - Oh and those initial queries in my example are presumed to be already in a background task -> use getObjectInBackgroundWithId:^(bla bla.. ) if not running this in a background task.

Upvotes: 2

Related Questions