Files
Radio/app/src/main/java/com/michatec/radio/NotificationSys.kt
T
Michatec d654d79bd5 feat(notification): support custom notification IDs
Update `NotificationSys.showNotification` to accept an optional `id`
parameter. This allows different notifications to be displayed
simultaneously by the system instead of overwriting each other.

In `SettingsFragment` and `PlayerFragment`, assign specific unique IDs
for test, share, and update notifications.
2026-06-01 19:44:56 +02:00

56 lines
2.1 KiB
Kotlin

package com.michatec.radio
import androidx.core.app.NotificationCompat
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
object NotificationSys {
private const val CHANNEL_ID = "com.michatec.radio.channel_messages"
private const val CHANNEL_NAME = "Notifications"
private const val NOTIFICATION_ID = 5000
fun createNotificationChannel(context: Context) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (notificationManager.getNotificationChannel(CHANNEL_ID) == null) {
val channel = NotificationChannel(
CHANNEL_ID,
CHANNEL_NAME,
NotificationManager.IMPORTANCE_LOW
).apply {
description = context.getString(R.string.notification_channel_description)
}
notificationManager.createNotificationChannel(channel)
}
}
fun showNotification(context: Context, title: String, content: String, intent: Intent? = null, id: Int = NOTIFICATION_ID) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
createNotificationChannel(context)
val targetIntent = intent ?: Intent(context, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
}
val pendingIntent = PendingIntent.getActivity(
context,
0,
targetIntent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
val notification = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.ic_notification_app_icon_white_24dp)
.setContentTitle(title)
.setContentText(content)
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
.build()
notificationManager.notify(id, notification)
}
}