From c64d3d09377e5c11debc9f4965927de4b7acd143 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Wed, 11 Oct 2023 14:08:12 +0900 Subject: [PATCH 01/16] =?UTF-8?q?feat:=20=EA=B3=B5=EC=97=B0=20=EC=9E=85?= =?UTF-8?q?=EC=9E=A5=20=ED=91=B8=EC=8B=9C=20=EC=95=8C=EB=A6=BC=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/TicketEntryService.kt | 59 ++++++++++++++++++- .../presentation/ui/home/HomeActivity.kt | 27 +++++++++ 2 files changed, 83 insertions(+), 3 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt index 14abf21a4..998acdc0c 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt @@ -1,18 +1,71 @@ package com.festago.festago.presentation.service +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import androidx.core.app.NotificationCompat +import com.festago.festago.R +import com.festago.festago.presentation.ui.home.HomeActivity import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.runBlocking class TicketEntryService : FirebaseMessagingService() { + + private val notificationManager by lazy { + getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + } + + override fun onCreate() { + super.onCreate() + val channel = NotificationChannel( + "ENTRY_ALERT", "공연 입장 알림", NotificationManager.IMPORTANCE_DEFAULT + ) + notificationManager.createNotificationChannel(channel) + } + override fun onMessageReceived(remoteMessage: RemoteMessage) { - runBlocking { - // TODO: 입장완료 로직인지 확인하는 로직 추가 필요 - ticketStateChangeEvent.emit(Unit) + when (remoteMessage.from) { + "ENTRY_ALERT" -> { + sendNotification(remoteMessage) + } + + "ENTRY_PROCESS" -> { + runBlocking { + // TODO: 입장완료 로직인지 확인하는 로직 추가 필요 + ticketStateChangeEvent.emit(Unit) + } + } + + else -> {} } } + private fun sendNotification(remoteMessage: RemoteMessage) { + + val intent = HomeActivity.getIntent(this).apply { + addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK) + } + val pendingIntent = PendingIntent.getActivity( + this, + 0, + intent, + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + ) + + val notificationBuilder = NotificationCompat.Builder(this, "ENTRY_ALERT") + .setSmallIcon(R.mipmap.ic_festago_logo_round) + .setContentTitle(remoteMessage.notification?.title) + .setContentText(remoteMessage.notification?.body) + .setAutoCancel(true) + .setContentIntent(pendingIntent) + + notificationManager.notify(1, notificationBuilder.build()) + } + override fun onNewToken(token: String) { // TODO: 토큰이 변경되었을 때 처리 } diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt index 8536e6a29..7bcd7d365 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt @@ -1,10 +1,16 @@ package com.festago.festago.presentation.ui.home +import android.Manifest import android.content.Context import android.content.Intent +import android.content.pm.PackageManager +import android.os.Build import android.os.Bundle +import android.widget.Toast +import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity +import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import com.festago.festago.R import com.festago.festago.databinding.ActivityHomeBinding @@ -23,11 +29,20 @@ class HomeActivity : AppCompatActivity() { private val vm: HomeViewModel by viewModels() + private val requestPermissionLauncher = registerForActivityResult( + ActivityResultContracts.RequestPermission(), + ) { isGranted: Boolean -> + if (!isGranted) { + Toast.makeText(this, "알림 권한을 거부했습니다 :(", Toast.LENGTH_SHORT).show() + } + } + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initBinding() initView() initObserve() + initNotificationPermission() } private fun initBinding() { @@ -109,6 +124,18 @@ class HomeActivity : AppCompatActivity() { fragmentTransaction.commit() } + private fun initNotificationPermission() { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if ((ContextCompat.checkSelfPermission( + this, + Manifest.permission.POST_NOTIFICATIONS + ) == PackageManager.PERMISSION_GRANTED).not() + ) { + requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) + } + } + } + companion object { fun getIntent(context: Context): Intent { return Intent(context, HomeActivity::class.java) From 0e3bff76e35a5511ca1b64df540f29cd0ead9b1e Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Wed, 11 Oct 2023 16:20:26 +0900 Subject: [PATCH 02/16] =?UTF-8?q?refactor:=20FcmMessageType=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/service/FcmMessageType.kt | 7 ++++ .../service/TicketEntryService.kt | 33 +++++++++++-------- .../app/src/main/res/values/strings.xml | 3 ++ 3 files changed, 30 insertions(+), 13 deletions(-) create mode 100644 android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt new file mode 100644 index 000000000..918f3f0c1 --- /dev/null +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt @@ -0,0 +1,7 @@ +package com.festago.festago.presentation.service + +enum class FcmMessageType(val id: Int, val channelId: String) { + ENTRY_ALERT(id = 0, channelId = "ENTRY_ALERT"), + ENTRY_PROCESS(id = 1, channelId = "ENTRY_PROCESS"), + ; +} diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt index 998acdc0c..7d97f52d3 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt @@ -12,6 +12,8 @@ import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.runBlocking +import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT +import com.festago.festago.presentation.service.FcmMessageType.ENTRY_PROCESS class TicketEntryService : FirebaseMessagingService() { @@ -19,28 +21,31 @@ class TicketEntryService : FirebaseMessagingService() { getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } + override fun onCreate() { super.onCreate() val channel = NotificationChannel( - "ENTRY_ALERT", "공연 입장 알림", NotificationManager.IMPORTANCE_DEFAULT + ENTRY_ALERT.channelId, + getString(R.string.entry_alert_channel_name), + NotificationManager.IMPORTANCE_DEFAULT ) notificationManager.createNotificationChannel(channel) } override fun onMessageReceived(remoteMessage: RemoteMessage) { - when (remoteMessage.from) { - "ENTRY_ALERT" -> { + when (remoteMessage.notification?.channelId) { + ENTRY_ALERT.channelId -> { sendNotification(remoteMessage) } - "ENTRY_PROCESS" -> { + ENTRY_PROCESS.channelId -> { runBlocking { // TODO: 입장완료 로직인지 확인하는 로직 추가 필요 ticketStateChangeEvent.emit(Unit) } } - else -> {} + else -> Unit } } @@ -51,19 +56,20 @@ class TicketEntryService : FirebaseMessagingService() { } val pendingIntent = PendingIntent.getActivity( this, - 0, + START_ACTIVITY_REQUEST_CODE, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) - val notificationBuilder = NotificationCompat.Builder(this, "ENTRY_ALERT") - .setSmallIcon(R.mipmap.ic_festago_logo_round) - .setContentTitle(remoteMessage.notification?.title) - .setContentText(remoteMessage.notification?.body) - .setAutoCancel(true) - .setContentIntent(pendingIntent) + val notificationBuilder = + NotificationCompat.Builder(this, getString(R.string.entry_alert_channel_name)) + .setSmallIcon(R.mipmap.ic_festago_logo_round) + .setContentTitle(remoteMessage.notification?.title) + .setContentText(remoteMessage.notification?.body) + .setAutoCancel(true) + .setContentIntent(pendingIntent) - notificationManager.notify(1, notificationBuilder.build()) + notificationManager.notify(ENTRY_ALERT.id, notificationBuilder.build()) } override fun onNewToken(token: String) { @@ -71,6 +77,7 @@ class TicketEntryService : FirebaseMessagingService() { } companion object { + private const val START_ACTIVITY_REQUEST_CODE = 0 val ticketStateChangeEvent: MutableSharedFlow = MutableSharedFlow() } } diff --git a/android/festago/app/src/main/res/values/strings.xml b/android/festago/app/src/main/res/values/strings.xml index 4e712baab..c5a432d91 100644 --- a/android/festago/app/src/main/res/values/strings.xml +++ b/android/festago/app/src/main/res/values/strings.xml @@ -129,4 +129,7 @@ 학교 목록 불러오기에 실패했습니다. 학교 선택 + + 공연 입장 알림 + From a56f8c33700a8b1e7d3982c41915a0e9b3e4950e Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Wed, 11 Oct 2023 16:52:17 +0900 Subject: [PATCH 03/16] =?UTF-8?q?refactor:=20NotificationManager=20?= =?UTF-8?q?=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/NotificationManager.kt | 52 +++++++++++++++++ .../service/TicketEntryService.kt | 57 +++---------------- 2 files changed, 60 insertions(+), 49 deletions(-) create mode 100644 android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt new file mode 100644 index 000000000..222928c99 --- /dev/null +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt @@ -0,0 +1,52 @@ +package com.festago.festago.presentation.service + +import android.app.NotificationChannel +import android.app.NotificationManager +import android.app.PendingIntent +import android.content.Context +import android.content.Intent +import androidx.core.app.NotificationCompat +import com.festago.festago.R +import com.festago.festago.presentation.ui.home.HomeActivity +import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT + + +class NotificationManager(context: Context) { + + private val notificationManager by lazy { + context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + } + + private val intent = HomeActivity.getIntent(context).apply { + addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK) + } + + private val pendingIntent = PendingIntent.getActivity( + context, + TicketEntryService.START_ACTIVITY_REQUEST_CODE, + intent, + PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT + ) + + private val entryAlertNotificationBuilder = + NotificationCompat.Builder(context, context.getString(R.string.entry_alert_channel_name)) + .setSmallIcon(R.mipmap.ic_festago_logo_round) + .setAutoCancel(true) + .setContentIntent(pendingIntent) + + init { + val channel = NotificationChannel( + ENTRY_ALERT.channelId, + context.getString(R.string.entry_alert_channel_name), + NotificationManager.IMPORTANCE_DEFAULT + ) + notificationManager.createNotificationChannel(channel) + } + + fun sendEntryAlertNotification(title: String, body: String) { + entryAlertNotificationBuilder + .setContentTitle(title) + .setContentText(body) + notificationManager.notify(ENTRY_ALERT.id, entryAlertNotificationBuilder.build()) + } +} diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt index 7d97f52d3..9a6684cd5 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt @@ -1,41 +1,23 @@ package com.festago.festago.presentation.service -import android.app.NotificationChannel -import android.app.NotificationManager -import android.app.PendingIntent -import android.content.Context -import android.content.Intent -import androidx.core.app.NotificationCompat -import com.festago.festago.R -import com.festago.festago.presentation.ui.home.HomeActivity +import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT +import com.festago.festago.presentation.service.FcmMessageType.ENTRY_PROCESS import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.runBlocking -import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT -import com.festago.festago.presentation.service.FcmMessageType.ENTRY_PROCESS class TicketEntryService : FirebaseMessagingService() { - private val notificationManager by lazy { - getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - } - - - override fun onCreate() { - super.onCreate() - val channel = NotificationChannel( - ENTRY_ALERT.channelId, - getString(R.string.entry_alert_channel_name), - NotificationManager.IMPORTANCE_DEFAULT - ) - notificationManager.createNotificationChannel(channel) - } + private val notificationManager by lazy { NotificationManager(this) } override fun onMessageReceived(remoteMessage: RemoteMessage) { when (remoteMessage.notification?.channelId) { ENTRY_ALERT.channelId -> { - sendNotification(remoteMessage) + notificationManager.sendEntryAlertNotification( + remoteMessage.notification?.title ?: "", + remoteMessage.notification?.body ?: "" + ) } ENTRY_PROCESS.channelId -> { @@ -49,35 +31,12 @@ class TicketEntryService : FirebaseMessagingService() { } } - private fun sendNotification(remoteMessage: RemoteMessage) { - - val intent = HomeActivity.getIntent(this).apply { - addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK) - } - val pendingIntent = PendingIntent.getActivity( - this, - START_ACTIVITY_REQUEST_CODE, - intent, - PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT - ) - - val notificationBuilder = - NotificationCompat.Builder(this, getString(R.string.entry_alert_channel_name)) - .setSmallIcon(R.mipmap.ic_festago_logo_round) - .setContentTitle(remoteMessage.notification?.title) - .setContentText(remoteMessage.notification?.body) - .setAutoCancel(true) - .setContentIntent(pendingIntent) - - notificationManager.notify(ENTRY_ALERT.id, notificationBuilder.build()) - } - override fun onNewToken(token: String) { // TODO: 토큰이 변경되었을 때 처리 } companion object { - private const val START_ACTIVITY_REQUEST_CODE = 0 + const val START_ACTIVITY_REQUEST_CODE = 0 val ticketStateChangeEvent: MutableSharedFlow = MutableSharedFlow() } } From 4ca5d968131d55cf91f8127f9427baec4cb756d1 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Thu, 12 Oct 2023 09:49:51 +0900 Subject: [PATCH 04/16] =?UTF-8?q?refactor:=20application=EC=97=90=EC=84=9C?= =?UTF-8?q?=20=EC=B1=84=EB=84=90=20=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/festago/festago/FestagoApplication.kt | 16 ++++++++++++++++ .../presentation/service/NotificationManager.kt | 15 +++------------ 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt b/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt index 1a1877069..48ee10327 100644 --- a/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt +++ b/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt @@ -1,6 +1,10 @@ package com.festago.festago import android.app.Application +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import com.festago.festago.presentation.service.FcmMessageType import com.kakao.sdk.common.KakaoSdk import dagger.hilt.android.HiltAndroidApp @@ -10,9 +14,21 @@ class FestagoApplication : Application() { override fun onCreate() { super.onCreate() initKakaoSdk() + initNotificationChannel() } private fun initKakaoSdk() { KakaoSdk.init(this, BuildConfig.KAKAO_NATIVE_APP_KEY) } + + private fun initNotificationChannel() { + val notificationManager = + getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager + val channel = NotificationChannel( + FcmMessageType.ENTRY_ALERT.channelId, + getString(R.string.entry_alert_channel_name), + NotificationManager.IMPORTANCE_DEFAULT + ) + notificationManager.createNotificationChannel(channel) + } } diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt index 222928c99..9d0902036 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt @@ -1,17 +1,16 @@ package com.festago.festago.presentation.service -import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent import androidx.core.app.NotificationCompat import com.festago.festago.R -import com.festago.festago.presentation.ui.home.HomeActivity import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT +import com.festago.festago.presentation.ui.home.HomeActivity -class NotificationManager(context: Context) { +class NotificationManager(private val context: Context) { private val notificationManager by lazy { context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @@ -34,19 +33,11 @@ class NotificationManager(context: Context) { .setAutoCancel(true) .setContentIntent(pendingIntent) - init { - val channel = NotificationChannel( - ENTRY_ALERT.channelId, - context.getString(R.string.entry_alert_channel_name), - NotificationManager.IMPORTANCE_DEFAULT - ) - notificationManager.createNotificationChannel(channel) - } - fun sendEntryAlertNotification(title: String, body: String) { entryAlertNotificationBuilder .setContentTitle(title) .setContentText(body) + notificationManager.notify(ENTRY_ALERT.id, entryAlertNotificationBuilder.build()) } } From 76eef231f741b2e9fc464865a529494914c9d970 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 15:28:44 +0900 Subject: [PATCH 05/16] =?UTF-8?q?fix:=20=ED=8F=AC=EA=B7=B8=EB=9D=BC?= =?UTF-8?q?=EC=9A=B4=EB=93=9C=20=ED=91=B8=EC=8B=9C=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EB=B2=84=EA=B7=B8=20=ED=95=B4=EA=B2=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/NotificationManager.kt | 27 ++++++++++++------- .../service/TicketEntryService.kt | 1 - 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt index 9d0902036..d6482e6d4 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt @@ -1,10 +1,12 @@ package com.festago.festago.presentation.service -import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.content.pm.PackageManager +import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat +import androidx.core.app.NotificationManagerCompat import com.festago.festago.R import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT import com.festago.festago.presentation.ui.home.HomeActivity @@ -12,24 +14,21 @@ import com.festago.festago.presentation.ui.home.HomeActivity class NotificationManager(private val context: Context) { - private val notificationManager by lazy { - context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager - } - private val intent = HomeActivity.getIntent(context).apply { - addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK) + flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK } private val pendingIntent = PendingIntent.getActivity( context, - TicketEntryService.START_ACTIVITY_REQUEST_CODE, + HOME_REQUEST_CODE, intent, PendingIntent.FLAG_IMMUTABLE or PendingIntent.FLAG_UPDATE_CURRENT ) private val entryAlertNotificationBuilder = - NotificationCompat.Builder(context, context.getString(R.string.entry_alert_channel_name)) + NotificationCompat.Builder(context, ENTRY_ALERT.channelId) .setSmallIcon(R.mipmap.ic_festago_logo_round) + .setPriority(NotificationCompat.PRIORITY_DEFAULT) .setAutoCancel(true) .setContentIntent(pendingIntent) @@ -38,6 +37,16 @@ class NotificationManager(private val context: Context) { .setContentTitle(title) .setContentText(body) - notificationManager.notify(ENTRY_ALERT.id, entryAlertNotificationBuilder.build()) + if (ActivityCompat.checkSelfPermission( + context, + android.Manifest.permission.POST_NOTIFICATIONS + ) == PackageManager.PERMISSION_GRANTED + ) { + NotificationManagerCompat.from(context).notify(0, entryAlertNotificationBuilder.build()) + } + } + + companion object { + private const val HOME_REQUEST_CODE = 0 } } diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt index 9a6684cd5..236859444 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt @@ -36,7 +36,6 @@ class TicketEntryService : FirebaseMessagingService() { } companion object { - const val START_ACTIVITY_REQUEST_CODE = 0 val ticketStateChangeEvent: MutableSharedFlow = MutableSharedFlow() } } From 49680d779f5baeedf160412588d9e814871009de Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 15:41:46 +0900 Subject: [PATCH 06/16] =?UTF-8?q?refactor:=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EA=B6=8C=ED=95=9C=20=EA=B1=B0=EB=B6=80=20=EB=A9=94=EC=8B=9C?= =?UTF-8?q?=EC=A7=80=20=EB=AC=B8=EC=9E=90=EC=97=B4=20=EC=B6=94=EC=B6=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../festago/festago/presentation/ui/home/HomeActivity.kt | 6 +++++- android/festago/app/src/main/res/values/strings.xml | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt index 7bcd7d365..bcf7746b2 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt @@ -33,7 +33,11 @@ class HomeActivity : AppCompatActivity() { ActivityResultContracts.RequestPermission(), ) { isGranted: Boolean -> if (!isGranted) { - Toast.makeText(this, "알림 권한을 거부했습니다 :(", Toast.LENGTH_SHORT).show() + Toast.makeText( + this, + getString(R.string.home_notification_permission_denied), + Toast.LENGTH_SHORT + ).show() } } diff --git a/android/festago/app/src/main/res/values/strings.xml b/android/festago/app/src/main/res/values/strings.xml index c5a432d91..155df7acc 100644 --- a/android/festago/app/src/main/res/values/strings.xml +++ b/android/festago/app/src/main/res/values/strings.xml @@ -32,6 +32,7 @@ 축제 목록 티켓 목록 마이페이지 + 알림 권한을 거부했습니다 :( %1s ~ %1s From c8d18ad712e91a14e91720c9bc412821d507a056 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 16:34:14 +0900 Subject: [PATCH 07/16] refactor: ktlintCheck --- .../festago/presentation/service/NotificationManager.kt | 1 - .../com/festago/festago/presentation/ui/home/HomeActivity.kt | 5 +++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt index d6482e6d4..78263f46e 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt @@ -11,7 +11,6 @@ import com.festago.festago.R import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT import com.festago.festago.presentation.ui.home.HomeActivity - class NotificationManager(private val context: Context) { private val intent = HomeActivity.getIntent(context).apply { diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt index bcf7746b2..0ea442590 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt @@ -130,10 +130,11 @@ class HomeActivity : AppCompatActivity() { private fun initNotificationPermission() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - if ((ContextCompat.checkSelfPermission( + if ( + ContextCompat.checkSelfPermission( this, Manifest.permission.POST_NOTIFICATIONS - ) == PackageManager.PERMISSION_GRANTED).not() + ) != PackageManager.PERMISSION_GRANTED ) { requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) } From 8b529135a1250550ec3c1f81d3b9bf9d94fb65a7 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 16:39:55 +0900 Subject: [PATCH 08/16] =?UTF-8?q?move:=20fcm=20=EA=B4=80=EB=A0=A8=20?= =?UTF-8?q?=ED=8C=8C=EC=9D=BC=20=ED=8C=A8=ED=82=A4=EC=A7=80=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- android/festago/app/src/main/AndroidManifest.xml | 2 +- .../src/main/java/com/festago/festago/FestagoApplication.kt | 2 +- .../festago/presentation/{service => fcm}/FcmMessageType.kt | 2 +- .../presentation/{service => fcm}/NotificationManager.kt | 4 ++-- .../presentation/{service => fcm}/TicketEntryService.kt | 6 +++--- .../presentation/ui/ticketentry/TicketEntryActivity.kt | 2 +- 6 files changed, 9 insertions(+), 9 deletions(-) rename android/festago/app/src/main/java/com/festago/festago/presentation/{service => fcm}/FcmMessageType.kt (78%) rename android/festago/app/src/main/java/com/festago/festago/presentation/{service => fcm}/NotificationManager.kt (93%) rename android/festago/app/src/main/java/com/festago/festago/presentation/{service => fcm}/TicketEntryService.kt (85%) diff --git a/android/festago/app/src/main/AndroidManifest.xml b/android/festago/app/src/main/AndroidManifest.xml index a0a346ffe..7a2f6f785 100644 --- a/android/festago/app/src/main/AndroidManifest.xml +++ b/android/festago/app/src/main/AndroidManifest.xml @@ -64,7 +64,7 @@ android:exported="false" /> diff --git a/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt b/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt index 48ee10327..d20567b58 100644 --- a/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt +++ b/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt @@ -4,7 +4,7 @@ import android.app.Application import android.app.NotificationChannel import android.app.NotificationManager import android.content.Context -import com.festago.festago.presentation.service.FcmMessageType +import com.festago.festago.presentation.fcm.FcmMessageType import com.kakao.sdk.common.KakaoSdk import dagger.hilt.android.HiltAndroidApp diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/FcmMessageType.kt similarity index 78% rename from android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt rename to android/festago/app/src/main/java/com/festago/festago/presentation/fcm/FcmMessageType.kt index 918f3f0c1..6bb066104 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/FcmMessageType.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/FcmMessageType.kt @@ -1,4 +1,4 @@ -package com.festago.festago.presentation.service +package com.festago.festago.presentation.fcm enum class FcmMessageType(val id: Int, val channelId: String) { ENTRY_ALERT(id = 0, channelId = "ENTRY_ALERT"), diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt similarity index 93% rename from android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt rename to android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt index 78263f46e..196859ac2 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/NotificationManager.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt @@ -1,4 +1,4 @@ -package com.festago.festago.presentation.service +package com.festago.festago.presentation.fcm import android.app.PendingIntent import android.content.Context @@ -8,7 +8,7 @@ import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.festago.festago.R -import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT +import com.festago.festago.presentation.fcm.FcmMessageType.ENTRY_ALERT import com.festago.festago.presentation.ui.home.HomeActivity class NotificationManager(private val context: Context) { diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt similarity index 85% rename from android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt rename to android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt index 236859444..35c246ebc 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/service/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt @@ -1,7 +1,7 @@ -package com.festago.festago.presentation.service +package com.festago.festago.presentation.fcm -import com.festago.festago.presentation.service.FcmMessageType.ENTRY_ALERT -import com.festago.festago.presentation.service.FcmMessageType.ENTRY_PROCESS +import com.festago.festago.presentation.fcm.FcmMessageType.ENTRY_ALERT +import com.festago.festago.presentation.fcm.FcmMessageType.ENTRY_PROCESS import com.google.firebase.messaging.FirebaseMessagingService import com.google.firebase.messaging.RemoteMessage import kotlinx.coroutines.flow.MutableSharedFlow diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryActivity.kt index 542aa8132..74094c06b 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/ticketentry/TicketEntryActivity.kt @@ -9,7 +9,7 @@ import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.content.res.ResourcesCompat import com.festago.festago.databinding.ActivityTicketEntryBinding -import com.festago.festago.presentation.service.TicketEntryService +import com.festago.festago.presentation.fcm.TicketEntryService import com.festago.festago.presentation.util.repeatOnStarted import com.google.zxing.BarcodeFormat import com.journeyapps.barcodescanner.BarcodeEncoder From 5fc6435099c7c7ff83619d585f2013b2ac70864b Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 17:15:56 +0900 Subject: [PATCH 09/16] =?UTF-8?q?refactor:=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EA=B6=8C=ED=95=9C=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=B6=84?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/ui/home/HomeActivity.kt | 20 ++----------------- .../presentation/util/PermissionUtil.kt | 17 ++++++++++++++++ 2 files changed, 19 insertions(+), 18 deletions(-) create mode 100644 android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt index 0ea442590..fb0c5b899 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt @@ -1,16 +1,12 @@ package com.festago.festago.presentation.ui.home -import android.Manifest import android.content.Context import android.content.Intent -import android.content.pm.PackageManager -import android.os.Build import android.os.Bundle import android.widget.Toast import androidx.activity.result.contract.ActivityResultContracts import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity -import androidx.core.content.ContextCompat import androidx.fragment.app.Fragment import com.festago.festago.R import com.festago.festago.databinding.ActivityHomeBinding @@ -19,6 +15,7 @@ import com.festago.festago.presentation.ui.home.mypage.MyPageFragment import com.festago.festago.presentation.ui.home.ticketlist.TicketListFragment import com.festago.festago.presentation.ui.signin.SignInActivity import com.festago.festago.presentation.util.repeatOnStarted +import com.festago.festago.presentation.util.requestNotificationPermission import dagger.hilt.android.AndroidEntryPoint @AndroidEntryPoint @@ -46,7 +43,7 @@ class HomeActivity : AppCompatActivity() { initBinding() initView() initObserve() - initNotificationPermission() + requestNotificationPermission(requestPermissionLauncher) } private fun initBinding() { @@ -128,19 +125,6 @@ class HomeActivity : AppCompatActivity() { fragmentTransaction.commit() } - private fun initNotificationPermission() { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - if ( - ContextCompat.checkSelfPermission( - this, - Manifest.permission.POST_NOTIFICATIONS - ) != PackageManager.PERMISSION_GRANTED - ) { - requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS) - } - } - } - companion object { fun getIntent(context: Context): Intent { return Intent(context, HomeActivity::class.java) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt new file mode 100644 index 000000000..932e14143 --- /dev/null +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt @@ -0,0 +1,17 @@ +package com.festago.festago.presentation.util + +import android.Manifest.permission.POST_NOTIFICATIONS +import android.app.Activity +import android.content.pm.PackageManager.PERMISSION_GRANTED +import android.os.Build +import androidx.activity.result.ActivityResultLauncher +import androidx.core.content.ContextCompat + + +fun Activity.requestNotificationPermission(resultLauncher: ActivityResultLauncher) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PERMISSION_GRANTED) { + resultLauncher.launch(POST_NOTIFICATIONS) + } + } +} From 13ee204770b85e78ada726aa6bf92612055f1f7a Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 17:28:25 +0900 Subject: [PATCH 10/16] refactor: ktlintcheck --- .../java/com/festago/festago/presentation/util/PermissionUtil.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt index 932e14143..48d30efbc 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt @@ -7,7 +7,6 @@ import android.os.Build import androidx.activity.result.ActivityResultLauncher import androidx.core.content.ContextCompat - fun Activity.requestNotificationPermission(resultLauncher: ActivityResultLauncher) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PERMISSION_GRANTED) { From c31cc5d563e0cd14cf4668b2a7411bce56eec2c3 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Fri, 13 Oct 2023 17:56:53 +0900 Subject: [PATCH 11/16] =?UTF-8?q?refactor:=20notificationManager=20?= =?UTF-8?q?=EC=BD=94=EB=93=9C=20=EC=88=9C=EC=84=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/main/java/com/festago/festago/FestagoApplication.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt b/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt index d20567b58..8f07fa96d 100644 --- a/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt +++ b/android/festago/app/src/main/java/com/festago/festago/FestagoApplication.kt @@ -22,13 +22,13 @@ class FestagoApplication : Application() { } private fun initNotificationChannel() { - val notificationManager = - getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val channel = NotificationChannel( FcmMessageType.ENTRY_ALERT.channelId, getString(R.string.entry_alert_channel_name), NotificationManager.IMPORTANCE_DEFAULT ) + val notificationManager = + getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.createNotificationChannel(channel) } } From 18e89c693f1e2516bd864898fb5071755ca2606f Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Sat, 14 Oct 2023 20:27:25 +0900 Subject: [PATCH 12/16] =?UTF-8?q?refactor:=20=EB=B6=88=ED=95=84=EC=9A=94?= =?UTF-8?q?=ED=95=9C=20=EC=A3=BC=EC=84=9D=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/festago/festago/presentation/fcm/TicketEntryService.kt | 1 - 1 file changed, 1 deletion(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt index 35c246ebc..a69dd62d7 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt @@ -22,7 +22,6 @@ class TicketEntryService : FirebaseMessagingService() { ENTRY_PROCESS.channelId -> { runBlocking { - // TODO: 입장완료 로직인지 확인하는 로직 추가 필요 ticketStateChangeEvent.emit(Unit) } } From 7376ead522fec4f95c829b76f510d42a9fa6f7f9 Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Sat, 14 Oct 2023 20:28:52 +0900 Subject: [PATCH 13/16] =?UTF-8?q?refactor:=20=EB=A9=94=EC=84=9C=EB=93=9C?= =?UTF-8?q?=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../festago/presentation/fcm/TicketEntryService.kt | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt index a69dd62d7..b2061db31 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/TicketEntryService.kt @@ -13,12 +13,7 @@ class TicketEntryService : FirebaseMessagingService() { override fun onMessageReceived(remoteMessage: RemoteMessage) { when (remoteMessage.notification?.channelId) { - ENTRY_ALERT.channelId -> { - notificationManager.sendEntryAlertNotification( - remoteMessage.notification?.title ?: "", - remoteMessage.notification?.body ?: "" - ) - } + ENTRY_ALERT.channelId -> handleEntryAlert(remoteMessage) ENTRY_PROCESS.channelId -> { runBlocking { @@ -30,6 +25,13 @@ class TicketEntryService : FirebaseMessagingService() { } } + private fun handleEntryAlert(remoteMessage: RemoteMessage) { + notificationManager.sendEntryAlertNotification( + remoteMessage.notification?.title ?: "", + remoteMessage.notification?.body ?: "" + ) + } + override fun onNewToken(token: String) { // TODO: 토큰이 변경되었을 때 처리 } From 41668f2778ac961d265d413750b7ed2bd938fd2b Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Mon, 16 Oct 2023 14:23:46 +0900 Subject: [PATCH 14/16] =?UTF-8?q?refactor:=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EA=B6=8C=ED=95=9C=20=ED=99=95=EC=9D=B8=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/fcm/NotificationManager.kt | 9 ++---- .../presentation/util/PermissionUtil.kt | 32 +++++++++++++++++-- 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt index 196859ac2..6abea300b 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/fcm/NotificationManager.kt @@ -3,13 +3,12 @@ package com.festago.festago.presentation.fcm import android.app.PendingIntent import android.content.Context import android.content.Intent -import android.content.pm.PackageManager -import androidx.core.app.ActivityCompat import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import com.festago.festago.R import com.festago.festago.presentation.fcm.FcmMessageType.ENTRY_ALERT import com.festago.festago.presentation.ui.home.HomeActivity +import com.festago.festago.presentation.util.checkNotificationPermission class NotificationManager(private val context: Context) { @@ -36,11 +35,7 @@ class NotificationManager(private val context: Context) { .setContentTitle(title) .setContentText(body) - if (ActivityCompat.checkSelfPermission( - context, - android.Manifest.permission.POST_NOTIFICATIONS - ) == PackageManager.PERMISSION_GRANTED - ) { + checkNotificationPermission(context) { NotificationManagerCompat.from(context).notify(0, entryAlertNotificationBuilder.build()) } } diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt index 48d30efbc..d8f257894 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/util/PermissionUtil.kt @@ -2,6 +2,7 @@ package com.festago.festago.presentation.util import android.Manifest.permission.POST_NOTIFICATIONS import android.app.Activity +import android.content.Context import android.content.pm.PackageManager.PERMISSION_GRANTED import android.os.Build import androidx.activity.result.ActivityResultLauncher @@ -9,8 +10,33 @@ import androidx.core.content.ContextCompat fun Activity.requestNotificationPermission(resultLauncher: ActivityResultLauncher) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - if (ContextCompat.checkSelfPermission(this, POST_NOTIFICATIONS) != PERMISSION_GRANTED) { - resultLauncher.launch(POST_NOTIFICATIONS) - } + checkSelfPermission( + this, + POST_NOTIFICATIONS, + onNotGranted = { + resultLauncher.launch(POST_NOTIFICATIONS) + } + ) + } +} + +fun checkNotificationPermission(context: Context, block: () -> Unit) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + checkSelfPermission(context, POST_NOTIFICATIONS, onGranted = block) + } else { + block() + } +} + +fun checkSelfPermission( + context: Context, + permission: String, + onGranted: () -> Unit = {}, + onNotGranted: () -> Unit = {} +) { + if (ContextCompat.checkSelfPermission(context, permission) == PERMISSION_GRANTED) { + onGranted() + } else { + onNotGranted() } } From 81c666f6b8ba23bcc9f313bf9dd73fa1c1fed51c Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Mon, 16 Oct 2023 15:03:29 +0900 Subject: [PATCH 15/16] =?UTF-8?q?refactor:=20=EC=95=8C=EB=A6=BC=20?= =?UTF-8?q?=EA=B6=8C=ED=95=9C=20=EC=9A=94=EC=B2=AD=20=EB=A9=94=EC=84=9C?= =?UTF-8?q?=EB=93=9C=20=EB=B6=84=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/ui/home/HomeActivity.kt | 29 ++++++++++--------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt index fb0c5b899..4aaee02da 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt @@ -26,24 +26,12 @@ class HomeActivity : AppCompatActivity() { private val vm: HomeViewModel by viewModels() - private val requestPermissionLauncher = registerForActivityResult( - ActivityResultContracts.RequestPermission(), - ) { isGranted: Boolean -> - if (!isGranted) { - Toast.makeText( - this, - getString(R.string.home_notification_permission_denied), - Toast.LENGTH_SHORT - ).show() - } - } - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) initBinding() initView() initObserve() - requestNotificationPermission(requestPermissionLauncher) + initNotificationPermission() } private fun initBinding() { @@ -77,6 +65,21 @@ class HomeActivity : AppCompatActivity() { } } + private fun initNotificationPermission() { + val requestPermissionLauncher = registerForActivityResult( + ActivityResultContracts.RequestPermission() + ) { isGranted: Boolean -> + if (!isGranted) { + Toast.makeText( + this, + getString(R.string.home_notification_permission_denied), + Toast.LENGTH_SHORT + ).show() + } + } + requestNotificationPermission(requestPermissionLauncher) + } + private fun getItemType(menuItemId: Int): HomeItemType { return when (menuItemId) { R.id.item_festival -> HomeItemType.FESTIVAL_LIST From c2e18c76a2f8f0326cc01199bafa4f10400c53fe Mon Sep 17 00:00:00 2001 From: EmilyCh0 Date: Mon, 16 Oct 2023 18:00:12 +0900 Subject: [PATCH 16/16] refactor: ktlintcheck --- .../com/festago/festago/presentation/ui/home/HomeActivity.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt index 28089c8e9..59dd80bb7 100644 --- a/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt +++ b/android/festago/app/src/main/java/com/festago/festago/presentation/ui/home/HomeActivity.kt @@ -3,9 +3,9 @@ package com.festago.festago.presentation.ui.home import android.content.Context import android.content.Intent import android.os.Bundle +import android.widget.Toast import androidx.activity.result.ActivityResultLauncher import androidx.activity.result.contract.ActivityResultContracts -import android.widget.Toast import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.fragment.app.Fragment