Swift Parse Push advanced targeting not sending

2019-09-10 08:35发布

问题:

Followed the documentation for Parse advanced targeting and push notifications are still not sending. (I can receive pushes from Parse's console and client push is enabled.)

let userQuery: PFQuery = PFUser.query()!
userQuery.whereKey("objectId", equalTo: "JrePAMWUbm") // The user I am logged in with

let pushQuery: PFQuery = PFInstallation.query()!
pushQuery.whereKey("user", matchesQuery: userQuery)

let push = PFPush()
push.setQuery(pushQuery)
push.setMessage("Hello, World!")

push.sendPushInBackgroundWithBlock {
  success, error in

  if success {
    println("The push succeeded.")
  } else {
    println("The push failed.")
  }
}

I keep getting "The push succeeded." But I look at Push Details, and it says "0 Pushes Sent."

If I remove the userQuery and only use a PFInstallation query, I receive a notification.

let pushQuery: PFQuery = PFInstallation.query()!
pushQuery.whereKey("deviceType", equalTo: "ios")

let push = PFPush()
push.setQuery(pushQuery)
push.setMessage("Hello, World!")

push.sendPushInBackgroundWithBlock(nil)

Something is likely wrong with userQuery, but I'm not sure what. Return type isn't compatible with pushQuery.whereKey("user", matchesQuery: userQuery)?

回答1:

Everything above is set up correctly.

My Installation user column was not associating with my users -- I had to add the following:

let installation = PFInstallation.currentInstallation()
installation["user"] = PFUser.currentUser()
installation.saveInBackground()

This post helped.