解析云代码函数get deviceToken按用户名

时间:2015-12-10 13:11:36

标签: javascript session parse-platform cloud-code

我是整个云代码的新手,我在获取一些数据时遇到了一些麻烦。 所以我基本上想要的是一个函数,通过给它一个位于Parse.User数据库中的“username”,它将返回用户的objectId,然后我将用它来定位用户的会话在Parse.Session,从那里我将获得installationId,然后我将在Parse.Installation中使用它来获取设备令牌。

  • 注意:我编写了一个函数,每位用户只能激活1个会话。

  • 我的问题: Parse.Session的查询结果只包含3个对象,并且不包含installationId,因此我无法找出安装ID是什么,然后用它来搜索Parse.Installation来获取设备令牌。 / p>

输入示例:

Input: 
{"from":"user1",
"msg":"hello",
"title":"Whatever title",
"to":"user2"}

以下是我目前使用的现有代码无效。

Parse.Cloud.define('gcm', function(request,response){

   var username = request.params.to;
   console.log("usr "+username);
   var userQuery = new Parse.Query(Parse.User);
   userQuery.equalTo('username', username);
   userQuery.find({
    success: function(results){
     var objectId = results[0].id;
     console.log("obj id "+objectId);
     var user = new Parse.User();

   //Set your id to desired user object id
     user.id = objectId;


     var sessionQuery = new Parse.Query(Parse.Object.extend('Session'));
     sessionQuery.include(user);
     sessionQuery.find({
         success: function(results1){
         console.log("result classname type: "+typeof(results1[0]));

         var installId = results1[0].installationId ; //here is the value which I want from the result, but the object is type of _Session which does not have installationId.
         console.log("inst id "+installId);
         var InstallationQuery = new Parse.Query(Parse.Installation);
         InstallationQuery.equalTo('installationId',installId);
         InstallationQuery.find({
              success: function(results2){
                   var deviceToken = results2[0].get("deviceToken");
                   console.log("token "+deviceToken);
                   Parse.Cloud.httpRequest({
            method: "POST",
            url: " https://gcm-http.googleapis.com/gcm/send",
            headers: {'Authorization' : 'key=AIzaSyDj4ISkLW7CzAQEQEhTsq3JYZK5OP8tSzY',
            'Content-Type' : 'application/json'},
                        body: { 
                "data": 
                {
                        "title": request.params.title,
                        "msg": request.params.msg
                },
                "to" : deviceToken
                  },
            success: function(httpResponse) {
                response.success("Message Sent!");
                        console.log(httpResponse.text);
                },
                error: function(httpResponse) {
                response.error("Error, Something went wrong.");
                        console.log("error 4: " + httpResponse.status);
                }
        });

              },
              error: function(error) {
              //error
              console.log("error 3: " + error);
              }
              });

         },
         error: function(error) {
         //error
          console.log("error 2: " + error);
         }
         });
     },
     error: function(error) {
     //error
     console.log("error 1: " + error);
    }
  });


});

0 个答案:

没有答案