Search code examples
iosparse-platformparse-cloud-code

Sending out a Targeted push notification with CloudCode


I tried writing out a cloud code function that sends targeted Push Notifications. The user id that I am passing in exists in the Parse database, however when I run this code I hit the response, success and my logs show that the result of the sent notification is true.

The notification exists in the PUSH logs, however when I look at the push details, it returns a 102 error for an invalid query on the User object.

This is the full target:

    { 
  "deviceType": "ios", 
  "user": { 
    "$exists": true, 
    "$inQuery": { 
      "className": "_User", 
      "where": { 
        "objectId": "R8q99hu62c" 
      } 
    } 
  } 
}

This is the error:

error 102: bad type for $inQuery 

Can someone please explain why this is happening?

    Parse.Cloud.define('sendPushNotification', function(request, response) {

    console.log(request);

    var userQuery = new Parse.Query(Parse.User);
    userQuery.equalTo('objectId', request.params.userId);

    var queryIos = new Parse.Query(Parse.Installation);
    queryIos.equalTo('deviceType', 'ios');
    queryIos.exists('user');

    queryIos.matchesQuery('user', userQuery);


    Parse.Push.send({
      where: queryIos,
      data: {
        alert: request.params.firstName + ' has invited you to join his party',
        badge: "Increment",
        guestlistInviteId: request.params.guestlistInviteId,
        notificationText: request.params.firstName + "has invited you to his party"
      }
    }).then(function(result) {
      console.log(result);
      response.success('sent out the push');
      }, function(error) {
          console.log('error');
          response.error(error);
    }); 
});

Edit:

Added a column in the installation table on parse as a pointer to user. Removed the exists and includes from the Installation query, while setting the user pointer to the user object that I have. This sends out a push notification to the targeted user.

 var queryIos = new Parse.Query(Parse.Installation);
 queryIos.equalTo('deviceType', 'ios');
 queryIos.equalTo("User", user);

Solution

  • According to Parse Docs, you're trying to query a datatype that doesn't support it.

    You can see that in this link.

    My guess is you can't query a Parse Installation for the User it's related to directly.

    I also found this, maybe it can be useful: Parse Question. It's basically saying that you need to create a pointer to the specific user inside of Installation, and then you can use "advanced targeting" to send push notifications to every device related to that user.

    I hope something here helps you.

    Best of luck.