user3353890
user3353890

Reputation: 1891

Trouble sending Push notifications from user to user using Parse and Swift

I'm having trouble sending push notifications from user to user. I'm using a Parse backend, and all of my code is in Swift. I can add a push notification to the server without issue, but I can't seem to add a push notification receiver without getting an error that highlights [PFInternalUtils assertValidClassForQuery:]. I know it has something to do with how I'm querying for the recipient, but I don't know how to fix it. Any help is appreciated. Here is my complete code:

    let message: NSString = responseMessage.text as NSString

    var data = [ "title": "Some Title",
                 "alert": message]

    var userQuery: PFQuery = PFUser.query()
    userQuery.whereKey("objectId", equalTo: recipientObjectId)
    var query: PFQuery = PFInstallation.query()
    query.whereKey("currentUser", equalTo: userQuery)

    var push: PFPush = PFPush()
    push.setQuery(query)
    push.setData(data)
    push.sendPushInBackground()

This Works If I take out the query, the code works with just sending a push notification to Parse:

    let message: NSString = responseMessage.text as NSString

    var data = [ "title": "Some Title",
                 "alert": message]

    var push: PFPush = PFPush()
    push.setData(data)
    push.sendPushInBackground()

This is the suspect code

    var userQuery: PFQuery = PFUser.query()
    userQuery.whereKey("objectId", equalTo: recipientObjectId)
    var query: PFQuery = PFInstallation.query()
    query.whereKey("currentUser", equalTo: userQuery)

    push.setQuery(query)

Also For Reference

recipientObjectId is the objectId of the user that is supposed to receive the push notification. It is saved as an NSString.

The "currentUser" key in my installation class is a pointer to the user that owns the installation.

EDIT

Forgot to give the console output. The console output reads: 'NSInvalidArgumentException', reason: 'Cannot do a comparison query for type: PFQuery'

Upvotes: 3

Views: 1939

Answers (1)

user3353890
user3353890

Reputation: 1891

Found the Answer!

Instead of using

query.whereKey("currentUser", equalTo: userQuery)

The Correct Code Is

query.whereKey("currentUser", matchesQuery: userQuery)

Upvotes: 3

Related Questions