StuartM
StuartM

Reputation: 6813

Sending Parse Push with Cloud Code

I cannot find any documentation on Parse.Push used in Parse Cloud Code. The usage case that I see is like this...

  // Send the push notification to results of the query
  Parse.Push.send({
    where: pushQuery,
    data: {
      alert: message
    }
  }).then(function() {
      response.success("Push was sent successfully.")
  }, function(error) {
      response.error("Push failed to send with error: " + error.message);
  });

What I am trying to do is send a push notification if a recipient user is setup for notifications (i.e. has a valid Installation instance, associated to their user).

At the moment I create the query and pass that into the above with pushQuery. What I notice is that a Push is created in the Parse dashboard but the Pushes sent is 0.

Really I just want to create the Push if a user exists. I have created the query and can run this and return if I get results or not like this...

Parse.Cloud.define("sendTurnNotificationToUser", function(request, response) {
  var senderUser = request.user;
  var recipientUserId = request.params.recipientId;
  var message = request.params.message;

  // Validate the message text.
  // For example make sure it is under 140 characters
  if (message.length > 140) {
  // Truncate and add a ...
    message = message.substring(0, 137) + "...";
  }

  // Send the push.
  // Find devices associated with the recipient user
  var recipientUser = new Parse.User();
  recipientUser.id = recipientUserId;
  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo("user", recipientUser);

  pushQuery.find({
    success: function(results) {
      response.success("push user lookup was ok");
      response.success(results);
    },
    error: function() {
      response.error("push user lookup failed");
    }
  });

I could add the Parse.Push.send call to the success of the query. However the Parse.Push.send has a where clause and I do not know what is required there? I do not want to run the query twice.

Upvotes: 0

Views: 204

Answers (2)

Héctor Ramos
Héctor Ramos

Reputation: 9258

It seems like you may be overthinking this. There's no harm in sending a push notification to 0 installations as the push query will not match any recipients. I wouldn't worry too much about this and I wouldn't add such a pre-check to your code. It would add an unnecessary delay to your code and of course would result in the query being run twice.

If you want to do this anyway -- maybe you wish to keep your push logs free of clutter - you can indeed query over the Installation class to check if the query would have matched a set of installations, and if it does, you can then pass that same query to Parse.Push.send().

Yes, that will result in the query run twice, but that's to be expected, as you can't know how many objects will be matched without running the query.

Upvotes: 0

danh
danh

Reputation: 62686

You're on the right track. Push "advanced targeting" allows the app to push to installations resulting from a query. That's what the where clause is for...

// don't run find on the pushQuery.  set it up as you have it
// then, assuming it returns some installation(s)...

Parse.Push.send({ where: pushQuery, data: "hello" }).then(function(result) {
    response.success(result);
}, function(error) {
    response.error(error);
});

Incidentally, you can use createWithoutData on Parse.User as a shortcut ...

var recipient = Parse.User.createWithoutData(request.params.recipientId);

but the longer form you have should work, too.

Upvotes: 1

Related Questions