В моем приложении у меня есть несколько занятий, одно из которых - чат. На этом экране у меня есть идентификатор того, кто разговаривает со мной, и каждый раз, когда я отправляю сообщение, отправляется уведомление pu sh с сообщением и идентификатором человека, с которым я разговариваю. Пока я болтаю с этим человеком, мне нужно заблокировать уведомления от него и получать их только в том случае, если у меня нет открытого чата с этим человеком. Как мне это сделать? Я использую Firebase в качестве серверной части.
class MyFirebaseMessagingService : FirebaseMessagingService() {
override fun onMessageReceived(remoteMessage: RemoteMessage) {
remoteMessage.notification?.let { notification ->
val data = remoteMessage.data
val type = data["type"] // chat message,like etc
val id = data["id"] //userId
val notificationChannelId = getString(R.string.default_notification_channel_id)
val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)
val intent = setIntent(remoteMessage).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
val pendingIntent = PendingIntent.getActivity(this,
0,
intent,
PendingIntent.FLAG_CANCEL_CURRENT)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationChannel = NotificationChannel(notificationChannelId, CHANNEL_SPEEACHT, NotificationManager.IMPORTANCE_DEFAULT)
notificationManager.createNotificationChannel(notificationChannel)
}
val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId)
.setContentTitle(notification.title ?: "")
.setContentText(notification.body ?: "")
.setSmallIcon(R.drawable.ic_stat)
.setSound(soundUri)
.setColor(ContextCompat.getColor(applicationContext, R.color.colorNotification))
.setAutoCancel(true)
.setContentIntent(pendingIntent)
notificationManager.notify(0, notificationBuilder.build())
}
}