【问题标题】:Sending Parse Push with Cloud Code使用云代码发送解析推送
【发布时间】:2026-01-24 12:30:01
【问题描述】:

我在Parse Cloud Code 中使用的Parse.Push 上找不到任何文档。我看到的用例是这样的……

  // 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);
  });

如果收件人用户设置了通知(即有一个有效的安装实例,与他们的用户相关联),我想要做的是发送一个推送通知。

目前我创建查询并使用 pushQuery 将其传递到上面。我注意到在 Parse 仪表板中创建了一个推送,但发送的推送为 0。

真的,如果用户存在,我只想创建推送。我已经创建了查询,并且可以运行它并在我得到结果时返回......

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");
    }
  });

我可以将 Parse.Push.send 调用添加到查询成功。但是 Parse.Push.send 有一个 where 子句,我不知道那里需要什么?我不想运行查询两次。

【问题讨论】:

    标签: ios parse-platform parse-cloud-code


    【解决方案1】:

    你在正确的轨道上。推送“高级定位”允许应用推送到查询产生的安装。这就是 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);
    });
    

    顺便说一句,您可以在 Parse.User 上使用 createWithoutData 作为快捷方式...

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

    但你拥有的更长的表格也应该可以工作。

    【讨论】:

    • 嘿,丹。抱歉,也许我不清楚我的 parse.push 位从第一个代码块开始工作
    • 哎呀.. 早点挖掘了。问题是即使查询结果为 0 也会创建推送,所以我只想在有结果时创建推送。所以我可以先进行查询并成功调用推送部分,但这不意味着同一个查询会运行两次吗??
    • 毫无疑问,push 运行 where 查询并仅在查询返回安装时采取进一步的操作(发送推送)。我认为这正是您想要的逻辑。
    • 实际上,无论安装查询返回 0 还是 > 0,都会创建推送。
    • 哦。我不会担心的。将其视为报告,而不是推动。这只是一个没有发送推送的报告(而不是什么都没有发送,这可能会被解释为失败)。
    【解决方案2】:

    看来你可能想多了。向 0 个安装发送推送通知没有任何害处,因为推送查询不会匹配任何收件人。我不会太担心这一点,也不会在您的代码中添加这样的预检查。它会给您的代码增加不必要的延迟,当然会导致查询运行两次。

    如果你还是想这样做——也许你希望让你的推送日志保持整洁——你确实可以查询 Installation 类来检查查询是否匹配一组安装,如果匹配,然后您可以将相同的查询传递给Parse.Push.send()

    是的,这将导致查询运行两次,但这是意料之中的,因为如果不运行查询,您无法知道将匹配多少对象。

    【讨论】:

    • 赫克托你就是男人!这正是我的想法......我想我不想过度混乱仪表板上的推送信息,因为发送给 0 人的推送不是推送......它无用的数据:) 我在想我可能会做一个查询计数而不是返回稍微快一点,然后以这种方式处理结果?
    • 强烈建议不要进行计数查询,它们不会更快。请记住,常规的非推送查询只需要找到前 100 个匹配的对象并且可以提前返回,而计数必须扫描整个表才能返回准确的答案。
    最近更新 更多