简体   繁体   中英

Parse iOS: Sending a push notification to a specific person/device

I'm trying to send a push notification to a specific device, but I can only get it to work if i send to a channel.

"userQuery.countObjects" is returning 1 user found, so I don't know what's wrong, help!

Here is my code:

              PFUser *user1= [friendUsers objectAtIndex:0];

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

              PFQuery *userQuery = [PFUser query];
              [userQuery whereKey:@"fbId" equalTo:user1[@"fbId"]];
              userQuery.limit = 1;

              NSLog(@"cnt=%d", userQuery.countObjects);

             // Find devices associated with these users
             PFQuery *pushQuery = [PFInstallation query];
             [pushQuery whereKey:@"user" matchesQuery:userQuery];

             // Send push notification to query
             PFPush *push = [[PFPush alloc] init];
             [push setQuery:pushQuery]; // Set our Installation query
             [push setData:data];
             [push sendPushInBackground];

The Parse push dashboard shows me this, I've removed the ID

PUSH ID
dkfo3WhNod

TARGETING
user advanced operator ($inQuery {"className"=>"_User", "limit"=>"1",    "where"=>{"fbId"=>"xxxx"}}) 
SENDING TIME
October 16th, 2015 at 4:37 PM

EXPIRATION
None

FULL TARGET
{ 
 "user": { 
   "$inQuery": { 
  "className": "_User", 
  "limit": "1", 
  "where": { 
    "fbId": "xxx" 
    } 
   } 
  } 
 }

FULL DATA
{ 
   "alert": "Hi", 
   "badge": "Increment", 
   "sound": "hi.wav" 
 }

You're on the right track by associating the installation objects with their respective users for targeting, but I think something has gone awry with the user query by fbId.

NSLog(@"cnt=%d", userQuery.countObjects); is returning one not because there is one user found (the query is never executed), but rather because it is counting the single query object.

Double check that the user query is returning what you think it is. Execute the query separately and print out the results.

Also, it looks like you already have the array of users friendUsers . Since you already have the pointer, there is no need to have a separate user query.

If you would like to send push notifications to all users in the array, try the following

// Find devices associated with these users
PFQuery *pushQuery = [PFInstallation query];
[pushQuery whereKey:@"user" containedIn:friendUsers];

// Send push notification to query
PFPush *push = [[PFPush alloc] init];
[push setQuery:pushQuery]; // Set our Installation query
[push setData:data];
[push sendPushInBackground];

Similarly, if you are only concerned with the first user, just use whereKey:@"user" equalTo:[friendUsers objectAtIndex:0]

As a helpful hint, you will also want to offload as much logic as possible to cloud code. Rather than making the user association client side, you could have the following in a cloud code beforeSave function

// Make sure all installations point to the current user
Parse.Cloud.beforeSave(Parse.Installation, function(request, response) {

    Parse.Cloud.useMasterKey();
    if (request.user) {
        request.object.set('user', request.user);
    } else {
        request.object.unset('user');
    }
    response.success();
});

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