Jacolack Jacolack - 2 months ago 21
Swift Question

Query Session class with cloud code Parse

I am trying to send a push notification using a user's id. I have already tested sending with installationId, querying the _Installation class, but i would like to query the session class of the user pointer, to then turn around and query the installation class.

My problem is lying in the restrictions of querying the session class. I have successfully used createWithoutData() found here, and I know it is working because i can output that user. However, even after using the master key found here, the results are always empty.

Answer

The general practise for sending Push Notification to specific user is that you will store pointer to User in Installation class... for example when user register do this

Swift

if let installation = PFInstallation.current() {
    installation["user_id"] = PFUser.current()!
    installation.saveInBackground()
}

Cloudcode

      var pushQuery = new Parse.Query(Parse.Installation);
      pushQuery.equalTo('user_id', tarUser);
      pushQuery.exists("deviceToken");
      pushQuery.limit(1); // in case there are more Installation with the user ID, use only the latest
      pushQuery.descending("createdAt");
      Parse.Push.send({
        where: pushQuery, // Set our Installation query
        data: {
          alert: "Some push text"
        }
      }, {
      success: function() {
        // Push was successful
        response.success();

      },
      error: function(error) {
        console.error("Got an error " + error.code + " : " + error);
        response.error(error);
      },
       useMasterKey: true 
    });

if I remember correctly you have to query the pointer in Cloud code with pointer structure, like this

var tarUser = { 
  __type: 'Pointer',
  className: '_User',
  objectId: 'insertObjectIDHere'
};