mirror of
https://github.com/Michatec/Radio.git
synced 2026-06-03 17:41:14 +02:00
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:
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user