From 2814ff2cfae056e97cb93f2606bdea13fe0a7db1 Mon Sep 17 00:00:00 2001 From: Michatec Date: Sat, 30 May 2026 21:17:01 +0200 Subject: [PATCH] feat(android): implement POST_NOTIFICATIONS permission handling Add NotificationSys to manage system notifications and update MainActivity to request POST_NOTIFICATIONS permission on Android 13+. Includes localized string resources for notification testing and permission error feedback. --- app/src/main/AndroidManifest.xml | 4 +- .../java/com/michatec/radio/MainActivity.kt | 34 ++++++++++ .../com/michatec/radio/NotificationSys.kt | 63 +++++++++++++++++++ app/src/main/res/values-da/strings.xml | 2 + app/src/main/res/values-de/strings.xml | 2 + app/src/main/res/values-el/strings.xml | 2 + app/src/main/res/values-fr/strings.xml | 2 + app/src/main/res/values-ja/strings.xml | 2 + app/src/main/res/values-nl/strings.xml | 2 + app/src/main/res/values-pl/strings.xml | 2 + app/src/main/res/values-ru/strings.xml | 2 + app/src/main/res/values-uk/strings.xml | 2 + app/src/main/res/values/strings.xml | 4 +- 13 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 app/src/main/java/com/michatec/radio/NotificationSys.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 56f181f..3ca6cde 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -9,10 +9,12 @@ android:required="false" /> - + + + if (isGranted) { + NotificationSys.showNotification( + this, + R.string.app_name, + R.string.notification_test_content + ) + } else { + Snackbar.make( + findViewById(android.R.id.content), + R.string.snackbar_failed_permission_notification, + Snackbar.LENGTH_LONG + ).show() + } + } + /* Overrides attachBaseContext from AppCompatActivity */ override fun attachBaseContext(newBase: Context) { val languageCode = PreferencesHelper.loadSelectedLanguage() @@ -82,6 +105,17 @@ class MainActivity : AppCompatActivity() { // register listener for changes in shared preferences PreferencesHelper.registerPreferenceChangeListener(sharedPreferenceChangeListener) + + // request permissions + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + permissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } else { + NotificationSys.showNotification( + this, + R.string.app_name, + R.string.notification_test_content + ) + } } /* Hides the loading/splash overlay */ diff --git a/app/src/main/java/com/michatec/radio/NotificationSys.kt b/app/src/main/java/com/michatec/radio/NotificationSys.kt new file mode 100644 index 0000000..e47dcdd --- /dev/null +++ b/app/src/main/java/com/michatec/radio/NotificationSys.kt @@ -0,0 +1,63 @@ +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 +import com.michatec.radio.R + +object NotificationSys { + private const val CHANNEL_ID = "com.michatec.radio.channel" + private const val CHANNEL_NAME = "Notifications" + private const val NOTIFICATION_ID = 1001 + + 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_DEFAULT + ).apply { + description = context.getString(R.string.notification_channel_description) + } + notificationManager.createNotificationChannel(channel) + } + } + + fun showNotification(context: Context, title: String, content: String) { + val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + createNotificationChannel(context) + + val 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, + intent, + 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(NOTIFICATION_ID, notification) + } + + fun showNotification(context: Context, titleResId: Int, contentResId: Int) { + val title = context.getString(titleResId) + val content = context.getString(contentResId) + showNotification(context, title, content) + } +} \ No newline at end of file diff --git a/app/src/main/res/values-da/strings.xml b/app/src/main/res/values-da/strings.xml index d9c1ad2..b8ce58b 100644 --- a/app/src/main/res/values-da/strings.xml +++ b/app/src/main/res/values-da/strings.xml @@ -44,6 +44,7 @@ Stop Forrige Næste + Dette er en testmeddelelse. Fordyb dig i lyden du elsker! Kom i gang @@ -111,6 +112,7 @@ Vis er tilgængelig! + Kunne ikke anmode om meddelelsestilladelse. Sprog Aktuelt sprog diff --git a/app/src/main/res/values-de/strings.xml b/app/src/main/res/values-de/strings.xml index 4924a8d..dbacff6 100644 --- a/app/src/main/res/values-de/strings.xml +++ b/app/src/main/res/values-de/strings.xml @@ -44,6 +44,7 @@ Stopp Zurück Nächste + Dies ist eine Testbenachrichtigung. Tauche ein in den Sound deiner Wahl! Jetzt starten @@ -122,6 +123,7 @@ Zeigen ist verfügbar! + Fehler bei der Anfrage nach Benachrichtigungsberechtigung. Audio-Effekte Bass-Boost Erhöhen Sie die Bassverstärkung. diff --git a/app/src/main/res/values-el/strings.xml b/app/src/main/res/values-el/strings.xml index 0ec697b..9596386 100644 --- a/app/src/main/res/values-el/strings.xml +++ b/app/src/main/res/values-el/strings.xml @@ -44,6 +44,7 @@ Διακοπή Προηγούμενο Επόμενο + Αυτή είναι μια δοκιμαστική ειδοποίηση. Βυθιστείτε στον ήχο της επιλογής σας! Ας ξεκινήσουμε @@ -113,6 +114,7 @@ Εμφάνισε είναι διαθέσιμη! + Απέτυχε η αίτηση δικαιώματος ειδοποίησης. Γλώσσα Τρέχουσα γλώσσα diff --git a/app/src/main/res/values-fr/strings.xml b/app/src/main/res/values-fr/strings.xml index e4f445d..8f1bcb6 100644 --- a/app/src/main/res/values-fr/strings.xml +++ b/app/src/main/res/values-fr/strings.xml @@ -44,6 +44,7 @@ Arrêt Précédent Suivant + Il s'agit d'une notification de test. Plongez dans le son de votre choix ! Commencer maintenant @@ -111,6 +112,7 @@ Afficher est disponible ! + Échec de la demande d'autorisation de notification. Langue Langue actuelle diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 1f68e72..9c50852 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -44,6 +44,7 @@ 停止 前へ 次へ + テスト通知です。 お気に入りのサウンドの世界に飛び込もう! 今すぐ始める @@ -112,6 +113,7 @@ 表示 が利用可能です! + 通知の権限リクエストに失敗しました。 言語 現在の言語 diff --git a/app/src/main/res/values-nl/strings.xml b/app/src/main/res/values-nl/strings.xml index e0c4ccc..535522a 100644 --- a/app/src/main/res/values-nl/strings.xml +++ b/app/src/main/res/values-nl/strings.xml @@ -44,6 +44,7 @@ Stoppen Vorige Volgende + Dit is een testmelding. Dompel jezelf onder in het geluid van je keuze! Aan de slag @@ -113,6 +114,7 @@ Weergeven is beschikbaar! + Kan notificatierechtiging niet aanvragen. Taal Huidige taal diff --git a/app/src/main/res/values-pl/strings.xml b/app/src/main/res/values-pl/strings.xml index 307a37d..523544c 100644 --- a/app/src/main/res/values-pl/strings.xml +++ b/app/src/main/res/values-pl/strings.xml @@ -44,6 +44,7 @@ Zatrzymaj Poprzedni Następny + To jest powiadomienie testowe. Zanurz się w dźwięku swojego wyboru! Zaczynamy @@ -113,6 +114,7 @@ Wyświetl jest dostępna! + Nie udało się poprosić o pozwolenie na powiadomienia. Język Aktualny język diff --git a/app/src/main/res/values-ru/strings.xml b/app/src/main/res/values-ru/strings.xml index 37c11fa..ea9c135 100644 --- a/app/src/main/res/values-ru/strings.xml +++ b/app/src/main/res/values-ru/strings.xml @@ -44,6 +44,7 @@ Остановить Предыдущий Следующий + Это тестовое уведомление. Погрузитесь в звук по вашему выбору! Начать @@ -113,6 +114,7 @@ Показать доступно! + Не удалось запросить разрешение на уведомления. Язык Текущий язык diff --git a/app/src/main/res/values-uk/strings.xml b/app/src/main/res/values-uk/strings.xml index c97a0da..021da40 100644 --- a/app/src/main/res/values-uk/strings.xml +++ b/app/src/main/res/values-uk/strings.xml @@ -44,6 +44,7 @@ Зупинити Попередня Наступна + Це тестове сповіщення. Пориньте у звук на ваш вибір! Початок роботи @@ -113,6 +114,7 @@ Показати доступне! + Не вдалося запитати дозвіл на сповіщення. Мова Поточна мова diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3dc1627..19df86a 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -2,6 +2,7 @@ \"Red\" + Radio App icon depicting an old radio @@ -49,6 +50,7 @@ Stop Previous Next + This is a test notification. Immerse yourself in the sound of your choice! @@ -184,8 +186,8 @@ is available! https://github.com/michatec/Radio/releases/latest https://api.github.com/repos/michatec/Radio/releases/latest - Radio Icon launcher. + Failed to request notification permission. Loading…