【发布时间】:2020-05-25 20:42:38
【问题描述】:
在我的应用程序中,我有几个活动,其中一个是聊天。在此屏幕中,我有与我聊天的人的 ID,每次我发送消息时,都会发送一条推送通知,其中包含消息和与我聊天的人的 ID。当我与此人聊天时,我需要阻止来自他们的通知,并且仅在我没有与该人进行公开聊天时才接收它们。我该怎么做呢?我使用 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())
}
}
【问题讨论】:
标签: android kotlin push-notification firebase-cloud-messaging