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:
- What is wrong with my code?
- How can I clean up my code starting with combining the "Parse.Push.send"?