【问题标题】:Parse send notification when object modified对象修改时解析发送通知
【发布时间】:2016-09-08 14:15:08
【问题描述】:

当某个对象被修改时,我正在尝试通过 Parse Cloud Code 发送推送通知 - “脏”

我想我快到了,但收到一个错误,因为我认为是在创建一个新用户而不是查询一个。

Parse.Cloud.beforeSave("Fact", function(request, response) {
  var dirtyKeys = request.object.dirtyKeys();
  for (var i = 0; i < dirtyKeys.length; ++i) {
    var dirtyKey = dirtyKeys[i];
    if (dirtyKey === "isValid") {

      //send push
      // Creates a pointer to _User with object id of userId

      var targetUser = new Parse.User();
      // targetUser.id = userId;
      targetUser.id = request.object.userID;

      var query = new Parse.Query(Parse.Installation);
      query.equalTo('user', targetUser);

      Parse.Push.send({
        where: query,
        data: {
          alert: "Your Fact was approved :)"
        }
      });

      return;
    }
  }
  response.success();
});

我发现 this post 与我的问题有关。我现在的问题是如何将用户查询集成到我的 beforeSave 块中。理想情况下,我会为用户查询创建另一个函数并将其放在我的 beforeSave 块中。

**5/14 更新 我接受了@toddg 的建议并修复了之前的保存。这是我正在尝试做的事情和新错误的更清晰的图片。

【问题讨论】:

  • 通知代码最好放在保存块之后。在上面的代码中 response.success() 可能会在发送推送通知之前运行。还有回报;阻止它通知 response.success() 或 response.error()。我建议将通知内容移至 afterSave
  • 顺便问一下你遇到了什么错误?
  • 我收到此错误 - “无法创建指向未保存 ParseObject 的指针”

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


【解决方案1】:

在我进入代码之前,有几点(正如 @Subash 在 cmets 中指出的那样):

  1. Parse.Push.send 是一个异步操作,因此您需要确保在推送发送完成后调用 response.success()。我将使用 Promises 来处理这个问题,因为我认为它们比回调更灵活。如果您不熟悉,请阅读它们here
  2. if 语句中的 return 可能会阻止调用 response.success()

这是我推荐的做法:

Parse.Cloud.beforeSave("Fact", function(request, response) {

  // Keep track of whether we need to send the push notification
  var shouldPushBeSent = false;

  var dirtyKeys = request.object.dirtyKeys();
  for (var i = 0; i < dirtyKeys.length; ++i) {
    var dirtyKey = dirtyKeys[i];

    if (dirtyKey === "isValid") {
      shouldPushBeSent = true;
    }
  }

  if (shouldPushBeSent) {

      //send push
      // Creates a pointer to _User with object id of userId

      var targetUser = new Parse.User();
      // targetUser.id = userId;
      targetUser.id = request.object.userId;

      var query = new Parse.Query(Parse.Installation);

      // We want to pass the User object to the query rather than the UserId
      query.equalTo('user', targetUser);
      Parse.Push.send({
        where: query, // Set our Installation query
        data: {
          alert: "Your fact was approved"
        }
      }).then(function(){

        // Now we know the push notification was successfully sent
        response.success();
      }, function(error){

        // There was an error sending the push notification
        response.error("We had an error sending push: " + error);
      });
  } else {
    // We don't need to send the push notification.
    response.success();
  }
});

顺便说一句,我假设您的安装类中有一个列,用于跟踪与每个安装关联的用户。

【讨论】:

  • 感谢您的及时回复!这看起来很棒,很快就会实现它。我确实有一个安装类,但那里没有用户 ID 列。那是我需要添加的吗?然后我必须将该用户 ID 从 iOS 应用程序发送到 Parse?
  • 对于其他尝试添加列安装的人来说,非常简单。这是我的做法let user = PFUser.currentUser() let currentInstallation = PFInstallation.currentInstallation() currentInstallation.setObject((user?.objectId!)!, forKey: "userID") currentInstallation.saveInBackground()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多