Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse Cloud: Send Push to a single user

I'm using Parse and I can't make this method to work.

The idea is to send push to a single user with an already known user identifier from any platform within 'Cloud code'.

Queries I've tried without push being delivered to that user (actually no push was delivered to any user)

  • PFInstallation through the user pointer:

    var targetUser = new Parse.User();

    targetUser.id = userID;

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

    query.equalTo('user', targetUser);

  • Query in PFUser itself

    var query = new Parse.Query(Parse.User);

    query.equalTo('objectId', userID);

Full code for the method:

Parse.Cloud.define("sendPushContactUser", function(request, response) {

// Get parameters
var userID       = request.params.userID; // I'm obviously passing this parameter in the method call


// Query
/* ------ query = Any of the above options ------ */


Parse.Push.send({
    where: query,
    data: {
        title   : "New message",
        alert   : "User message",   
        badge   : "Increment",
        sound   : "soundNotifUserContact.caf",
        "params" : {
            "pushType"  : 2,
            "userID"    : userID
        }
    }
}, {
    success: function() {
        response.success();
    },
    error: function(error) {
        response.error(error);
    }
    });

});

Thanks for your time and help.

like image 337
Sento Avatar asked Sep 25 '14 13:09

Sento


1 Answers

This works well for me, but you have to link your users with your installations before:

var query = new Parse.Query(Parse.User);
query.equalTo('username', 'Sento');
// Find devices associated with these users
var pushQuery = new Parse.Query(Parse.Installation);
// need to have users linked to installations
pushQuery.matchesQuery('user', query);


Parse.Push.send({
    where: pushQuery,
    data: {
        aps: {
            alert: "Test",
            sound: ""
        }
    }
}, {
    success: function () {
        response.success("Hello world!");
    },
    error: function (error) {
        response.error(error);
    }
});
like image 129
mistapink Avatar answered Sep 23 '22 15:09

mistapink