1+ package com.terning.core.firebase.remoteconfig.messageservice
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 androidx.core.app.NotificationManagerCompat
9+ import androidx.core.content.getSystemService
10+ import androidx.core.net.toUri
11+ import com.google.firebase.messaging.FirebaseMessagingService
12+ import com.google.firebase.messaging.RemoteMessage
13+ import com.terning.core.firebase.R
14+ import com.terning.core.local.TerningDataStore
15+ import com.terning.navigator.NavigatorProvider
16+ import dagger.hilt.android.AndroidEntryPoint
17+ import timber.log.Timber
18+ import java.util.Random
19+ import javax.inject.Inject
20+
21+ @AndroidEntryPoint
22+ class TerningMessagingService : FirebaseMessagingService () {
23+
24+ @Inject
25+ lateinit var terningDataStore: TerningDataStore
26+
27+ @Inject
28+ lateinit var navigatorProvider: NavigatorProvider
29+
30+ override fun onNewToken (token : String ) {
31+ super .onNewToken(token)
32+
33+ Timber .tag(" okhttp" ).d(" ON NEW TOKEN" )
34+ }
35+
36+ override fun handleIntent (intent : Intent ? ) {
37+ super .handleIntent(intent)
38+
39+ if (intent?.getStringExtra(TITLE )?.isEmpty() == true ) return
40+
41+ val title = intent?.getStringExtra(TITLE ).orEmpty()
42+ val body = intent?.getStringExtra(BODY ).orEmpty()
43+ val type = intent?.getStringExtra(TYPE ).orEmpty()
44+
45+ sendNotification(title = title, body = body, type = type)
46+ }
47+
48+ override fun onMessageReceived (message : RemoteMessage ) {
49+ super .onMessageReceived(message)
50+
51+ if (message.data.isEmpty())
52+ // TODO: 쑰건 μΆκ° by μ΄μ λΉ -> || !terningDataStore.alarmAvailable
53+ return
54+
55+ val title = message.data[TITLE ].orEmpty()
56+ val body = message.data[BODY ].orEmpty()
57+ val type = message.data[TYPE ].orEmpty()
58+
59+ sendNotification(title = title, body = body, type = type)
60+ }
61+
62+ private fun sendNotification (title : String , body : String , type : String ) {
63+ val notifyId = Random ().nextInt()
64+ val intent = navigatorProvider.getMainActivityIntent(deeplink = type).apply {
65+ action = Intent .ACTION_VIEW
66+ data = type.toUri()
67+ flags = Intent .FLAG_ACTIVITY_NEW_TASK or Intent .FLAG_ACTIVITY_CLEAR_TOP
68+ }
69+ val pendingIntent = PendingIntent .getActivity(
70+ this @TerningMessagingService,
71+ notifyId,
72+ intent,
73+ PendingIntent .FLAG_ONE_SHOT or PendingIntent .FLAG_MUTABLE
74+ )
75+ val channelId: String = CHANNEL_ID
76+ val notificationBuilder =
77+ NotificationCompat .Builder (this , channelId).apply {
78+ setSmallIcon(R .mipmap.ic_terning_launcher)
79+ setContentTitle(title)
80+ setContentText(body)
81+ setPriority(NotificationManagerCompat .IMPORTANCE_HIGH )
82+ setContentIntent(pendingIntent)
83+ }
84+
85+ getSystemService<NotificationManager >()?.run {
86+ createNotificationChannel(
87+ NotificationChannel (
88+ channelId,
89+ channelId,
90+ NotificationManager .IMPORTANCE_HIGH ,
91+ ),
92+ )
93+ notify(notifyId, notificationBuilder.build())
94+ }
95+ }
96+
97+ companion object {
98+ private const val CHANNEL_ID : String = " terning"
99+ private const val TITLE : String = " title"
100+ private const val BODY : String = " body"
101+ private const val TYPE : String = " type"
102+ }
103+ }
0 commit comments