Skip to content

Commit

Permalink
[Feat/#74] switch on/off 따른 fcm 알림 create 처리
Browse files Browse the repository at this point in the history
  • Loading branch information
ss99x2002 committed Jul 18, 2023
1 parent 3c17f71 commit 9dd3eb6
Showing 1 changed file with 16 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -17,28 +20,27 @@ import com.sopt.umbba_android.presentation.qna.QuestionAnswerActivity
import timber.log.Timber

class MyFirebaseMessagingService : FirebaseMessagingService() {
var isSwitchChecked = MutableLiveData<Boolean>(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)
Expand All @@ -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 =
Expand Down

0 comments on commit 9dd3eb6

Please sign in to comment.