-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotificationService.kt
More file actions
133 lines (110 loc) · 4.26 KB
/
NotificationService.kt
File metadata and controls
133 lines (110 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.idle.care.notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.core.app.NotificationCompat
import androidx.core.content.ContextCompat
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.idle.designsystem.binding.R
import com.idle.domain.model.error.ErrorHelper
import com.idle.domain.repositorry.ProfileRepository
import com.idle.domain.repositorry.TokenRepository
import com.idle.presentation.MainActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineExceptionHandler
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.launch
import javax.inject.Inject
@AndroidEntryPoint
class NotificationService : FirebaseMessagingService() {
@Inject
lateinit var tokenRepository: TokenRepository
@Inject
lateinit var profileRepository: ProfileRepository
@Inject
lateinit var errorHelper: ErrorHelper
private val notificationManager: NotificationManager =
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
private val coroutineExceptionHandler = CoroutineExceptionHandler { _, throwable ->
errorHelper.logError(throwable)
}
private val scope = CoroutineScope(Dispatchers.IO + SupervisorJob() + coroutineExceptionHandler)
override fun onNewToken(token: String) {
super.onNewToken(token)
scope.launch {
val userType = profileRepository.getMyUserType()
if (userType.isNotEmpty()) {
tokenRepository.postDeviceToken(
deviceToken = token,
userType = userType,
)
}
}
}
override fun onMessageReceived(message: RemoteMessage) {
super.onMessageReceived(message)
val title = message.notification?.title ?: "케어밋"
val body = message.notification?.body ?: ""
val data = message.data
deliverNotification(
title = title,
body = body,
data = data,
)
}
internal fun deliverNotification(
title: String,
body: String,
data: Map<String, String>
) {
val intent = Intent(this, MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_SINGLE_TOP
}
if (data.isNotEmpty()) {
data.forEach { (key, value) ->
intent.putExtra(key, value)
}
}
val pendingIntent = PendingIntent.getActivity(
this,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_IMMUTABLE,
)
val builder = NotificationCompat.Builder(this, BACKGROUND_CHANNEL)
.setSmallIcon(com.idle.care.R.drawable.ic_notification_icon)
.setColor(ContextCompat.getColor(this, R.color.orange_500))
.setContentTitle(title)
.setContentText(body)
.setContentIntent(pendingIntent)
.setAutoCancel(true)
notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
}
override fun onDestroy() {
super.onDestroy()
scope.cancel()
}
companion object {
private const val BACKGROUND_CHANNEL = "백그라운드 알림"
private const val BACKGROUND_DESCRIPTION =
"센터장 : 공고 지원자 확인, 요양보호사 : 희망 공고가 게시되었을 때의 알림을 받을 수 있는 채널입니다."
fun initNotification(context: Context) {
val channel =
NotificationChannel(
BACKGROUND_CHANNEL,
BACKGROUND_CHANNEL,
NotificationManager.IMPORTANCE_DEFAULT
)
channel.description = BACKGROUND_DESCRIPTION
val notificationManager =
context.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}