Skip to content

Commit

Permalink
[Setting] NotificationUtil (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajou4095 authored Feb 4, 2024
1 parent 4c67aa4 commit 0414d69
Show file tree
Hide file tree
Showing 5 changed files with 137 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
package ac.dnd.bookkeeping.android

import ac.dnd.bookkeeping.android.presentation.common.CHANNEL_1
import ac.dnd.bookkeeping.android.presentation.common.CHANNEL_GROUP_1
import android.app.Application
import android.app.NotificationChannel
import android.app.NotificationChannelGroup
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.annotation.RequiresApi
import com.kakao.sdk.common.KakaoSdk
import dagger.hilt.android.HiltAndroidApp

Expand All @@ -9,5 +17,34 @@ open class BookkeepingApplication : Application() {
override fun onCreate() {
super.onCreate()
KakaoSdk.init(this, BuildConfig.KAKAO_APP_KEY)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
initializeChannel(this)
}
}

@RequiresApi(Build.VERSION_CODES.O)
private fun initializeChannel(
context: Context
) {
val channel1 = NotificationChannel(
CHANNEL_1,
"channel 1",
NotificationManager.IMPORTANCE_HIGH
).apply {
description = "This is channel 1"
}

val group = NotificationChannelGroup(
CHANNEL_GROUP_1,
"group 1"
).apply {
channels.add(channel1)
}

context.getSystemService(NotificationManager::class.java).run {
createNotificationChannel(channel1)
createNotificationChannelGroup(group)
}
}
}
2 changes: 2 additions & 0 deletions presentation/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android">

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application>
<activity
android:name=".ui.main.MainActivity"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
package ac.dnd.bookkeeping.android.presentation.common

const val REGEX_EMAIL = "^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,6}$"

const val CHANNEL_1 = "1"
const val CHANNEL_GROUP_1 = "1"
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package ac.dnd.bookkeeping.android.presentation.common.util

import ac.dnd.bookkeeping.android.presentation.R
import android.Manifest
import android.annotation.SuppressLint
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import androidx.annotation.DrawableRes
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.gun0912.tedpermission.coroutine.TedPermission

@SuppressLint("MissingPermission")
suspend fun Context.showNotification(
channelId: String,
notificationId: Int = 0,
title: String? = null,
content: String? = null,
@DrawableRes icon: Int = R.drawable.ic_launcher,
group: String? = null,
priority: Int = NotificationCompat.PRIORITY_DEFAULT
): Boolean {
if (!checkPermissionGranted() || !checkNotificationChannelEnabled(channelId)) return false

val notification = NotificationCompat.Builder(this, channelId)
.setSmallIcon(icon)
.setContentTitle(title)
.setContentText(content)
.setPriority(priority)
.setGroup(group)
.build()

NotificationManagerCompat.from(this)
.notify(notificationId, notification)

return true
}

private suspend fun checkPermissionGranted(): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
TedPermission
.create()
.setPermissions(Manifest.permission.POST_NOTIFICATIONS)
.check().isGranted
} else {
false
}
}

private suspend fun Context.checkNotificationChannelEnabled(
id: String
): Boolean {
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val manager = getSystemService(NotificationManager::class.java)
val channel = manager.getNotificationChannel(id) ?: return false
val isChannelEnabled = channel.importance != NotificationManager.IMPORTANCE_NONE

if (!isChannelEnabled) {
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
putExtra(Settings.EXTRA_CHANNEL_ID, id)
}
startActivity(intent)
}
return isChannelEnabled
} else {
true
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package ac.dnd.bookkeeping.android.presentation.ui.main.home.setting

import ac.dnd.bookkeeping.android.presentation.R
import ac.dnd.bookkeeping.android.presentation.common.CHANNEL_1
import ac.dnd.bookkeeping.android.presentation.common.theme.Body1
import ac.dnd.bookkeeping.android.presentation.common.theme.Negative
import ac.dnd.bookkeeping.android.presentation.common.util.ErrorObserver
import ac.dnd.bookkeeping.android.presentation.common.util.showNotification
import ac.dnd.bookkeeping.android.presentation.common.view.BottomSheetScreen
import ac.dnd.bookkeeping.android.presentation.common.view.DialogScreen
import ac.dnd.bookkeeping.android.presentation.common.view.confirm.ConfirmButton
Expand Down Expand Up @@ -32,15 +34,18 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import kotlinx.coroutines.launch
import timber.log.Timber

@Composable
Expand All @@ -49,6 +54,8 @@ fun SettingScreen(
onShowSnackBar: () -> Unit,
viewModel: SettingViewModel = hiltViewModel()
) {
val scope = rememberCoroutineScope()
val context = LocalContext.current
Observer(
appState = appState,
viewModel = viewModel
Expand Down Expand Up @@ -158,6 +165,22 @@ fun SettingScreen(
isAddGroupShowing = true
}
)
Text(
text = "Notification",
fontSize = 20.sp,
color = Color.Black,
modifier = Modifier
.padding(vertical = 10.dp)
.clickable {
scope.launch {
context.showNotification(
channelId = CHANNEL_1,
title = "title",
content = "content"
)
}
}
)
}

if (isSearchRelationShowing) {
Expand Down

0 comments on commit 0414d69

Please sign in to comment.