使用数组减少Parse Push Requests

时间:2014-10-13 08:39:55

标签: javascript parse-platform cloud-code

我目前正在使用Parse Cloud Code Background Job来通知用户在指定半径内找到新丢失的宠物。

例如宠物XYZ失踪了所有拥有Push Enabled并且在指定范围内的用户将获得新丢失宠物的推送通知。

我现在的主要问题是,如果失踪宠物附近有很多用户推送通知将超过API请求解析限制,因为我目前正在发送一个。

我怎么能改善这个?我想也许使用一个包含所有Users_Ids的数组应该收到通知(每个宠物的文字是相同的)

到目前为止,这是我的(工作)代码:

正在检查新宠物的主要工作职能

Parse.Cloud.job("pushRadius", function(request, response) {

    Parse.Cloud.useMasterKey();

    console.log('pushRadius');

    var Pet = Parse.Object.extend("Pet");
    var petQuery = new Parse.Query(Pet);
    petQuery.equalTo("initialPushSent", false);
    petQuery.equalTo("status", "missing");
    petQuery.equalTo("deleted", false);

    petQuery.find().then(function(pets) {
        console.log("found " + pets.length + " pets");
        var promises = [];

        _.each(pets, function(pet) {
            promises.push(checkUsersForPet(pet));
            Parse.Cloud.useMasterKey();
            pet.set("initialPushSent", true);
            pet.save();
        });

        return Parse.Promise.when(promises);
    }).then(function(obj) {
        console.log('All Pets checked');
    }, function(error) {
        console.log('whoops, something went wrong ' + error.message);
    });

});

在主要功能中为每个找到的宠物获取范围内的用户

function checkUsersForPet(pet){
    console.log("check Pet" + pet.id);

    var petLocation = pet.get("lastSeenLocation");
    var query = new Parse.Query("User");
    query.withinKilometers("lastLocation", petLocation, 50);
    query.equalTo("pushActivated", true)

    query.find().then(function(users) {
        console.log("found " + users.length + " users for pet " + pet.id);
        var promises = [];

        _.each(users, function(user) {

            if (petLocation.kilometersTo(user.get("lastLocation")) <= user.get("pushRadius")) {
                promises.push(sendUsersPush(user, pet));        
            } 

        });

        return Parse.Promise.when(promises);
    }).then(function(obj) {
        console.log('All Users checked');
    }, function(error) {
        console.log('whoops, something went wrong ' + error.message);
    });

}

发送用户通知并为缺失的宠物添加新的通知对象 推送发送应使用用户通道数组完成,仅计为1 API请求

function sendUsersPush(user, pet){
    console.log("send user "+ user.id +" push for pet" + pet.id)

    Parse.Push.send({
        channels: ["user_" + user.id],
        data: {
            badge: "Increment",
            alert: "Neues vermisstes Tier " + pet.get("title") + " im Umkreis"
        }
    }, {
        success: function() {
            console.log("Push sent to user" + user.id + "for pet " + pet.id);
        },
        error: function(error) {
            console.error("Got an error " + error.code + " : " + error.message);
        }
    });


    var Notification = Parse.Object.extend("Notification");
    var notification = new Notification();

    notification.set("read", false);
    notification.set("user", user);
    notification.set("pet", pet);
    notification.set("type", "new");
    notification.set("petName", pet.get("title"));

    notification.save(null, {
        success: function(notification) {

        },
        error: function(notification, error) {

        }
    });

}

1 个答案:

答案 0 :(得分:0)

我可能太迟回答你的问题,但这可能会帮助别人。您可以在Parse中将推送通知发送到查询。像这样:

&#13;
&#13;
var dogOwnerArray = new Array();
for (var i = 0; i < results.length; i++) 
{
	var userStatus = results[i];
	var dogOwner = userStatus.get("user");
	console.log(JSON.stringify(dogOwner));
	dogOwnerArray.push(dogOwner);
}

var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.containedIn("user", dogOwnerArray);

Parse.Push.send({
    where: pushQuery,
    data: 
    {
       alert: "Looks like HyreCar app has been closed. Please reopen it."
   }
}, 
{
    success: function() 
    {
     response.success("Success");
 },
 error: function(error) 
 {
  response.error("Error")
}
});
&#13;
&#13;
&#13;

其中&#39;结果&#39;是上一个查询的结果。