0

I'm currently using a Parse Cloud Code Background Job to notify users when a new missing pet is found in their specified radius.

For example Pet XYZ is gone missing all users who have Push Enabled and are within their specified radius will be getting the push notification of the new missing pet.

My main problem now is, that if there is a lot of users near the missing pet that the push notifications will exceed the API Request limit of parse since I'm currently sending them one at a time.

How could I improve this? I was thinking maybe using an array with all the Users_Ids which should be getting the notification (Text for every Pet is the same)

Here's my (working) code so far:

Main Job Function which is checking for 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();
        });

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

});

Get Users in range for each found pet in the 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 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);
    });

}

Send user notification and add a new Notification Object for the missing pet The Push Send should be done with an Array of User Channels to only count as 1 API Request

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) {

        }
    });

}
Patrick
  • 45
  • 1
  • 1
  • 7

1 Answers1

0

I am probabaly answering your question too late, but this might help someone else. You can send Push notifications to a query in Parse. Something like this:

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")
}
});

Where 'results' are the results from a previous query.

Ichibanpanda
  • 906
  • 7
  • 8