|
| 1 | +package com.ninecraft.booket |
| 2 | + |
| 3 | +import android.app.NotificationChannel |
| 4 | +import android.app.NotificationManager |
| 5 | +import android.app.PendingIntent |
| 6 | +import android.content.Context |
| 7 | +import android.content.Intent |
| 8 | +import androidx.core.app.NotificationCompat |
| 9 | +import androidx.core.content.ContextCompat |
| 10 | +import com.google.firebase.messaging.FirebaseMessagingService |
| 11 | +import com.google.firebase.messaging.RemoteMessage |
| 12 | +import com.ninecraft.booket.core.data.api.repository.UserRepository |
| 13 | +import com.ninecraft.booket.core.designsystem.R |
| 14 | +import com.ninecraft.booket.feature.main.MainActivity |
| 15 | +import dagger.hilt.android.AndroidEntryPoint |
| 16 | +import kotlinx.coroutines.CoroutineScope |
| 17 | +import kotlinx.coroutines.Dispatchers |
| 18 | +import kotlinx.coroutines.SupervisorJob |
| 19 | +import kotlinx.coroutines.cancel |
| 20 | +import kotlinx.coroutines.launch |
| 21 | +import javax.inject.Inject |
| 22 | + |
| 23 | +@AndroidEntryPoint |
| 24 | +class ReedFirebaseMessagingService : FirebaseMessagingService() { |
| 25 | + |
| 26 | + @Inject |
| 27 | + lateinit var userRepository: UserRepository |
| 28 | + |
| 29 | + private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob()) |
| 30 | + |
| 31 | + override fun onNewToken(token: String) { |
| 32 | + super.onNewToken(token) |
| 33 | + |
| 34 | + scope.launch { |
| 35 | + userRepository.syncFcmToken(token) |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + override fun onMessageReceived(message: RemoteMessage) { |
| 40 | + super.onMessageReceived(message) |
| 41 | + |
| 42 | + val title = message.notification?.title ?: "Reed" |
| 43 | + val body = message.notification?.body ?: "" |
| 44 | + |
| 45 | + val intent = Intent(this, MainActivity::class.java).apply { |
| 46 | + flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP |
| 47 | + } |
| 48 | + |
| 49 | + val pendingIntent = PendingIntent.getActivity( |
| 50 | + this, |
| 51 | + 0, |
| 52 | + intent, |
| 53 | + PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE, |
| 54 | + ) |
| 55 | + |
| 56 | + val builder = NotificationCompat.Builder(this, REED_CHANNEL_ID) |
| 57 | + .setSmallIcon(R.drawable.ic_notification) |
| 58 | + .setColor(ContextCompat.getColor(this, R.color.green_500)) |
| 59 | + .setContentTitle(title) |
| 60 | + .setContentText(body) |
| 61 | + .setContentIntent(pendingIntent) |
| 62 | + .setAutoCancel(true) |
| 63 | + .setPriority(NotificationCompat.PRIORITY_DEFAULT) |
| 64 | + |
| 65 | + val manager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager |
| 66 | + manager.notify(System.currentTimeMillis().toInt(), builder.build()) |
| 67 | + } |
| 68 | + |
| 69 | + override fun onDestroy() { |
| 70 | + scope.cancel() |
| 71 | + super.onDestroy() |
| 72 | + } |
| 73 | + |
| 74 | + companion object { |
| 75 | + private const val REED_CHANNEL_ID = "REED_PUSH_CHANNEL" |
| 76 | + private const val REED_CHANNEL_NAME = "리드 푸시 알림" |
| 77 | + private const val REED_CHANNEL_DESC = "리드 앱에서 보내는 푸시 알림을 관리합니다." |
| 78 | + |
| 79 | + // Android 8.0 이상 필수 채널 생성 |
| 80 | + fun createNotificationChannel(context: Context) { |
| 81 | + val channel = NotificationChannel( |
| 82 | + REED_CHANNEL_ID, |
| 83 | + REED_CHANNEL_NAME, |
| 84 | + NotificationManager.IMPORTANCE_DEFAULT, |
| 85 | + ).apply { |
| 86 | + description = REED_CHANNEL_DESC |
| 87 | + } |
| 88 | + |
| 89 | + val manager = context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager |
| 90 | + manager.createNotificationChannel(channel) |
| 91 | + } |
| 92 | + } |
| 93 | +} |
0 commit comments