59 lines
2.0 KiB
Kotlin
59 lines
2.0 KiB
Kotlin
package core.notifications.data
|
|
|
|
import com.google.firebase.messaging.FirebaseMessagingService
|
|
import com.google.firebase.messaging.RemoteMessage
|
|
import auth.data.remote.api.AuthApi
|
|
import dagger.hilt.android.AndroidEntryPoint
|
|
import kotlinx.coroutines.CoroutineScope
|
|
import kotlinx.coroutines.Dispatchers
|
|
import kotlinx.coroutines.SupervisorJob
|
|
import kotlinx.coroutines.launch
|
|
import javax.inject.Inject
|
|
|
|
@AndroidEntryPoint
|
|
class KnotFirebaseMessagingService : FirebaseMessagingService() {
|
|
|
|
@Inject
|
|
lateinit var authApi: AuthApi
|
|
|
|
@Inject
|
|
lateinit var activeChatTracker: ActiveChatTracker
|
|
|
|
private val job = SupervisorJob()
|
|
private val scope = CoroutineScope(Dispatchers.IO + job)
|
|
|
|
override fun onNewToken(token: String) {
|
|
super.onNewToken(token)
|
|
// Отправляем новый токен на сервер
|
|
scope.launch {
|
|
try {
|
|
authApi.updatePushToken(token)
|
|
} catch (e: Exception) {
|
|
// Ошибка логируется или игнорируется (сервер может быть недоступен)
|
|
}
|
|
}
|
|
}
|
|
|
|
override fun onMessageReceived(message: RemoteMessage) {
|
|
super.onMessageReceived(message)
|
|
android.util.Log.d("FCM", "Message received: ${message.data}")
|
|
|
|
val title = message.notification?.title ?: message.data["title"]
|
|
val body = message.notification?.body ?: message.data["body"]
|
|
val type = message.data["type"] // "chat", "call", "story"
|
|
val chatId = message.data["chatId"]
|
|
val messageId = message.data["id"] ?: message.messageId
|
|
|
|
NotificationHelper.showNotification(this, title, body, type, chatId, messageId?.hashCode(), activeChatTracker.totalUnreadCount.value)
|
|
}
|
|
|
|
private fun showNotification(title: String?, body: String?, type: String?) {
|
|
NotificationHelper.showNotification(this, title, body, type, totalCount = activeChatTracker.totalUnreadCount.value)
|
|
}
|
|
|
|
override fun onDestroy() {
|
|
job.cancel()
|
|
super.onDestroy()
|
|
}
|
|
}
|