1

我正在尝试使用解析云将新通道添加到其他用户的解析安装中。这是解析云代码:

  Parse.Cloud.define("subscribeToChannel", function(request, response) {
  var channelName = request.params.channel;
  var ids = request.params.ids;//this is an array of ids to change
  var _ = require("underscore");
  _.each(ids, function(id) {
  // Create a Pointer to this user based on their object id
    var user = new Parse.User();
    user.id = id;
    // Need the Master Key to update Installations
    Parse.Cloud.useMasterKey(); 
    // A user might have more than one Installation
    var query = new Parse.Query(Parse.Installation);
    query.equalTo("User", user); // Match Installations with a pointer to this User
    query.find({
      success: function(installations){
    for (var i = 0; i < installations.length; i++){
      // Add the channel to all the installations for this user
      installations[i].addUnique("channels", channelName);
    }
    // Save all the installations
    Parse.Object.saveAll(installations, {
      success: function(installations) {
      },
      error: function(error) {
        // An error occurred while saving one of the objects.
        console.error(error);
      }
    });
      },
      error: function(error) {
    console.error(error);
      }
    });
  });

这就是我为每个用户设置第一次安装的方式:

ParseInstallation inst = ParseInstallation.getCurrentInstallation();
inst.put("User", ParseUser.getCurrentUser());

我发送的 id 数组是一个 JSONArray,其中包含一个字符串 id 列表。JSONArray 正确获取和迭代。但无论我尝试了多少次,它似乎都不起作用。谢谢!

4

2 回答 2

2

我的错误是我没有以正确的方式保存频道。在与 Parse 的开发团队进行了长时间的交谈后,我得以实现。这是代码:

Parse.Cloud.define("subscribeToChannel", function(request, response) {
var channelName = request.params.channel; //Channel the user will be subscribed to
var ids = request.params.ids; //Id of the users
var completed = true;
var size = 0;
var pased = 0;
var loops = 0;
var idStrings = "";
var _ = require("underscore");
if (!channelName) {
response.error("Missing parameter: channel");
return;
}
if (!ids) {
response.error("Missing parameter: ids");
return;
}
var query = new Parse.Query(Parse.Installation);
var user = new Parse.User();
var changedObjects = [];
var pointerArray = [];
_.each(ids, function(id){
pointerArray.push({"__type":"Pointer","className":"_User","objectId":id});
});

// Need the Master Key to update Installations
Parse.Cloud.useMasterKey();
query.containedIn("User",pointerArray);
query.find({
success: function(installations){
for (var i = 0; i < installations.length; i++){
// Add the channel to all the installations for this user
installations[i].addUnique("channels", channelName); //Add the channel to the installation
changedObjects.push(installations[i]); //Add the installation to be saved later on!
}

//Saving all the installations
Parse.Object.saveAll(changedObjects, { 
success: function(installations) {
response.success();
},
error: function(error) {
// An error occurred while saving one of the objects.
response.error(error);
}
});
},
error: function(error) {
response.error(error);
}
});
});
于 2014-04-25T08:36:38.220 回答
1

我刚刚找到了您的问题,因为我正在尝试使用 CloudCode 进行相同的操作,但至少目前看来是不可能的。Parse 团队表示:“JavaScript SDK 目前不支持修改安装对象。请查看 iOS、Android 或 REST 推送指南”。下面是链接:

parse.com/docs/push_guide#setup/JavaScript

除此之外,还有一些 JavaScript SDK 不能做的操作:

  1. JavaScript SDK 目前不支持订阅频道以发送/接收推送通知
  2. JavaScript SDK 目前不支持接收推送

但另一方面,您可以发送推送通知。然后,当我正在开发一个 Android 原生应用程序时,我被迫“混合” ParseClode 和“Android 代码”以完成通知推送功能。

我希望它会帮助你

问候

于 2014-04-24T07:32:06.927 回答