0
votes

Currently I am trying to add push notifications to my app, specifically a push notification to two different users which is triggered by a third user.

The cloud code below works fine except for the fact that push notifications aren't working. I want two push notifications set to both the winner and loser users when this function is triggered client side by the third user.

Parse.Cloud.define('addWinsLosses', function(request,response) { 

    Parse.Cloud.useMasterKey(); 

    var winnerUserId = request.params.winnerUserId;
    var loserUserId = request.params.loserUserId;

    var User = Parse.Object.extend('_User');
    var winner = new User({ objectId: winnerUserId });
    var loser = new User({ objectId: loserUserId });

    console.log(JSON.stringify(winner));
    console.log(JSON.stringify(loser));

    winner.increment('wins');
    loser.increment('losses');

    var pushQuery = new Parse.Query(Parse.Installation);

    pushQuery.equalTo('user', winner)

    Parse.Push.send({
        where: pushQuery
        data: {
            alert: "You Won!",
            badge: "increment"
        }
    }, {
        success: function () {
            response.success();
        },
        error: function (error) {
            response.error(error);
        }
    })

    var pushQuery = new Parse.Query(Parse.Installation);

    pushQuery.equalTo('user', loser)

    Parse.Push.send({
        where: pushQuery
        data: {
            alert: "You lost.",
            badge: 'increment'
        }
    }, {
        success: function () {
            response.success();
        },
        error: function (error) {
            response.error(error);
        }
    })

    Parse.Promise.when([winner.save(), loser.save()]).then(function () {
        // both saves have completed
        response.success();
    }, function (error) {
        // one or more errors
        console.log("Error in addWinsLosses : " + error.message);
        response.error(error);
    });
});

I have added the relationship needed:

let installation = PFInstallation.currentInstallation()
            installation["user"] = PFUser.currentUser()
            installation.saveEventually()

This should be a pointer to a PFUser which is then compared to another pointer to a PFUser in my cloud code, either "winner" or "loser". The message is different for the two users.

So a few questions:

  1. What is wrong with my code?
  2. How can I clean up my code starting with combining the "Parse.Push.send"?
1
Consider just setting the user id (with some prefix) as a channel - Wain

1 Answers

0
votes

You should probably only be calling response.error() and response.success() in the last promise callback. Otherwise, the cloud function will almost certainly return prematurely once one of the push notifications are sent, or when the user objects finish saving (whichever finishes first).

I would try removing the success/error callbacks from your usage of Parse.Push.send, and instead add the promise objects returned by the function to the call to Parse.Promise.when:

var winnerPushPromise = Parse.Push.send({
    where: pushQuery
    data: {
        alert: "You Won!",
        badge: "increment"
    }
})

...

var loserPushPromise = Parse.Push.send({
    where: pushQuery
    data: {
        alert: "You lost.",
        badge: 'increment'
    }
})

var promises = [winnerPushPromise, loserPushPromise, winner.save(), loser.save()]

Parse.Promise.when(promises).then(function () {
...

EDIT

I just noticed an issue with your user objects that might be causing problems with your query - try changing 'objectId' to 'id'.