feat: improve rich chat notifications
- show image previews in photo notifications - localize non-text notification bodies by message type - use sender names as notification titles
This commit is contained in:
@@ -4,6 +4,8 @@ package ru.daemonlord.messenger.core.notifications
|
||||
|
||||
import android.Manifest
|
||||
import android.app.PendingIntent
|
||||
import android.graphics.BitmapFactory
|
||||
import android.graphics.Bitmap
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
@@ -18,6 +20,7 @@ import ru.daemonlord.messenger.MainActivity
|
||||
import javax.inject.Inject
|
||||
import javax.inject.Singleton
|
||||
import kotlin.math.abs
|
||||
import java.net.URL
|
||||
|
||||
@Singleton
|
||||
class NotificationDispatcher @Inject constructor(
|
||||
@@ -32,6 +35,7 @@ class NotificationDispatcher @Inject constructor(
|
||||
} else {
|
||||
NotificationChannels.CHANNEL_MESSAGES
|
||||
}
|
||||
val displayBody = payload.toDisplayBody(context)
|
||||
|
||||
val state = synchronized(chatStates) {
|
||||
val existing = chatStates[payload.chatId]
|
||||
@@ -40,7 +44,7 @@ class NotificationDispatcher @Inject constructor(
|
||||
}
|
||||
val updated = (existing ?: ChatNotificationState(title = payload.title))
|
||||
.copy(title = payload.title)
|
||||
.appendMessage(payload.body, payload.messageId)
|
||||
.appendMessage(displayBody, payload.messageId)
|
||||
chatStates[payload.chatId] = updated
|
||||
updated
|
||||
}
|
||||
@@ -82,19 +86,21 @@ class NotificationDispatcher @Inject constructor(
|
||||
)
|
||||
|
||||
val contentText = when {
|
||||
state.unreadCount <= 1 -> state.lines.firstOrNull() ?: payload.body
|
||||
else -> "${state.unreadCount} new messages"
|
||||
state.unreadCount <= 1 -> state.lines.firstOrNull() ?: displayBody
|
||||
else -> context.getString(R.string.notification_messages_count, state.unreadCount)
|
||||
}
|
||||
val inboxStyle = NotificationCompat.InboxStyle()
|
||||
.setBigContentTitle(state.title)
|
||||
.setSummaryText("${state.unreadCount} messages")
|
||||
.setSummaryText(context.getString(R.string.notification_messages_count, state.unreadCount))
|
||||
state.lines.reversed().forEach { inboxStyle.addLine(it) }
|
||||
val bigPicture = payload.previewImageUrl
|
||||
?.takeIf { it.isNotBlank() && state.unreadCount <= 1 && payload.messageType.equals("image", ignoreCase = true) }
|
||||
?.let { loadNotificationBitmap(it) }
|
||||
|
||||
val notification = NotificationCompat.Builder(context, channelId)
|
||||
val builder = NotificationCompat.Builder(context, channelId)
|
||||
.setSmallIcon(R.drawable.ic_notification_small)
|
||||
.setContentTitle(state.title)
|
||||
.setContentTitle(payload.senderName?.ifBlank { null } ?: state.title)
|
||||
.setContentText(contentText)
|
||||
.setStyle(inboxStyle)
|
||||
.setAutoCancel(true)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setGroup(GROUP_KEY_CHATS)
|
||||
@@ -120,7 +126,19 @@ class NotificationDispatcher @Inject constructor(
|
||||
.setAllowGeneratedReplies(true)
|
||||
.build(),
|
||||
)
|
||||
.build()
|
||||
if (bigPicture != null) {
|
||||
builder.setLargeIcon(bigPicture)
|
||||
.setStyle(
|
||||
NotificationCompat.BigPictureStyle()
|
||||
.bigPicture(bigPicture)
|
||||
.bigLargeIcon(null as Bitmap?)
|
||||
.setBigContentTitle(payload.senderName?.ifBlank { null } ?: state.title)
|
||||
.setSummaryText(contentText)
|
||||
)
|
||||
} else {
|
||||
builder.setStyle(inboxStyle)
|
||||
}
|
||||
val notification = builder.build()
|
||||
|
||||
val manager = NotificationManagerCompat.from(context)
|
||||
manager.notifySafely(chatNotificationId(payload.chatId), notification)
|
||||
@@ -145,7 +163,7 @@ class NotificationDispatcher @Inject constructor(
|
||||
val totalUnread = snapshot.sumOf { it.unreadCount }
|
||||
val inboxStyle = NotificationCompat.InboxStyle()
|
||||
.setBigContentTitle("Benya Messenger")
|
||||
.setSummaryText("$totalUnread messages")
|
||||
.setSummaryText(context.getString(R.string.notification_messages_count, totalUnread))
|
||||
snapshot.take(6).forEach { state ->
|
||||
val preview = state.lines.firstOrNull().orEmpty()
|
||||
val line = if (preview.isBlank()) {
|
||||
@@ -158,7 +176,7 @@ class NotificationDispatcher @Inject constructor(
|
||||
val summary = NotificationCompat.Builder(context, NotificationChannels.CHANNEL_MESSAGES)
|
||||
.setSmallIcon(R.drawable.ic_notification_small)
|
||||
.setContentTitle("Benya Messenger")
|
||||
.setContentText("$totalUnread new messages in ${snapshot.size} chats")
|
||||
.setContentText(context.getString(R.string.notification_summary_count, totalUnread, snapshot.size))
|
||||
.setStyle(inboxStyle)
|
||||
.setGroup(GROUP_KEY_CHATS)
|
||||
.setGroupSummary(true)
|
||||
@@ -188,6 +206,35 @@ class NotificationDispatcher @Inject constructor(
|
||||
return abs((chatId * 1_000_003L).toInt())
|
||||
}
|
||||
|
||||
private fun loadNotificationBitmap(url: String): Bitmap? {
|
||||
return runCatching {
|
||||
URL(url).openStream().use { input ->
|
||||
BitmapFactory.decodeStream(input)
|
||||
}
|
||||
}.getOrNull()
|
||||
}
|
||||
|
||||
private fun ChatNotificationPayload.toDisplayBody(context: Context): String {
|
||||
val normalizedType = messageType?.trim()?.lowercase()
|
||||
val raw = body.trim()
|
||||
if (!raw.isBlank() && normalizedType == "text") {
|
||||
return raw
|
||||
}
|
||||
return when (normalizedType) {
|
||||
"image" -> {
|
||||
if (raw.equals("photo", ignoreCase = true) || raw.equals("image", ignoreCase = true) || raw.isBlank()) {
|
||||
context.getString(R.string.notification_type_photo)
|
||||
} else raw
|
||||
}
|
||||
"video" -> if (raw.equals("video", ignoreCase = true) || raw.isBlank()) context.getString(R.string.notification_type_video) else raw
|
||||
"audio" -> if (raw.equals("audio", ignoreCase = true) || raw.isBlank()) context.getString(R.string.notification_type_audio) else raw
|
||||
"voice" -> if (raw.equals("voice message", ignoreCase = true) || raw.isBlank()) context.getString(R.string.notification_type_voice) else raw
|
||||
"circle_video" -> if (raw.equals("video note", ignoreCase = true) || raw.isBlank()) context.getString(R.string.notification_type_circle) else raw
|
||||
"file" -> if (raw.equals("file", ignoreCase = true) || raw.isBlank()) context.getString(R.string.notification_type_file) else raw
|
||||
else -> raw.ifBlank { context.getString(R.string.notification_type_message) }
|
||||
}
|
||||
}
|
||||
|
||||
private data class ChatNotificationState(
|
||||
val title: String,
|
||||
val unreadCount: Int = 0,
|
||||
@@ -195,7 +242,7 @@ class NotificationDispatcher @Inject constructor(
|
||||
val lastMessageId: Long? = null,
|
||||
) {
|
||||
fun appendMessage(body: String, messageId: Long?): ChatNotificationState {
|
||||
val normalized = body.trim().ifBlank { "New message" }
|
||||
val normalized = body.trim().ifBlank { "Message" }
|
||||
val updatedLines = buildList {
|
||||
add(normalized)
|
||||
lines.forEach { add(it) }
|
||||
|
||||
@@ -6,6 +6,9 @@ data class ChatNotificationPayload(
|
||||
val title: String,
|
||||
val body: String,
|
||||
val isMention: Boolean = false,
|
||||
val messageType: String? = null,
|
||||
val previewImageUrl: String? = null,
|
||||
val senderName: String? = null,
|
||||
)
|
||||
|
||||
object NotificationIntentExtras {
|
||||
|
||||
@@ -38,6 +38,9 @@ object PushPayloadParser {
|
||||
title = title,
|
||||
body = body,
|
||||
isMention = isMention,
|
||||
messageType = data["message_type"] ?: data["messageType"],
|
||||
previewImageUrl = data["preview_image_url"] ?: data["previewImageUrl"],
|
||||
senderName = data["sender_name"] ?: data["senderName"],
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,6 +83,15 @@
|
||||
<string name="notification_action_reply">Ответить</string>
|
||||
<string name="notification_action_reply_hint">Введите ответ</string>
|
||||
<string name="notification_action_mark_read">Прочитано</string>
|
||||
<string name="notification_messages_count">%1$d сообщений</string>
|
||||
<string name="notification_summary_count">%1$d новых сообщений в %2$d чатах</string>
|
||||
<string name="notification_type_message">Сообщение</string>
|
||||
<string name="notification_type_photo">Фото</string>
|
||||
<string name="notification_type_video">Видео</string>
|
||||
<string name="notification_type_audio">Аудио</string>
|
||||
<string name="notification_type_voice">Голосовое</string>
|
||||
<string name="notification_type_circle">Кружок</string>
|
||||
<string name="notification_type_file">Файл</string>
|
||||
<string name="profile_avatar_content_description">Аватар</string>
|
||||
<string name="profile_user_fallback">Пользователь</string>
|
||||
<string name="profile_choose_photo">Выбрать фото</string>
|
||||
|
||||
@@ -83,6 +83,15 @@
|
||||
<string name="notification_action_reply">Reply</string>
|
||||
<string name="notification_action_reply_hint">Write a reply</string>
|
||||
<string name="notification_action_mark_read">Mark as read</string>
|
||||
<string name="notification_messages_count">%1$d messages</string>
|
||||
<string name="notification_summary_count">%1$d new messages in %2$d chats</string>
|
||||
<string name="notification_type_message">Message</string>
|
||||
<string name="notification_type_photo">Photo</string>
|
||||
<string name="notification_type_video">Video</string>
|
||||
<string name="notification_type_audio">Audio</string>
|
||||
<string name="notification_type_voice">Voice message</string>
|
||||
<string name="notification_type_circle">Video note</string>
|
||||
<string name="notification_type_file">File</string>
|
||||
<string name="profile_avatar_content_description">Avatar</string>
|
||||
<string name="profile_user_fallback">User</string>
|
||||
<string name="profile_choose_photo">Choose photo</string>
|
||||
|
||||
Reference in New Issue
Block a user