javascript - Decrease Parse Push Requests with Array -
javascript - Decrease Parse Push Requests with Array -
i'm using parse cloud code background job notify users when new missing pet found in specified radius.
for illustration pet xyz gone missing users have force enabled , within specified radius getting force notification of new missing pet.
my main problem is, if there lot of users near missing pet force notifications exceed api request limit of parse since i'm sending them 1 @ time.
how improve this? thinking maybe using array users_ids should getting notification (text every pet same)
here's (working) code far:
main job function checking new pets
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(); }); homecoming parse.promise.when(promises); }).then(function(obj) { console.log('all pets checked'); }, function(error) { console.log('whoops, went wrong ' + error.message); }); });
get users in range each found pet in main function
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 pet " + pet.id); var promises = []; _.each(users, function(user) { if (petlocation.kilometersto(user.get("lastlocation")) <= user.get("pushradius")) { promises.push(senduserspush(user, pet)); } }); homecoming parse.promise.when(promises); }).then(function(obj) { console.log('all users checked'); }, function(error) { console.log('whoops, went wrong ' + error.message); }); }
send user notification , add together new notification object missing pet force send should done array of user channels count 1 api request
function senduserspush(user, pet){ console.log("send user "+ user.id +" force 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 user" + user.id + "for pet " + pet.id); }, error: function(error) { console.error("got 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) { } }); }
i probabaly answering question late, might help else. can send force notifications query in parse. this:
class="snippet-code-js lang-js prettyprint-override">var dogownerarray = new array(); (var = 0; < 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 hyrecar app has been closed. please reopen it." } }, { success: function() { response.success("success"); }, error: function(error) { response.error("error") } });
where 'results' results previous query.
javascript parse.com cloud-code
Comments
Post a Comment