2014-11-25 33 views
2

正如文檔所述 - 「JavaScript SDK當前不支持修改安裝對象」,但是如何創建這些對象?是否可以從雲代碼創建安裝對象?使用解析從Cloud代碼創建安裝對象

對於前:

Parse.Cloud.define("registerForNotifications", function(request,response) { 

var token = request.params.deviceToken; 
var deviceType = request.params.deviceType; 
var user = request.user; 

var installation = new Parse.Object("Installation"); 
if (installation) { 
    installation.set("deviceToken",token); 
    ... so on.. 
    installation.save(); 
} 
}); 
請問

這項工作?謝謝。

+0

是的,這會工作,我測試了它。很好的機會,不要添加依賴來解析你的應用程序內的SDK。訂閱/發送推送可通過REST API和雲代碼獲得 – alex 2014-11-25 13:50:46

+1

UPD:因此,它可以工作,但它會創建「安裝」解析對象,而不是連接到安裝對象,用於推送:)因此,您需要使POST請求直接https://api.parse.com/1/installations – alex 2014-11-25 14:14:52

+0

您可以在不使用API​​的情況下使用您的代碼,您應該使用(「_Installation」)替換(「安裝」) – 2015-10-24 12:52:43

回答

1

一些示例:

//The following Cloud Function expects two parameters: the channel to be added to this user's installations, and the object id for the user. 

//It assumes that each installation has a `Pointer <_User>` back to the original user. 


//... 
Parse.Cloud.define("subscribeToChannel", function(request, response) { 
    var channelName = request.params.channel; 
    var userId = request.params.userId; 

    if (!channelName) { 
    response.error("Missing parameter: channel") 
    return; 
    } 

    if (!userId) { 
    response.error("Missing parameter: userId") 
    return; 
    } 

    // Create a Pointer to this user based on their object id 
    var user = new Parse.User(); 
    user.id = userId; 

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

     // Save all the installations 
     Parse.Object.saveAll(installations, { 
     success: function(installations) { 
      // All the installations were saved. 
      response.success("All the installations were updated with this channel."); 
     }, 
     error: function(error) { 
      // An error occurred while saving one of the objects. 
      console.error(error); 
      response.error("An error occurred while updating this user's installations.") 
     }, 
     }); 
    }, 
    error: function(error) { 
     console.error(error); 
     response.error("An error occurred while looking up this user's installations.") 
    } 
    }); 
});