2015-09-07 69 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 
     } 
    }); 
});