I am in the middle of rewriting my Android app from Java to Kotlin. I'm using Parse LiveQuery on Android and can't seem to get subscriptions working whatsoever.
I am using this code to start the LiveQuery but not getting any response. I know that the table in the DB has liveQuery enabled. I also know that the same subscription worked in the Java version of this app.
fun startListeningToNotifications() {
//Build Live Query Client
val parseLiveQueryClient = ParseLiveQueryClient.Factory.getClient()
//Build Query
var parseQuery = ParseQuery.getQuery<ParseObject>("Notifications")
parseQuery.whereEqualTo("toUser", ParseUser.getCurrentUser())
parseQuery.orderByAscending("createdAt")
parseQuery.findInBackground { objects, e ->
//Do something with notifications
}
//Build Live Query Listener
var subscriptionHandling: SubscriptionHandling<ParseObject> = parseLiveQueryClient.subscribe(parseQuery)
subscriptionHandling.handleSubscribe {
Toast.makeText(this, "SUBSCRIBED", Toast.LENGTH_LONG).show()
}
subscriptionHandling.handleEvents { query, event, `object` ->
fetchNotificationCount()
}
subscriptionHandling.handleError { query, exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_LONG).show()
}
}
Any help is extremely appreciated. Been stuck on this for days now and the GitHub issue tracker is no help whatsoever as it's either all old, doesn't work with Kotlin or just a different issue all together.
EDIT
Attaching image of debugger here in the hopes it helps someone realise where I'm going wrong.
I have resolved the issue. When specifying the server URL, if you use an nginx server, the port must be specified otherwise it will fail.
My parse config looked like this:
Parse.initialize(
Parse.Configuration.Builder(context)
.applicationId("appId")
.clientKey("clientKey")
.server("https://yourdomain.com/database/parse")
.enableLocalDataStore()
.build()
)
The correct configuration is:
Parse.initialize(
Parse.Configuration.Builder(context)
.applicationId("appId")
.clientKey("clientKey")
.server("https://yourdomain.com:1337/parse")
.enableLocalDataStore()
.build()
)