【问题标题】:Parse Cloud Code: Can you pass objects in a push notification?Parse Cloud Code:你能在推送通知中传递对象吗?
【发布时间】:2013-08-29 21:55:46
【问题描述】:

我正在创建一个类似于 Parse 提供的示例代码的推送通知:

Parse.Cloud.afterSave('Activity', function(request) {

    if (request.object.get("type") === ("comment") {

        var message = request.user.get('displayName') + ': ';
        message += request.object.get('content').trim();

        var query = new Parse.Query(Parse.Installation);
        query.equalTo('user', request.object.get("toUser"));

        Parse.Push.send({
            where:query,
            data: {
                alert: message,
                badge: 'Increment'
            }
        });
    }
});

我的问题是:在 Parse.Push.send 的数据区,我可以发送整个消息对象,其中 Message 是我创建的自定义类吗?如果是这样,那会是什么样子?

【问题讨论】:

  • 你弄明白了吗?我也需要这个。谢谢!

标签: javascript parsing object notifications push


【解决方案1】:

如果您已经创建了类并保存了一个对象,为什么不直接发送对象 ID 并在用户检索推送通知后异步查询它?

消息对象不需要浪费空间并通过推送发送,只需要一个指针。

我正在实现类似的东西,这是我计划使用的路线。

【讨论】:

    【解决方案2】:

    您可以将对象序列化为 JSON/XML 格式,然后在收到推送通知时对其进行反序列化。

    【讨论】:

      【解决方案3】:

      您不能直接发送对象。你会得到一个异常(几天前我遇到了这个问题,但没有写下异常的名称)。到目前为止,最好的答案是 BrentonGray88。

      我假设您没有使用 Android,因为您包含了徽章:“增量”值,但我会这样做:

      发送通知的 Android 代码:

      确保评论对象对发送评论的用户有一个pointer (_User) column。创建评论时,请在您的 Android 代码中包含 user.put("commentAuthor", ParseUser.getCurrentUser());,以便您始终可以访问创建评论的用户。

      现在您需要查询 Comment 以将其 objectId 发送到推送通知。

      ParseQuery<ParseObject> query = new ParseQuery<>("Comment");
      query.whereEqualTo("objectId", I AM NOT SURE WHAT CONDITION YOU WANT HERE);
      query.findInBackground((comment, e) -> {
          if (e == null) for (ParseObject commentObject: comment) {
      
             String recipientObjectId = commentObject.getParseObject("commentAuthor").getObjectId();
      
              final Map<String, Object> params = new HashMap<>();
              // This is to send the notification to the author of the Comment
              params.put("recipientObjectId", recipientObjectId);
      
              // This is so we can use values from the Comment in the notification
              params.put("commentObjectId", commentObject.getObjectId());
      
              // This is a required lined
              params.put("useMasterKey", true);
      
              ParseCloud.callFunctionInBackground("pushMessage", params, new FunctionCallback<String>() {
                  public void done(String result, ParseException e) {
                      if (e == null) {
                          Log.d(getClass().toString(), "ANNOUNCEMENT SUCCESS");
                      } else {
                          System.out.println(e);
                          Log.d(getClass().toString(), "ANNOUNCEMENT FAILURE");
                      }
                  }
              });
      
          }
      });
      

      现在在您的 Cloude 代码中进行查询:

        Parse.Cloud.define("pushMessage", function (request, response) {
            // Again, we are sending the notification to the author of the Comment
            var pushQuery = new Parse.Query(Parse.Installation);
            pushQuery.equalTo('user', request.params.get("recipientObjectId"));
      
            // We retrieve information from the Comment created by that author
            var commentQuery = new Parse.Query(Parse.Comment);
            commentQuery.equalTo('objectId', request.params.commentObjectId);
      
             commentQuery.get("commentObjectId", {
                 success: function(userObject) {
                     var displayName = userObject.get("displayName");
                     var content = userObject.get("content");
      
                      var message = displayName + ': ';
                      message += content.trim();
      
                      Parse.Push.send({
                      where: pushQuery,
                      data: {
                          alert: message
                      },
                      }, {
                          useMasterKey: true,
                          success: function () {
                              response.success("Success!");
                      },
                      error: function (error) {
                          response.error("Error! " + error.message);
                      }
                  });
      
                      console.log();
                  },
                  error: function(error) {
                      console.log("An error occured :(");
                  }
            });
      
        });
      

      抱歉,我不是最擅长 JavaScript,但我就是这样做的。祝你好运! :)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-05-17
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多