2015-09-07 73 views
1

我已将所有用户的位置保存在安装对象中。我还有另一个名为locationObject的对象,它在当前用户发送当前位置时得到更新。当它发生时,我想将他当前的位置与所有其他保存的位置进行比较,并向附近的用户发送推送通知。这是我的代码,但这似乎并不奏效。无法在云代码中发送推送通知

//this code should run when the locationObject is updated 
Parse.Cloud.afterSave("locationObject", function (request) { 
    var geoPoint = request.object.get("myCurrentLocation"); 
    var pushQuery = new Parse.Query(Parse.Installation); 
    pushQuery.near("significantLocationUpdate", geoPoint); 
    pushQuery.limit(100); 
    pushQuery.find({ 
     success: function(results) { 
      if (results.length > 0) { 
       //console.log(JSON.stringify(results)); 
       for (i = 0; i < results.length; 1++) { 
        Parse.Push.send({ 
         where: pushQuery, 
         data: { 
          alert: "some user is nearby" 
         } 
        }, { 
         success: function() { 
          console.log("push was successfull") 
         }, 
         error: function(error) { 
          console.log("sending push failed")// Handle error 
         } 
        }); 
       } 
      } else { 
       console.log("failure"); 
      } 
     }, 
     error: function (error) { 
      console.log("error"); 
     } 
    }); 
}); 
+0

我可以看到一些问题 - 你有'1 ++',而不是'我'++在你的循环,你都在执行查询,并包括在你的推动和你不使用来自查询/循环的结果 - 或者只是在您的推送中包含“pushQuery”,或者使用结果生成单个推送消息。 – Paulw11

+0

感谢您的指导。 – shivamkaushik

回答

0

这就是我重构代码的方法。它可以工作。由于Paulw11

Parse.Cloud.afterSave("locationObject", function (request) { 
    var geoPoint = request.object.get("myCurrentLocation"); 
    var pushQuery = new Parse.Query(Parse.Installation); 
    pushQuery.near("significantLocationUpdate", geoPoint); 
    pushQuery.limit(100); 

    Parse.Push.send({ 
     where: pushQuery, 
     data: { 
      alert: "some user is nearby" 
     } 
    }, { 
     success: function() { 
      console.log("push was successful"); 
     }, 
     error: function(error) { 
      console.log("sending push failed")// Handle error 
     } 
    }); 
});