This commit is contained in:
Халимов Рустам
2026-04-15 02:40:52 +03:00
parent 9560a9235f
commit 8409c51842
16 changed files with 207 additions and 34 deletions
@@ -0,0 +1,17 @@
package core.notifications.data
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@Singleton
class ActiveChatTracker @Inject constructor() {
private val _currentChatId = MutableStateFlow<String?>(null)
val currentChatId: StateFlow<String?> = _currentChatId.asStateFlow()
fun setChatId(chatId: String?) {
_currentChatId.value = chatId
}
}
@@ -40,46 +40,19 @@ class ForkFirebaseMessagingService : FirebaseMessagingService() {
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
showNotification(title, body, type)
NotificationHelper.showNotification(this, title, body, type, chatId, messageId?.hashCode())
}
private fun showNotification(title: String?, body: String?, type: String?) {
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val channelId = "fork_notifications_channel"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
channelId,
"Fork Messenger Notifications",
NotificationManager.IMPORTANCE_HIGH
)
notificationManager.createNotificationChannel(channel)
}
// Create intent to open MainActivity
val intent = Intent(this, com.knot.messenger.MainActivity::class.java).apply {
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
putExtra("type", type)
}
val pendingIntent = PendingIntent.getActivity(
this, 0, intent,
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0
)
val notificationBuilder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setContentIntent(pendingIntent)
notificationManager.notify(System.currentTimeMillis().toInt(), notificationBuilder.build())
NotificationHelper.showNotification(this, title, body, type)
}
override fun onDestroy() {
@@ -0,0 +1,55 @@
package core.notifications.data
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import androidx.core.app.NotificationCompat
object NotificationHelper {
private const val CHANNEL_ID = "fork_notifications_channel"
fun showNotification(context: Context, title: String?, body: String?, type: String?, chatId: String? = null, notificationId: Int? = null) {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val finalNotificationId = notificationId ?: (System.currentTimeMillis() % Int.MAX_VALUE).toInt()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
CHANNEL_ID,
"Fork Messenger Notifications",
NotificationManager.IMPORTANCE_HIGH
).apply {
description = "Chat and system notifications"
}
notificationManager.createNotificationChannel(channel)
}
// Find our launcher activity via package manager to avoid static dependency on MainActivity
val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)?.apply {
action = Intent.ACTION_MAIN
addCategory(Intent.CATEGORY_LAUNCHER)
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_SINGLE_TOP)
putExtra("type", type)
putExtra("chatId", chatId)
} ?: return
val pendingIntent = PendingIntent.getActivity(
context, System.currentTimeMillis().toInt(), intent,
PendingIntent.FLAG_UPDATE_CURRENT or (if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) PendingIntent.FLAG_IMMUTABLE else 0)
)
val notificationBuilder = NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle(title)
.setContentText(body)
.setAutoCancel(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL)
.setContentIntent(pendingIntent)
notificationManager.notify(finalNotificationId, notificationBuilder.build())
}
}