add title in notificationDispatcher

This commit is contained in:
Sayantan Roychowdhury 2024-07-04 16:57:17 +05:30
parent 6f9d4bff5a
commit bb6adde03a
4 changed files with 24 additions and 6 deletions

View file

@ -7,6 +7,7 @@ import io.heckel.ntfy.db.Subscription
import io.heckel.ntfy.util.Log import io.heckel.ntfy.util.Log
import io.heckel.ntfy.up.Distributor import io.heckel.ntfy.up.Distributor
import io.heckel.ntfy.util.decodeBytesMessage import io.heckel.ntfy.util.decodeBytesMessage
import io.heckel.ntfy.util.decodeBytesTitle
import io.heckel.ntfy.util.safeLet import io.heckel.ntfy.util.safeLet
/** /**
@ -39,7 +40,12 @@ class NotificationDispatcher(val context: Context, val repository: Repository) {
} }
if (distribute) { if (distribute) {
safeLet(subscription.upAppId, subscription.upConnectorToken) { appId, connectorToken -> safeLet(subscription.upAppId, subscription.upConnectorToken) { appId, connectorToken ->
distributor.sendMessage(appId, connectorToken, decodeBytesMessage(notification)) distributor.sendMessage(
appId,
connectorToken,
decodeBytesMessage(notification),
decodeBytesTitle(notification),
)
} }
} }
if (downloadAttachment && downloadIcon) { if (downloadAttachment && downloadIcon) {

View file

@ -19,7 +19,9 @@ const val EXTRA_APPLICATION = "application"
const val EXTRA_TOKEN = "token" const val EXTRA_TOKEN = "token"
const val EXTRA_ENDPOINT = "endpoint" const val EXTRA_ENDPOINT = "endpoint"
const val EXTRA_MESSAGE = "message" const val EXTRA_MESSAGE = "message"
const val EXTRA_TITLE = "title"
const val EXTRA_BYTES_MESSAGE = "bytesMessage" const val EXTRA_BYTES_MESSAGE = "bytesMessage"
const val EXTRA_BYTES_TITLE = "bytesTitle"
const val PACKAGE_MURENA_UNIFIED_PUSH = "foundation.e.unifiedpoc" const val PACKAGE_MURENA_UNIFIED_PUSH = "foundation.e.unifiedpoc"
const val TOPIC_MURENA = "murena_notification" const val TOPIC_MURENA = "murena_notification"

View file

@ -9,14 +9,16 @@ import io.heckel.ntfy.util.Log
* See https://unifiedpush.org/spec/android/ for details. * See https://unifiedpush.org/spec/android/ for details.
*/ */
class Distributor(val context: Context) { class Distributor(val context: Context) {
fun sendMessage(app: String, connectorToken: String, message: ByteArray) { fun sendMessage(app: String, connectorToken: String, message: ByteArray, title: ByteArray) {
Log.d(TAG, "Sending MESSAGE to $app (token=$connectorToken): ${message.size} bytes") Log.d(TAG, "Sending MESSAGE to $app (token=$connectorToken): ${message.size} bytes")
val broadcastIntent = Intent() val broadcastIntent = Intent()
broadcastIntent.`package` = app broadcastIntent.`package` = app
broadcastIntent.action = ACTION_MESSAGE broadcastIntent.action = ACTION_MESSAGE
broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken) broadcastIntent.putExtra(EXTRA_TOKEN, connectorToken)
broadcastIntent.putExtra(EXTRA_MESSAGE, String(message)) // UTF-8 broadcastIntent.putExtra(EXTRA_MESSAGE, String(message)) // UTF-8
broadcastIntent.putExtra(EXTRA_TITLE, String(title)) // UTF-8
broadcastIntent.putExtra(EXTRA_BYTES_MESSAGE, message) broadcastIntent.putExtra(EXTRA_BYTES_MESSAGE, message)
broadcastIntent.putExtra(EXTRA_BYTES_TITLE, title)
context.sendBroadcast(broadcastIntent) context.sendBroadcast(broadcastIntent)
} }

View file

@ -170,18 +170,26 @@ fun decodeMessage(notification: Notification): String {
} }
} }
fun decodeBytesMessage(notification: Notification): ByteArray { fun decodeBytes(notification: Notification, string: String): ByteArray {
return try { return try {
if (notification.encoding == MESSAGE_ENCODING_BASE64) { if (notification.encoding == MESSAGE_ENCODING_BASE64) {
Base64.decode(notification.message, Base64.DEFAULT) Base64.decode(string, Base64.DEFAULT)
} else { } else {
notification.message.toByteArray() string.toByteArray()
} }
} catch (e: IllegalArgumentException) { } catch (e: IllegalArgumentException) {
notification.message.toByteArray() string.toByteArray()
} }
} }
fun decodeBytesMessage(notification: Notification): ByteArray {
return decodeBytes(notification, notification.message)
}
fun decodeBytesTitle(notification: Notification): ByteArray {
return decodeBytes(notification, notification.title)
}
/** /**
* See above; prepend emojis to title if the title is non-empty. * See above; prepend emojis to title if the title is non-empty.
* Otherwise, they are prepended to the message. * Otherwise, they are prepended to the message.