1+ package com.eatssu.android.alarm
2+
3+ import android.app.NotificationChannel
4+ import android.app.NotificationManager
5+ import android.app.PendingIntent
6+ import android.content.Intent
7+ import androidx.core.app.NotificationCompat
8+ import com.eatssu.android.R
9+ import com.eatssu.android.presentation.login.IntroActivity
10+ import com.google.firebase.messaging.FirebaseMessagingService
11+ import com.google.firebase.messaging.RemoteMessage
12+ import timber.log.Timber
13+
14+
15+ class EatSsuFirebaseMessagingService : FirebaseMessagingService () {
16+ override fun onMessageReceived (remoteMessage : RemoteMessage ) {
17+ Timber .d(" From: ${remoteMessage.from} " )
18+
19+ val notificationBody = remoteMessage.notification?.body ? : return
20+ Timber .d(" Message Notification Body: $notificationBody " )
21+
22+ sendNotification(notificationBody)
23+ }
24+
25+
26+ override fun onNewToken (token : String ) {
27+ Timber .d(" Refreshed token: $token " )
28+ // 아직 특정 기기의 Push Token이 필요하지 않음
29+ }
30+
31+ private fun sendNotification (messageBody : String? ) {
32+ val notificationManager =
33+ getSystemService(NOTIFICATION_SERVICE ) as NotificationManager
34+
35+ val channel = NotificationChannel (
36+ CHANNEL_ID ,
37+ " 서버가 보낸 알림" ,
38+ NotificationManager .IMPORTANCE_HIGH
39+ ).apply {
40+ description = " 잇슈 서버가 보낸 알림을 표시합니다."
41+ enableLights(true )
42+ enableVibration(true ) // 진동도 활성화
43+ lockscreenVisibility = NotificationCompat .VISIBILITY_PUBLIC // 잠금 화면에서도 표시
44+ }
45+ notificationManager.createNotificationChannel(channel)
46+
47+ val intent = Intent (this , IntroActivity ::class .java).apply {
48+ putExtra(" launch_path" , " notification" )
49+ flags = Intent .FLAG_ACTIVITY_CLEAR_TOP
50+ }
51+
52+ val pendingIntent = PendingIntent .getActivity(
53+ this ,
54+ 0 ,
55+ intent,
56+ PendingIntent .FLAG_IMMUTABLE or PendingIntent .FLAG_UPDATE_CURRENT
57+ )
58+
59+ val notification =
60+ NotificationCompat .Builder (this , CHANNEL_ID )
61+ .setSmallIcon(R .drawable.ic_mini_logo)
62+ .setContentText(messageBody)
63+ .setPriority(NotificationCompat .PRIORITY_HIGH )
64+ .setCategory(NotificationCompat .CATEGORY_EVENT )
65+ .setVisibility(NotificationCompat .VISIBILITY_PUBLIC )
66+ .setAutoCancel(true )
67+ .setContentIntent(pendingIntent)
68+ .build()
69+
70+ notificationManager.notify(NOTIFICATION_ID , notification)
71+ }
72+
73+ companion object {
74+ private const val CHANNEL_ID = " FCMNotificationChannel"
75+ private const val NOTIFICATION_ID = 2
76+ }
77+ }
0 commit comments