Swift:获取用户安装ID以发送通知

时间:2016-05-25 22:41:31

标签: 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,但它看起来并不太优雅。我是否正确地假设我必须通过云代码向安装发送通知而不是用户?

1 个答案:

答案 0 :(得分:0)

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

权限不够,请尝试使用masterKey

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

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