Parse Cloud: Send Push for Single User

I am using Parse and I cannot get this method to work.

The idea is to send push to one user with the already known user ID from any platform in the "Cloud code".

The requests I tried without clicking on this user (in fact, no push was delivered to anyone)

  • PFInstallation via user pointer:

    var targetUser = new Parse.User();

    targetUser.id = userID;

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

    query.equalTo('user', targetUser);

  • Request 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.

+6
source share
3 answers

This works well for me, but you should associate 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); } }); 
+10
source

You can join Parse.Session and Parse.Installation with the user pointer as the primary key of the session, the next "installationId" as the connection key.

 var PushSend = function(payload, dbContact) { var querySession = new Parse.Query(Parse.Session); querySession.equalTo("user", dbContact); querySession.descending("updatedAt"); querySession.first({ useMasterKey: true }) .then(function(dbSession) { var installId = dbSession.get("installationId"); var installQuery = new Parse.Query(Parse.Installation); installQuery.equalTo("installationId", installId); Parse.Push.send( { where: installQuery, data: { alert: payload } }, { useMasterKey: true} ) .then(function() { // "success" }, function(error) { // "failed" } ); }, function(error) { // "failed" } ); } 

The above cloud function takes as arguments:

  • payload text string
  • destination destination pointer (e.g. previously requested from Parse.User)
0
source

I use the first of the two methods you describe, and it works great.

Just make sure you use objectId and not the username for targetUser.id .

Example:

 targetUser.id = 'Qhao1j22j'; //Never hard code like this though. 
-2
source

Source: https://habr.com/ru/post/975837/


All Articles