Skip to content

Commit 0d9a78c

Browse files
committed
fix: allow pushing messages with client auth
1 parent 214c012 commit 0d9a78c

1 file changed

Lines changed: 12 additions & 5 deletions

File tree

‎app/src/main/kotlin/com/github/gotify/sharing/ShareActivity.kt‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import com.github.gotify.api.ApiException
1616
import com.github.gotify.api.ClientFactory
1717
import com.github.gotify.client.api.MessageApi
1818
import com.github.gotify.client.model.Application
19+
import com.github.gotify.client.model.CreateMessage
1920
import com.github.gotify.client.model.Message
2021
import com.github.gotify.databinding.ActivityShareBinding
2122
import com.github.gotify.messages.provider.ApplicationHolder
@@ -107,15 +108,15 @@ internal class ShareActivity : AppCompatActivity() {
107108
return
108109
}
109110

110-
val message = Message()
111+
val message = CreateMessage()
111112
if (titleText.isNotEmpty()) {
112113
message.title = titleText
113114
}
114115
message.message = contentText
115116
message.priority = priority.toLong()
116117

117118
launchCoroutine {
118-
val response = executeMessageCall(appIndex, message)
119+
val response = executeMessageCall(appsHolder.get()[appIndex], message)
119120
withContext(Dispatchers.Main) {
120121
if (response) {
121122
Toast.makeText(this@ShareActivity, "Pushed!", Toast.LENGTH_LONG).show()
@@ -131,10 +132,16 @@ internal class ShareActivity : AppCompatActivity() {
131132
}
132133
}
133134

134-
private fun executeMessageCall(appIndex: Int, message: Message): Boolean {
135-
val pushClient = ClientFactory.clientToken(settings, appsHolder.get()[appIndex].token)
135+
private fun executeMessageCall(app: Application, message: CreateMessage): Boolean {
136+
// In gotify 3.0, tokens aren't returned in the API anymore, but the push api allows setting the appid with client auth.
137+
val client = if (app.token == null) {
138+
message.appid = app.id
139+
ClientFactory.clientToken(settings)
140+
} else {
141+
ClientFactory.clientToken(settings, app.token)
142+
}
136143
return try {
137-
val messageApi = pushClient.createService(MessageApi::class.java)
144+
val messageApi = client.createService(MessageApi::class.java)
138145
Api.execute(messageApi.createMessage(message))
139146
true
140147
} catch (apiException: ApiException) {

0 commit comments

Comments
 (0)