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.
This commit is contained in:
2026-05-30 21:17:01 +02:00
parent a9f8efc72d
commit 2814ff2cfa
13 changed files with 121 additions and 2 deletions
+3 -1
View File
@@ -9,10 +9,12 @@
android:required="false" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"
tools:ignore="ForegroundServicesPolicy" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
<application
android:name=".Radio"
@@ -1,5 +1,7 @@
package com.michatec.radio
import android.Manifest
import android.os.Build
import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
@@ -13,6 +15,8 @@ import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.view.isVisible
import androidx.navigation.fragment.NavHostFragment
import androidx.activity.result.contract.ActivityResultContracts
import com.google.android.material.snackbar.Snackbar
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.navigateUp
@@ -31,6 +35,25 @@ class MainActivity : AppCompatActivity() {
/* Main class variables */
private lateinit var appBarConfiguration: AppBarConfiguration
// request notification permission (for Android 13+)
private val permissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
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 */
@@ -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)
}
}
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Stop</string>
<string name="notification_skip_to_previous">Forrige</string>
<string name="notification_skip_to_next">Næste</string>
<string name="notification_test_content">Dette er en testmeddelelse.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Fordyb dig i lyden du elsker!</string>
<string name="onboarding_app_get_started">Kom i gang</string>
@@ -111,6 +112,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Vis</string>
<string name="snackbar_update_available">er tilgængelig!</string>
<string name="snackbar_failed_permission_notification">Kunne ikke anmode om meddelelsestilladelse.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Sprog</string>
<string name="pref_language_selection_summary">Aktuelt sprog</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Stopp</string>
<string name="notification_skip_to_previous">Zurück</string>
<string name="notification_skip_to_next">Nächste</string>
<string name="notification_test_content">Dies ist eine Testbenachrichtigung.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Tauche ein in den Sound deiner Wahl!</string>
<string name="onboarding_app_get_started">Jetzt starten</string>
@@ -122,6 +123,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Zeigen</string>
<string name="snackbar_update_available">ist verfügbar!</string>
<string name="snackbar_failed_permission_notification">Fehler bei der Anfrage nach Benachrichtigungsberechtigung.</string>
<string name="pref_audio_effects_title">Audio-Effekte</string>
<string name="pref_bass_boost_title">Bass-Boost</string>
<string name="pref_bass_boost_summary">Erhöhen Sie die Bassverstärkung.</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Διακοπή</string>
<string name="notification_skip_to_previous">Προηγούμενο</string>
<string name="notification_skip_to_next">Επόμενο</string>
<string name="notification_test_content">Αυτή είναι μια δοκιμαστική ειδοποίηση.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Βυθιστείτε στον ήχο της επιλογής σας!</string>
<string name="onboarding_app_get_started">Ας ξεκινήσουμε</string>
@@ -113,6 +114,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Εμφάνισε</string>
<string name="snackbar_update_available">είναι διαθέσιμη!</string>
<string name="snackbar_failed_permission_notification">Απέτυχε η αίτηση δικαιώματος ειδοποίησης.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Γλώσσα</string>
<string name="pref_language_selection_summary">Τρέχουσα γλώσσα</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Arrêt</string>
<string name="notification_skip_to_previous">Précédent</string>
<string name="notification_skip_to_next">Suivant</string>
<string name="notification_test_content">Il s'agit d'une notification de test.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Plongez dans le son de votre choix !</string>
<string name="onboarding_app_get_started">Commencer maintenant</string>
@@ -111,6 +112,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Afficher</string>
<string name="snackbar_update_available">est disponible !</string>
<string name="snackbar_failed_permission_notification">Échec de la demande d'autorisation de notification.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Langue</string>
<string name="pref_language_selection_summary">Langue actuelle</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">停止</string>
<string name="notification_skip_to_previous">前へ</string>
<string name="notification_skip_to_next">次へ</string>
<string name="notification_test_content">テスト通知です。</string>
<!-- オンボーディング -->
<string name="onboarding_app_description">お気に入りのサウンドの世界に飛び込もう!</string>
<string name="onboarding_app_get_started">今すぐ始める</string>
@@ -112,6 +113,7 @@
<!-- スナックバー -->
<string name="snackbar_show">表示</string>
<string name="snackbar_update_available">が利用可能です!</string>
<string name="snackbar_failed_permission_notification">通知の権限リクエストに失敗しました。</string>
<!-- 言語選択 -->
<string name="pref_language_selection_title">言語</string>
<string name="pref_language_selection_summary">現在の言語</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Stoppen</string>
<string name="notification_skip_to_previous">Vorige</string>
<string name="notification_skip_to_next">Volgende</string>
<string name="notification_test_content">Dit is een testmelding.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Dompel jezelf onder in het geluid van je keuze!</string>
<string name="onboarding_app_get_started">Aan de slag</string>
@@ -113,6 +114,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Weergeven</string>
<string name="snackbar_update_available">is beschikbaar!</string>
<string name="snackbar_failed_permission_notification">Kan notificatierechtiging niet aanvragen.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Taal</string>
<string name="pref_language_selection_summary">Huidige taal</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Zatrzymaj</string>
<string name="notification_skip_to_previous">Poprzedni</string>
<string name="notification_skip_to_next">Następny</string>
<string name="notification_test_content">To jest powiadomienie testowe.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Zanurz się w dźwięku swojego wyboru!</string>
<string name="onboarding_app_get_started">Zaczynamy</string>
@@ -113,6 +114,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Wyświetl</string>
<string name="snackbar_update_available">jest dostępna!</string>
<string name="snackbar_failed_permission_notification">Nie udało się poprosić o pozwolenie na powiadomienia.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Język</string>
<string name="pref_language_selection_summary">Aktualny język</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Остановить</string>
<string name="notification_skip_to_previous">Предыдущий</string>
<string name="notification_skip_to_next">Следующий</string>
<string name="notification_test_content">Это тестовое уведомление.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Погрузитесь в звук по вашему выбору!</string>
<string name="onboarding_app_get_started">Начать</string>
@@ -113,6 +114,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Показать</string>
<string name="snackbar_update_available">доступно!</string>
<string name="snackbar_failed_permission_notification">Не удалось запросить разрешение на уведомления.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Язык</string>
<string name="pref_language_selection_summary">Текущий язык</string>
+2
View File
@@ -44,6 +44,7 @@
<string name="notification_stop">Зупинити</string>
<string name="notification_skip_to_previous">Попередня</string>
<string name="notification_skip_to_next">Наступна</string>
<string name="notification_test_content">Це тестове сповіщення.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Пориньте у звук на ваш вибір!</string>
<string name="onboarding_app_get_started">Початок роботи</string>
@@ -113,6 +114,7 @@
<!-- Snackbars -->
<string name="snackbar_show">Показати</string>
<string name="snackbar_update_available">доступне!</string>
<string name="snackbar_failed_permission_notification">Не вдалося запитати дозвіл на сповіщення.</string>
<!-- Language Selection -->
<string name="pref_language_selection_title">Мова</string>
<string name="pref_language_selection_summary">Поточна мова</string>
+3 -1
View File
@@ -2,6 +2,7 @@
<resources>
<!-- App Name -->
<string name="app_version_name" translatable="false">\"Red\"</string>
<string name="app_name" translatable="false">Radio</string>
<!-- Accessibility Descriptions -->
<string name="descr_app_icon">App icon depicting an old radio</string>
@@ -49,6 +50,7 @@
<string name="notification_stop">Stop</string>
<string name="notification_skip_to_previous">Previous</string>
<string name="notification_skip_to_next">Next</string>
<string name="notification_test_content">This is a test notification.</string>
<!-- Onboarding -->
<string name="onboarding_app_description">Immerse yourself in the sound of your choice!</string>
@@ -184,8 +186,8 @@
<string name="snackbar_update_available">is available!</string>
<string name="snackbar_url_app_home_page" translatable="false">https://github.com/michatec/Radio/releases/latest</string>
<string name="snackbar_github_update_check_url" translatable="false">https://api.github.com/repos/michatec/Radio/releases/latest</string>
<string name="app_name" translatable="false">Radio</string>
<string name="icon_launcher" translatable="false">Icon launcher.</string>
<string name="snackbar_failed_permission_notification">Failed to request notification permission.</string>
<!-- Extras -->
<string name="loading">Loading…</string>