Add notification channel and request permission for habit reminders: implement notification setup in HabitApp and handle permission requests in MainActivity

This commit is contained in:
bobbert committed 2026-03-05 17:05:28 +00:00
1 parent 7f92507ab7
commit 98c92377ba
2 files changed
+46 -1

No files matched your search

@@ -1,6 +1,9 @@
package com.example.madwellbeingapp
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationManager
import android.os.Build
import com.example.madwellbeingapp.data.HabitDatabase
import com.example.madwellbeingapp.data.repository.HabitRepository
@@ -14,6 +17,25 @@ class HabitApp : Application() {
HabitRepository(database.habitDao(), database.habitLogDao(), database.activityTypeDao())
}
override fun onCreate() {
super.onCreate()
createNotificationChannel()
}
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"Habit Reminders",
NotificationManager.IMPORTANCE_HIGH
).apply {
description = "Daily reminders for your tracked habits"
}
val manager = getSystemService(NotificationManager::class.java)
manager.createNotificationChannel(channel)
}
}
companion object {
const val CHANNEL_ID = "habit_reminders"
}
@@ -1,14 +1,19 @@
package com.example.madwellbeingapp
import android.Manifest
import android.content.pm.PackageManager
import android.os.Build
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.remember
import androidx.core.content.ContextCompat
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation3.runtime.entryProvider
import androidx.navigation3.ui.NavDisplay
@@ -23,11 +28,29 @@ import com.example.madwellbeingapp.ui.theme.MadWellbeingAppTheme
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
class MainActivity : ComponentActivity() {
private val requestNotificationPermissionLauncher =
registerForActivityResult(ActivityResultContracts.RequestPermission()) { _ ->
// Permission granted or denied — nothing extra needed.
// If denied, the HabitReminderReceiver gracefully skips posting.
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
requestNotificationPermissionIfNeeded()
setContent { MadWellbeingAppTheme { HabitNavGraph() } }
}
private fun requestNotificationPermissionIfNeeded() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS)
!= PackageManager.PERMISSION_GRANTED
) {
requestNotificationPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
}
}
}
}
// ── Screen destinations ─────────────────────────────────────
@@ -47,7 +70,7 @@ fun HabitNavGraph(vm: HabitViewModel = viewModel()) {
val backStack = remember { mutableStateListOf<Any>(Screen.Home) }
fun nav(screen: Screen) = backStack.add(screen)
fun back() { if (backStack.size > 1) backStack.removeLast() }
fun back() { if (backStack.size > 1) backStack.removeAt(backStack.lastIndex) }
NavDisplay(
backStack = backStack,