Skip to content

Commit 05c885e

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

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +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.Message
19+
import com.github.gotify.client.model.CreateMessage
2020
import com.github.gotify.databinding.ActivityShareBinding
2121
import com.github.gotify.messages.provider.ApplicationHolder
2222
import kotlinx.coroutines.Dispatchers
@@ -107,15 +107,15 @@ internal class ShareActivity : AppCompatActivity() {
107107
return
108108
}
109109

110-
val message = Message()
110+
val message = CreateMessage()
111111
if (titleText.isNotEmpty()) {
112112
message.title = titleText
113113
}
114114
message.message = contentText
115115
message.priority = priority.toLong()
116116

117117
launchCoroutine {
118-
val response = executeMessageCall(appIndex, message)
118+
val response = executeMessageCall(appsHolder.get()[appIndex], message)
119119
withContext(Dispatchers.Main) {
120120
if (response) {
121121
Toast.makeText(this@ShareActivity, "Pushed!", Toast.LENGTH_LONG).show()
@@ -131,10 +131,16 @@ internal class ShareActivity : AppCompatActivity() {
131131
}
132132
}
133133

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

0 commit comments

Comments
 (0)