Parse Cloud: Send Push to a single user

情到浓时终转凉″ 提交于 2019-12-18 12:35:48

问题


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.


回答1:


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



回答2:


You can join Parse.Session and Parse.Installation with a user pointer as the Session primary key, next "installationId" as the join 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 :

  1. the payload text string
  2. a destination contact pointer (you have for example previously queried from Parse.User)



回答3:


I am using the first of the two methods that you mentioned, and it works fine.

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

Ex:

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


来源:https://stackoverflow.com/questions/26040437/parse-cloud-send-push-to-a-single-user

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!