javascript - swift : get User Installation id to send notifications

标签 javascript swift parse-platform push-notification

我正在尝试向 Parse 中的特定用户发送通知。我已经阅读了有关该主题的几个问题,但无法完全理解它;我在云代码(javascript)中有这个函数:

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

  var senderUser = request.user;
  var recipientUserId = request.params.recipientId;
  var message = request.params.message;

  // Validate that the sender is allowed to send to the recipient.
  // For example each user has an array of objectIds of friends
  //if (senderUser.get("friendIds").indexOf(recipientUserId) === -1) {
    //response.error("The recipient is not the sender's friend, cannot send push.");
  //}

  // Validate the message text.
  // For example make sure it is under 140 characters
  //if (message.length > 140) {
  // Truncate and add a ...
    //message = message.substring(0, 137) + "...";
  //}
  // Send the push.
  // Find devices associated with the recipient user
  var recipientUser = new Parse.User();
 recipientUser.id = recipientUserId;
  var pushQuery = new Parse.Query(Parse.Installation);
  pushQuery.equalTo("user", recipientUser);
  // Send the push notification to results of the query
  Parse.Push.send({
    where: pushQuery,
    data: {
      alert: message
    }
  }).then(function() {
      response.success("Push was sent successfully.");
  }, function(error) {
      response.error("Push failed to send with error: " + error.message);
  });
});

因此,我尝试在调用 Xcode 项目中的函数之前传递接收者 UserId 中的安装。起初,我以为你应该使用 _User.objectId,但似乎你需要定位安装。因此,我尝试从 _Installation 类中获取特定用户的安装,但收到错误消息“不允许客户端在安装集合上执行查找功能”。

知道我该怎么做吗?我应该尝试在函数中进行安装吗?

编辑:检查后,问题似乎出在我的 Swift 代码上,特别是在这里:

var sendeeInstallation = PFInstallation.query()
                    var sendeeId = PFUser.currentUser()?.objectId
                    sendeeInstallation!.whereKey("User", equalTo: "sendeeId")
                    println("SEND INSTALLATION")
                    sendeeInstallation!.findObjectsInBackgroundWithBlock({ (results:[AnyObject]?, error:NSError?) -> Void in
                        println("FIND DONE \(results![0].objectId)")

控制台记录“SEND INSTALLATION”,但不记录“FIND DONE”,所以看来我无法在 _Installation 类中使用 find 函数。知道我该如何解决这个问题吗?我正在考虑在 _User 类中使用链接到 _Installation 的指针,但它看起来不太优雅。我是否正确地假设我必须将通知发送给安装,而不是通过云代码发送给用户?

最佳答案

不允许客户端对安装集合执行查找功能。

权限不够,尝试使用masterKey

您可以在 Push.send 上使用 useMasterKey:true 。用法如下

  Parse.Push.send({
    where: pushQuery,
    data: {
      alert: message
    }, useMasterKey:true 
  })

关于javascript - swift : get User Installation id to send notifications,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37448819/

相关文章:

ios - swift 的动态类名

JavaScript 计算器。如何停止多个操作数?

javascript - 导出 Highcharts 时无法根据需要禁用图例标题/启用图例标题

javascript - Javascript Class.create() 表示类未定义

swift - 具有自定义节标题的可重用 UITableView "base class"

swift - Vapor SQLite 缺少模块 CSQLiteMac

javascript 和 jQuery 动画

swift - 进度条 iOS 13

parse-platform - Parse.com 'Explorer' View 自定义事件计数与 'Events' View 不一致?

android - ParseImageView 是否缓存 ParseFile