【问题标题】:Send a push notification on a single device through heroku parse server通过 heroku 解析服务器在单个设备上发送推送通知
【发布时间】:2017-12-29 23:00:40
【问题描述】:

我正在使用 heroku 解析服务器应用程序,并尝试根据 mongodb 中可用的用户数据向单个设备发送推送通知。我正在使用如下所示的云功能:

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

var user = request.user;
var params = request.params;
var someKey = params.someKey
var data = params.data

var pushQuery = new Parse.Query(Parse.Installation);
pushQuery.equalTo('deviceType', 'ios'); // targeting iOS devices only
pushQuery.equalTo("someKey", someKey)

Parse.Push.send({
    where: pushQuery, // Set our Installation query
    data: data
}, { success: function() {
    console.log("#### PUSH OK");
}, error: function(error) {
    console.log("#### PUSH ERROR" + error.message);
}, useMasterKey: true});

response.success('success');
});

我正在点击 curl 请求,以检查推送通知,例如 -

curl \
-X POST \
-H "X-Parse-Application-Id: myAppId" \
-H "X-Parse-Master-Key: masterkey" \
-H "Content-Type: application/json" \
-d { "where": { "user_id": "cedsGn98ja" }, "data": { "alert": "Hello, Parse!"  } }

http://myapp.herokuapp.com/parse/functions/iosPush

推送正在运行,但它正在所有设备上发送。我想要一种方法,以便我可以在单个设备上发送它。

谢谢...

【问题讨论】:

  • 我试过这样 curl -X POST -H "X-Parse-Application-Id: myAppId" -H "X-Parse-Master-Key: masterkey" -H "Content-Type: application /json" -d '{ "where": { "user": { "__type": "Pointer", "className": "_User", "objectId": "cedsGn98ja" } }, "data": { "alert ": "new one2" } }' myapp.herokuapp.com/parse/functions/iosPush 但还是一样。 push 正在所有设备上发送,而不仅仅是这个用户

标签: ios node.js heroku push parse-server


【解决方案1】:

我使用以下代码从用户 1 向用户 1 发送通知以进行测试。您需要修改“someKey”以针对您的用例定位特定用户。不要忘记在您的应用程序委托中link your installation to the user

迅速

let text = "\(PFUser.current()!.username!) followed you, or whatever you want this to say";
let data = [
        "badge" : "Increment",
        "alert" : text,
    ]
    let request: [String : Any] = [
        "someKey" : PFUser.current()!.objectId!,
        "data" : data
    ]
    print(PFUser.current()!.objectId!)
    print("sending push notification...")
    PFCloud.callFunction(inBackground: "pushToFollowers", withParameters: request as [NSObject : AnyObject], block: { (results:AnyObject?, error:NSError?) in
        print("push \(String(describing: results!))")
        if error == nil {
            print (results!)
        }
        else {
            print (error!)
        }
    } as? PFIdResultBlock)

云代码

  Parse.Cloud.define("pushToFollowers", function (request, response) {
  console.log("Inside pushToFollowers");
  var params = request.params;
  var user = request.user;
  var sessionToken = user.getSessionToken();
  var someKey = request.someKey;
  var data = params.data;

 // Set our query to target specific user 
  var recipientUser = new Parse.Query(Parse.User);
  recipientUser.equalTo("objectId", request.params.someKey);

// Set our installation query
  var pushQuery = new Parse.Query(Parse.Installation);
   pushQuery.equalTo('deviceType', 'ios');
   pushQuery.matchesQuery('user', recipientUser); 
   pushQuery.find({ useMasterKey: true }).then(function(object) {
        response.success(object); 
        console.log("pushQuery got " + object.length);
        }, function(error) {
          response.error(error);
            console.error("pushQuery find failed. error = " + error.message);
    });

  // Send push notification to query
  Parse.Push.send({
    where: pushQuery,
    data: data  
  }, { useMasterKey: true })
    .then(function() {
      // Push sent!
      console.log("#### push sent!");
    }, function(error) {
      // There was a problem :(
      console.error("#### push error" + error.message);
    });
response.success('success, end of pushToFollowers')
});

【讨论】:

  • @s.jain 如果这对您有用,请接受答案。干杯!
【解决方案2】:

someKey 对所有用户来说都是唯一的吗?如果不是,您的推送应该发送到具有该匹配密钥的所有安装。为了发送给特定用户,您需要为 Installation 对象上的 user 字段添加查询约束。如果您想将其发送到该用户的所有设备,这可能是唯一的查询约束,或者您可以使用 deviceId 或 installationId 定位特定设备。

【讨论】:

  • 谢谢。你能解释一下我应该如何写安装ID的查询吗?我试过像 "where": { "user": "instalation_id" } 但它没有用。能否请您解释一下如何使用 somekey?
  • 问题是“where”条件不起作用。我已经尝试了一切,但推送正在发送给所有用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-05-06
  • 1970-01-01
  • 1970-01-01
  • 2015-11-21
  • 2017-05-12
  • 1970-01-01
相关资源
最近更新 更多