mirror of
https://github.com/mudabbir-ahmad/Kotlin-TB2P1.git
synced 2026-12-06 20:13:10 +00:00
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:
1 parent
7f92507ab7
commit
98c92377ba
2 files changed
+46
-1
No files matched your search
@@ -1,6 +1,9 @@
|
|||||||
package com.example.madwellbeingapp
|
package com.example.madwellbeingapp
|
||||||
|
|
||||||
import android.app.Application
|
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.HabitDatabase
|
||||||
import com.example.madwellbeingapp.data.repository.HabitRepository
|
import com.example.madwellbeingapp.data.repository.HabitRepository
|
||||||
|
|
||||||
@@ -14,6 +17,25 @@ class HabitApp : Application() {
|
|||||||
HabitRepository(database.habitDao(), database.habitLogDao(), database.activityTypeDao())
|
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 {
|
companion object {
|
||||||
const val CHANNEL_ID = "habit_reminders"
|
const val CHANNEL_ID = "habit_reminders"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,19 @@
|
|||||||
package com.example.madwellbeingapp
|
package com.example.madwellbeingapp
|
||||||
|
|
||||||
|
import android.Manifest
|
||||||
|
import android.content.pm.PackageManager
|
||||||
|
import android.os.Build
|
||||||
import android.os.Bundle
|
import android.os.Bundle
|
||||||
import androidx.activity.ComponentActivity
|
import androidx.activity.ComponentActivity
|
||||||
import androidx.activity.compose.setContent
|
import androidx.activity.compose.setContent
|
||||||
import androidx.activity.enableEdgeToEdge
|
import androidx.activity.enableEdgeToEdge
|
||||||
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
import androidx.compose.runtime.Composable
|
import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.runtime.collectAsState
|
import androidx.compose.runtime.collectAsState
|
||||||
import androidx.compose.runtime.getValue
|
import androidx.compose.runtime.getValue
|
||||||
import androidx.compose.runtime.mutableStateListOf
|
import androidx.compose.runtime.mutableStateListOf
|
||||||
import androidx.compose.runtime.remember
|
import androidx.compose.runtime.remember
|
||||||
|
import androidx.core.content.ContextCompat
|
||||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||||
import androidx.navigation3.runtime.entryProvider
|
import androidx.navigation3.runtime.entryProvider
|
||||||
import androidx.navigation3.ui.NavDisplay
|
import androidx.navigation3.ui.NavDisplay
|
||||||
@@ -23,11 +28,29 @@ import com.example.madwellbeingapp.ui.theme.MadWellbeingAppTheme
|
|||||||
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
|
import com.example.madwellbeingapp.ui.viewmodel.HabitViewModel
|
||||||
|
|
||||||
class MainActivity : ComponentActivity() {
|
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?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
enableEdgeToEdge()
|
enableEdgeToEdge()
|
||||||
|
requestNotificationPermissionIfNeeded()
|
||||||
setContent { MadWellbeingAppTheme { HabitNavGraph() } }
|
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 ─────────────────────────────────────
|
// ── Screen destinations ─────────────────────────────────────
|
||||||
@@ -47,7 +70,7 @@ fun HabitNavGraph(vm: HabitViewModel = viewModel()) {
|
|||||||
val backStack = remember { mutableStateListOf<Any>(Screen.Home) }
|
val backStack = remember { mutableStateListOf<Any>(Screen.Home) }
|
||||||
|
|
||||||
fun nav(screen: Screen) = backStack.add(screen)
|
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(
|
NavDisplay(
|
||||||
backStack = backStack,
|
backStack = backStack,
|
||||||
|
|||||||
Reference in new issue
Block a user