我正在尝试使用解析云向其他用户的解析安装添加新频道。 这是Parse Cloud Code:
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正确获取并迭代。 但不管我试过多少次,它似乎都没有用。 谢谢!
答案 0 :(得分: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);
}
});
});
答案 1 :(得分:1)
我刚刚找到了你的问题,因为我正在尝试使用CloudCode进行相同的操作,但似乎不可能,至少目前是这样。 根据Parse团队的说法:&#34; JavaScript SDK目前不支持修改安装对象。看一下iOS,Android或REST Push指南&#34;。 下面是链接:
parse.com/docs/push_guide#setup/JavaScript
此外,还有其他JavaScript SDK无法执行的操作:
但另一方面,您可以发送推送通知。 然后,当我开发Android原生应用程序时,我被迫&#34;混合&#34; ParseClode和&#34; Android代码&#34;为了完成通知推送功能。
我希望它会帮助你
此致