Parse Push发送方法问题

时间:2017-05-30 00:42:33

标签: parse-platform

我很难遍历下面的有效负载列表,并为我的朋友列表调用具有不同值的Parse.push.send方法。我相信我应该使用承诺,但我不知道如何去做。任何帮助将不胜感激。

Parse.Cloud.define('senderChannel', function(request, response) {
  /* getting parameters */  
  var params = request.params;
  var senderId = params.senderId;
  var sharedValue = params.sharedValueList;
  var recipientList = params.recipientList;
  var friendCount = params.friendCount;

  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo("deviceType", "android");

  var payloadList = [];

  if(friendCount > 1) {
    /* if friendCount is greater than 1 build custom payload for each friend */   
    var friendList = recipientList.split(',');
    var valueList = sharedValue.split(',');

    var friendListArray = [];

    for(var item in friendList) {
        friendListArray.push(item);
    }

    for(var value in valueList) {
        var payload = {"title": senderId, "alert": value};
        payloadList.push(payload);
    }

    pushQuery.containedIn("device_id", friendListArray);

    } else {

        pushQuery.equalTo("device_id", recipientList);
        var payload = {"title": senderId, "alert": sharedValue};
        payloadList.push(payload);
   }

   payloadList.forEach(function(payload, index){
       /* Send push to every friend */
       Parse.Push.send({
           where: pushQuery,
           data: payload,
       }, { success: function() {
           console.log("#### PUSH OK");
       }, error: function(error) {
           console.log("#### PUSH ERROR" + error.message);
       }, useMasterKey: true});

       response.success('success');
   });
});

1 个答案:

答案 0 :(得分:0)

我已经能够通过将push函数添加到数组并使用Parse Promise来解决我的问题。以下是代码

payloadList.forEach(function(payload, index){

      promises.push(sendPush(payload));

   });

  Parse.Promise.when(promises).then(function(){
      console.log("All pushes have completed !!!");
  });

   function sendPush(payloadMessage){

       Parse.Push.send({
           where: pushQuery,
           data: payloadMessage,
       }, { success: function() {
           console.log("#### PUSH OK");
       }, error: function(error) {
           console.log("#### PUSH ERROR" + error.message);
       }, useMasterKey: true});

       response.success('success');
   }