notifications: show reaction emoji as icon + quoted message snippet
Build / increment-version (push) Successful in 5s
Build / build-android (push) Successful in 4m47s

Reaction notifications previously had no icon and a generic body. Now the
reaction emoji is rendered as the notification icon and the body quotes the
reacted-to message. Applied to both the web/Electron/Tauri path and the
Android background sync service.
This commit is contained in:
2026-10-06 10:43:12 +11:00
parent 96d5ae40e1
commit cef9f28dfd
2 changed files with 135 additions and 7 deletions
@@ -377,12 +377,50 @@ class MatrixSyncService : Service() {
val isMessageLike =
eventType == "m.room.message" ||
eventType == "m.room.encrypted" ||
eventType == "m.sticker"
eventType == "m.sticker" ||
eventType == "m.reaction"
if (!isMessageLike) continue
if (sender.isNotBlank() && sender == myUserId) continue
if (eventId.isNotBlank() && !shownEventIds.add(eventId)) continue
val content = event.optJSONObject("content") ?: JSONObject()
// Reactions have no media, so use the emoji as the icon and quote the
// reacted-to message as the body.
if (eventType == "m.reaction") {
val relates = content.optJSONObject("m.relates_to")
val key = relates?.optString("key").orEmpty()
if (key.isBlank()) continue
val parentEventId = relates?.optString("event_id").orEmpty()
val snippet =
if (parentEventId.isNotBlank())
fetchEventBodySnippet(homeserver, token, roomId, parentEventId)
else ""
val reactionBody =
if (snippet.isNotBlank()) "reacted with $key to \"$snippet\""
else "reacted with $key"
val profile = try {
resolveProfile(sender, homeserver, token)
} catch (e: Exception) {
Log.w(TAG, "profile fetch failed: ${e.message}")
UserProfile(sender.substringAfter("@").substringBefore(":").ifBlank { "Someone" }, null)
}
Log.i(TAG, "notify room=$roomId type=$eventType sender=$sender")
showMessageNotification(
nm,
roomId,
profile.displayName,
reactionBody,
renderEmojiBitmap(key),
null,
resolveGroupInfo(roomId, notifyCtx),
)
notifiedForRoom = true
continue
}
val encrypted = eventType == "m.room.encrypted"
val msgtype = content.optString("msgtype")
val rawBody = content.optString("body")
@@ -587,6 +625,25 @@ class MatrixSyncService : Service() {
}
}
/** Fetch the body of the event a reaction points at, as a short single-line snippet. */
private fun fetchEventBodySnippet(
homeserver: String,
token: String,
roomId: String,
eventId: String,
maxLength: Int = 60,
): String {
val obj = fetchJson(
"$homeserver/_matrix/client/v3/rooms/${urlEncode(roomId)}/event/${urlEncode(eventId)}",
token,
) ?: return ""
val content = obj.optJSONObject("content") ?: return ""
val rawBody = content.optString("body")
if (rawBody.isBlank()) return ""
val trimmed = rawBody.replace(Regex("\\s+"), " ").trim()
return if (trimmed.length > maxLength) trimmed.substring(0, maxLength) + "…" else trimmed
}
private fun resolveProfile(mxid: String, homeserver: String, token: String): UserProfile {
val cachedName = displayNameCache[mxid]
if (cachedName != null) return UserProfile(cachedName, avatarCache[mxid])
@@ -1164,6 +1221,25 @@ class MatrixSyncService : Service() {
}
}
/** Render a reaction emoji to a bitmap so it can be used as the notification icon. */
fun renderEmojiBitmap(emoji: String): Bitmap? {
return try {
val size = 128
val bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
val paint = Paint(Paint.ANTI_ALIAS_FLAG)
paint.textSize = size * 0.7f
paint.textAlign = Paint.Align.CENTER
val fontMetrics = paint.fontMetrics
val y = size / 2f - (fontMetrics.ascent + fontMetrics.descent) / 2f
canvas.drawText(emoji, size / 2f, y, paint)
bitmap
} catch (e: Exception) {
Log.w(TAG, "Failed to render emoji icon: ${e.message}")
null
}
}
/** Crop a bitmap into a circle for the notification avatar slot. */
fun toCircularBitmap(bitmap: Bitmap): Bitmap {
val size = minOf(bitmap.width, bitmap.height)