Skip to content

Commit 2fd8a76

Browse files
authored
[Feat] Firebase Cloud Messaging 도입 (#351)
1 parent 860fc74 commit 2fd8a76

File tree

4 files changed

+96
-0
lines changed

4 files changed

+96
-0
lines changed

app/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ dependencies {
180180
implementation(libs.firebase.config)
181181
implementation(libs.firebase.analytics)
182182
implementation(libs.firebase.crashlytics)
183+
implementation(libs.firebase.messaging)
183184

184185
// Timber for logging
185186
implementation(libs.timber)

app/src/main/AndroidManifest.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@
8484
android:resource="@xml/widget_info" />
8585
</receiver>
8686

87+
<!-- Firebase Cloud Messaging -->
88+
<service
89+
android:name=".alarm.EatSsuFirebaseMessagingService"
90+
android:exported="false">
91+
<intent-filter>
92+
<action android:name="com.google.firebase.MESSAGING_EVENT" />
93+
</intent-filter>
94+
</service>
95+
96+
<!-- 기본 알림 아이콘과 색상 설정 -->
97+
<meta-data
98+
android:name="com.google.firebase.messaging.default_notification_icon"
99+
android:resource="@drawable/ic_mini_logo" />
100+
<meta-data
101+
android:name="com.google.firebase.messaging.default_notification_color"
102+
android:resource="@color/primary" />
103+
87104

88105
<!-- 워커 매니저를 위한 프로바이더 -->
89106
<provider
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
}

gradle/libs.versions.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ firebase-bom = { group = "com.google.firebase", name = "firebase-bom", version.r
125125
firebase-analytics = { group = "com.google.firebase", name = "firebase-analytics" }
126126
firebase-config = { group = "com.google.firebase", name = "firebase-config" }
127127
firebase-crashlytics = { group = "com.google.firebase", name = "firebase-crashlytics" }
128+
firebase-messaging = { group = "com.google.firebase", name = "firebase-messaging" }
128129
play-services-base = { group = "com.google.android.gms", name = "play-services-base", version.ref = "play-services-base" }
129130

130131
# etc

0 commit comments

Comments
 (0)