简体   繁体   English

使用云代码发送解析推送

[英]Sending Parse Push with Cloud Code

I cannot find any documentation on Parse.Push used in Parse Cloud Code . 我在Parse Cloud Code找不到有关Parse.Push任何文档。 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 (ie has a valid Installation instance, associated to their user). 我要尝试执行的操作是,如果为接收者设置了通知用户(即具有与其用户相关联的有效Installation实例),则发送推送通知。

At the moment I create the query and pass that into the above with pushQuery. 目前,我创建了查询并将其通过pushQuery传递到上面。 What I notice is that a Push is created in the Parse dashboard but the Pushes sent is 0. 我注意到的是,在“解析”面板中创建了一个Push,但是发送的Push是0。

Really I just want to create the Push if a user exists. 真的,我只想在存在用户的情况下创建Push。 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. 我可以将Parse.Push.send调用添加到查询的成功中。 However the Parse.Push.send has a where clause and I do not know what is required there? 但是, Parse.Push.send有一个where子句,我不知道那里需要什么? I do not want to run the query twice. 我不想两次运行查询。

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... 这就是where子句的用途...

// 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 ... 顺便说一句,您可以在Parse.User上使用createWithoutData作为快捷方式...

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

but the longer form you have should work, too. 但是较长的表格也可以使用。

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. 将推送通知发送到0个安装没有任何危害,因为推送查询将不匹配任何收件人。 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() . 如果仍然要执行此操作-也许您希望保持推送日志的整洁-您确实可以查询Installation类,以检查查询是否与一组安装匹配,如果可以,则可以将相同的查询传递给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. 是的,这将导致查询运行两次,但这是可以预期的,因为如果不运行查询,您将不知道将匹配多少个对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM