Skip to content

Commit

Permalink
알림 표시 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
kts6056 committed Sep 10, 2024
1 parent e9342b7 commit 125d009
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 9 deletions.
8 changes: 0 additions & 8 deletions presentation/notification/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<application>
<meta-data
android:name="com.google.firebase.messaging.default_notification_channel"
android:value="default" />

<meta-data
android:name="com.google.firebase.messaging.default_notification_icon"
android:resource="@mipmap/ic_launcher" />

<service
android:name=".ChevitFirebaseMessagingService"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,32 @@
package com.dkin.chevit.presentation.notification

import android.Manifest.permission.POST_NOTIFICATIONS
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.pm.PackageManager.PERMISSION_GRANTED
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.content.ContextCompat
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import com.dkin.chevit.domain.usecase.notification.UpdatePushTokenUseCase
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import timber.log.Timber
import javax.inject.Inject
import com.dkin.chevit.presentation.resource.R as ChevitResource

@AndroidEntryPoint
class ChevitFirebaseMessagingService : FirebaseMessagingService() {
@Inject
lateinit var updatePushTokenUseCase: UpdatePushTokenUseCase

@Inject
lateinit var notificationManagerCompat: NotificationManagerCompat

private val processLifecycleScope by lazy {
ProcessLifecycleOwner.get().lifecycleScope
}
Expand All @@ -25,4 +38,45 @@ class ChevitFirebaseMessagingService : FirebaseMessagingService() {
updatePushTokenUseCase(param)
}
}

override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
if (hasNotificationChannel().not()) {
createNotificationChannel()
}
if (ActivityCompat.checkSelfPermission(this, POST_NOTIFICATIONS) == PERMISSION_GRANTED) {
notificationManagerCompat.notify(message.hashCode(), message.toNotification())
}
}

private fun RemoteMessage.toNotification(): Notification {
val notification =
NotificationCompat.Builder(this@ChevitFirebaseMessagingService, "default")
.setColor(
ContextCompat.getColor(
this@ChevitFirebaseMessagingService,
ChevitResource.color.blue_7
)
)
.setSmallIcon(com.dkin.chevit.presentation.resource.R.drawable.ic_notification_logo)
.setContentTitle(notification?.title)
.setContentText(notification?.body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.build()
return notification
}

private fun createNotificationChannel() {
val channel = NotificationChannel(
"default",
"알림 기본 채널",
NotificationManager.IMPORTANCE_HIGH
)
notificationManagerCompat.createNotificationChannel(channel)
}

private fun hasNotificationChannel(): Boolean {
return notificationManagerCompat.getNotificationChannel("default") != null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dkin.chevit.presentation.notification.di

import android.content.Context
import androidx.core.app.NotificationManagerCompat
import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.android.qualifiers.ApplicationContext
import dagger.hilt.components.SingletonComponent
import javax.inject.Singleton

@Module
@InstallIn(SingletonComponent::class)
internal class NotificationModule {
@Singleton
@Provides
fun provideNotificationManagerCompat(
@ApplicationContext context: Context
): NotificationManagerCompat {
return NotificationManagerCompat.from(context)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="83dp"
android:height="83dp"
android:viewportWidth="83"
android:viewportHeight="83">
<group>
<clip-path android:pathData="M0.5,0.965h82v82h-82z" />
<path
android:fillColor="#3531FF"
android:pathData="M23.151,14.498L65.018,21.846A5.076,5.076 54.954,0 1,69.14 27.723L65.725,47.18A5.076,5.076 54.954,0 1,59.848 51.302L17.982,43.955A5.076,5.076 54.954,0 1,13.86 38.078L17.275,18.62A5.076,5.076 54.954,0 1,23.151 14.498z" />
<path
android:fillColor="#4A47FF"
android:fillType="evenOdd"
android:pathData="M27.769,26.003C27.769,25.35 27.24,24.821 26.587,24.821H13.585C10.782,24.821 8.509,27.094 8.509,29.897V67.251C8.509,70.054 10.782,72.326 13.585,72.326H69.415C72.218,72.326 74.491,70.054 74.491,67.251V29.897C74.491,27.094 72.218,24.821 69.415,24.821H56.522C55.869,24.821 55.34,25.35 55.34,26.003C55.34,26.655 54.812,27.184 54.159,27.184H28.95C28.298,27.184 27.769,26.655 27.769,26.003Z" />
<path
android:fillColor="#00000000"
android:pathData="M28.006,18.378L28.313,16.605C28.79,13.843 31.416,11.991 34.179,12.468L55.665,16.183C58.428,16.66 60.28,19.287 59.802,22.049L59.495,23.822"
android:strokeWidth="3.66818"
android:strokeColor="#3532FE" />
<path
android:fillColor="#ffffff"
android:pathData="M49.509,38.571L39.134,49.661C38.649,50.179 37.833,50.199 37.324,49.704L33.59,46.076C33.117,45.617 32.371,45.596 31.874,46.028L29.044,48.484C28.485,48.97 28.459,49.83 28.988,50.349L37.321,58.518C37.831,59.018 38.652,58.999 39.138,58.476L54.058,42.438C54.562,41.896 54.498,41.039 53.918,40.579L51.224,38.443C50.707,38.033 49.961,38.088 49.509,38.571Z" />
</group>
</vector>

0 comments on commit 125d009

Please sign in to comment.