From 9dd3eb652c1f2870c830115e3520c4d526f6a7a1 Mon Sep 17 00:00:00 2001 From: ss99x2002 Date: Wed, 19 Jul 2023 00:46:37 +0900 Subject: [PATCH] =?UTF-8?q?[Feat/#74]=20switch=20on/off=20=EB=94=B0?= =?UTF-8?q?=EB=A5=B8=20fcm=20=EC=95=8C=EB=A6=BC=20create=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../util/fcm/MyFirebaseMessagingService.kt | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/sopt/umbba_android/util/fcm/MyFirebaseMessagingService.kt b/app/src/main/java/com/sopt/umbba_android/util/fcm/MyFirebaseMessagingService.kt index fa35a52d..31d11b39 100644 --- a/app/src/main/java/com/sopt/umbba_android/util/fcm/MyFirebaseMessagingService.kt +++ b/app/src/main/java/com/sopt/umbba_android/util/fcm/MyFirebaseMessagingService.kt @@ -1,12 +1,15 @@ package com.sopt.umbba_android.util.fcm +import android.Manifest import android.app.NotificationChannel import android.app.NotificationManager import android.app.PendingIntent import android.content.Context import android.content.Intent +import android.content.pm.PackageManager import android.util.Log import androidx.core.app.NotificationCompat +import androidx.core.content.ContextCompat import androidx.lifecycle.MutableLiveData import com.google.android.gms.tasks.OnCompleteListener import com.google.firebase.messaging.FirebaseMessaging @@ -17,28 +20,27 @@ import com.sopt.umbba_android.presentation.qna.QuestionAnswerActivity import timber.log.Timber class MyFirebaseMessagingService : FirebaseMessagingService() { - var isSwitchChecked = MutableLiveData(false) - override fun onNewToken(token: String) { super.onNewToken(token) - Timber.tag("hyeon").e("fcm token : " + token) + Log.e("hyeon", "fcm token : " + token) } override fun onMessageReceived(message: RemoteMessage) { super.onMessageReceived(message) - Timber.tag("hyeon").e(message.notification.toString()) - Timber.tag("hyeon").e(message.data.toString()) - setNotificationOnOff(isSwitchChecked.value!!, message) - } - - private fun setNotificationOnOff(isSwitchChecked: Boolean, message: RemoteMessage) { - if (isSwitchChecked) { + Log.e("hyeon", message.notification.toString()) + Log.e("hyeon", message.data.toString()) + if (isNotificationGranted()) { createNotification(message) - } else { - Log.e("hyeon", "알림 끈 상태") } } + fun isNotificationGranted(): Boolean { + return ContextCompat.checkSelfPermission( + this, + Manifest.permission.POST_NOTIFICATIONS + ) == PackageManager.PERMISSION_GRANTED + } + private fun createNotification(message: RemoteMessage) { val intent = Intent(this, QuestionAnswerActivity::class.java).apply { addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) @@ -60,8 +62,8 @@ class MyFirebaseMessagingService : FirebaseMessagingService() { val builder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(R.drawable.ic_umbba_logo) - .setContentTitle("엄빠도 어렸다.") - .setContentText("알림 내용") + .setContentTitle(message.notification?.title.toString()) + .setContentText(message.notification?.body.toString()) .setContentIntent(pendingIntent) val notificationManager: NotificationManager =