【问题标题】:Parse Cloud: Send Push to a single userParse Cloud:向单个用户发送推送
【发布时间】:2014-11-20 08:03:57
【问题描述】:

我正在使用 Parse,但无法使用此方法。

这个想法是从“云代码”中的任何平台向具有已知用户标识符的单个用户发送推送。

我尝试过的查询没有推送给那个用户(实际上没有推送给任何用户)

  • PF通过用户指针安装:

    var targetUser = new Parse.User();

    targetUser.id = userID;

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

    query.equalTo('user', targetUser);

  • 在 PFUser 本身中查询

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

    query.equalTo('objectId', userID);

方法的完整代码:

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

});

感谢您的时间和帮助。

【问题讨论】:

  • 你如何从android调用云方法??

标签: parse-platform cloud push where


【解决方案1】:

您可以加入 Parse.Session 和 Parse.Installation 以用户指针作为会话主键,下一个“installationId”作为加入键。

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

上面的云函数作为参数:

  1. 有效负载文本字符串
  2. 一个目标联系人指针(例如,您之前曾从 Parse.User 查询过)

【讨论】:

    【解决方案2】:

    这对我来说很有效,但您必须先将您的用户与您的安装链接:

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

    【讨论】:

    • 我添加了一个指向 User.installation 的指针,链接看起来很好。我可以点击安装并显示安装。但是还是发不出去。
    • 是的!祝福你的心。
    • 正在尝试这样做并编写相同的代码,但我不明白您如何链接用户和安装?我应该从客户端执行此操作还是有办法从 Parse 仪表板执行此操作?
    • 首先,您必须在用户表(仪表板)中创建一列作为指向安装的指针。用户注册您的应用后,您必须使用从 Parse(客户端)获得的安装更新用户表。
    • 帮助!尝试您的代码后,它给了我这个.. 错误 102:$inQuery parse.com/api/collections/collection.go:389 inQueryVisitor.runChildQuery parse.com/api/collections/collection.go:363 inQueryVisitor 的错误类型。访问 :171 (*inQueryVisitor)。访问 parse.com/api/types/query/visitor.go:27 访问 parse.com/api/collections/collection.go:403 (*Collection).resolveInQueries parse.com /api/collections/collection.go:550 (*Collection).PrepareQuery parse.com/api/collections/installation_collection.go:740 (*InstallationCollection).Iterate parse.com/push/expansion/expansion_job.go:362跨度>
    【解决方案3】:

    我使用的是你提到的两种方法中的第一种,效果很好。

    请确保您使用 objectId 而不是 targetUser.id 的用户名。

    例如:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多