Skip to content

Commit

Permalink
알림 권한 물어보도록 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
kts6056 committed Sep 10, 2024
1 parent 2ae22bb commit e9342b7
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ app/google-services.json
*.jks
keystore.properties
*.aab
output-metadata.json
**/output-metadata.json
*.zip
*.apk
50 changes: 50 additions & 0 deletions app/src/main/java/com/dkin/chevit/app/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.dkin.chevit.app

import android.Manifest.permission.POST_NOTIFICATIONS
import android.content.Intent
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.net.Uri
import android.os.Build
import android.provider.Settings
import android.view.MenuItem
import android.view.inputmethod.InputMethodManager
import androidx.activity.result.contract.ActivityResultContracts.RequestPermission
import androidx.appcompat.app.AlertDialog
import androidx.core.content.ContextCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.NavigationUI
import androidx.navigation.ui.navigateUp
import com.dkin.chevit.app.databinding.ActivityMainBinding
import com.dkin.chevit.core.base.BaseActivity
import dagger.hilt.android.AndroidEntryPoint
import timber.log.Timber

@AndroidEntryPoint
class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::inflate) {
Expand All @@ -27,6 +37,12 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
navHostFragment.navController
}

private val requestPermissionLauncher by lazy {
registerForActivityResult(RequestPermission()) { isGranted: Boolean ->
Timber.d("Permission granted: $isGranted")
}
}

override fun initView() {
NavigationUI.setupActionBarWithNavController(
this@MainActivity,
Expand All @@ -37,6 +53,9 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
hideSoftKeyboard()
}
supportActionBar?.hide()

// TODO 임시로 이렇게 처리 추후 개선 필요
askNotificationPermission()
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
Expand All @@ -63,4 +82,35 @@ class MainActivity : BaseActivity<ActivityMainBinding>(ActivityMainBinding::infl
val inputMethodManager = getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager?
inputMethodManager?.hideSoftInputFromWindow(focus.windowToken, 0)
}

private fun askNotificationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PERMISSION_GRANTED) {
// 이미 알림 권한 허용
return
} else if (shouldShowRequestPermissionRationale(POST_NOTIFICATIONS)) {
AlertDialog.Builder(this)
.setTitle("기기 알림이 꺼져있어요")
.setMessage("알림을 허용하여 준비물을 빼먹지 않도록 해요!")
.setPositiveButton("확인") { _, _ ->
navigateNotificationSetting()
}
.setNegativeButton("취소") { _, _ -> }
.show()
} else {
requestPermissionLauncher.launch(POST_NOTIFICATIONS)
}
}
}

private fun navigateNotificationSetting() {
val intent = Intent(Settings.ACTION_APP_NOTIFICATION_SETTINGS).apply {
putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
}
kotlin.runCatching {
startActivity(intent)
}.onFailure { throwable ->
Timber.e(throwable)
}
}
}
1 change: 1 addition & 0 deletions presentation/notification/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ android {
dependencies {
implementation(project(":core"))
implementation(project(":domain"))
implementation(project(":presentation:resource"))

implementation(platform(libs.firebase.bom))
implementation(libs.firebase.messaging)
Expand Down
9 changes: 9 additions & 0 deletions presentation/notification/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
<?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>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="default" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />

<service
android:name=".ChevitFirebaseMessagingService"
android:exported="false"
Expand Down

0 comments on commit e9342b7

Please sign in to comment.