【问题标题】:How to dismiss all desired notifications on Android?如何在 Android 上关闭所有需要的通知?
【发布时间】:2021-07-01 13:45:12
【问题描述】:

我正在创建一个通过 Firebase Cloud Functions 发送和接收通知的聊天应用。假设我有 2 个朋友,每个朋友向我发送 5 条消息(5 条通知)。当我在我的聊天列表中打开一个聊天时,我希望关闭该聊天的所有通知,而不是其他通知。

目前我只能忽略 1 个通知。如果我收到多个通知,notificationManager.cancel(notificationId) 将不起作用。我不想使用notificationManager.cancelAll(),因为我只想关闭一些通知。

override fun onMessageReceived(remoteMessage: RemoteMessage) {

    remoteMessage.notification.let { notification ->

        val type = remoteMessage.data[NOTIFICATION_TYPE]

        val notificationChannelId = getNotificationChannelId(type)
        val soundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val intent = Intent(this, HomeActivity::class.java)
        val notificationIntent = setIntent(remoteMessage.data)
        val stackBuilder: TaskStackBuilder = TaskStackBuilder.create(this)
        stackBuilder.addParentStack(HomeActivity::class.java)
        stackBuilder.addNextIntent(intent)
        stackBuilder.addNextIntent(notificationIntent)
        val pendingIntent: PendingIntent =
            stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)

        val notificationManager =
            getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

        val notificationBuilder = NotificationCompat.Builder(this, notificationChannelId)
            .setContentTitle(notification?.title ?: "")
            .setContentText(notification?.body ?: "")
            .setStyle(
                NotificationCompat.BigTextStyle()
                    .bigText(notification?.body ?: "")
            )
            .setSmallIcon(R.drawable.ic_stat)
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
            .setSound(soundUri)
            .setColor(ContextCompat.getColor(applicationContext, R.color.colorNotification))
            .setVibrate(vibrationPattern)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)

        notify(remoteMessage.data, notificationManager, notificationBuilder)

    }
}

private fun notify(
    data: Map<String, String>,
    notificationManager: NotificationManager,
    notificationBuilder: NotificationCompat.Builder
) {

    when (data[NOTIFICATION_TYPE]) {
        CHAT_MSG -> {
            if (data[NotificationUtils.CHAT_ID] != ChatActivity.currentChat) {
                notificationManager.notify(
                    CHAT_MSG_NOTIFICATION_ID,
                    notificationBuilder.build()
                )
            }
        }
        FRIEND_REQUEST -> {
            notificationManager.notify(
                FRIEND_REQUEST_NOTIFICATION_ID,
                notificationBuilder.build()
            )
        }
    }
}

OnStart():

fun dismissNotifications(notificationId: Int) {
    val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.cancel(notificationId)
}

【问题讨论】:

    标签: android firebase-cloud-messaging android-notifications


    【解决方案1】:

    我的想法是:
    将所有通知 ID 保存在地图中:
    键是消息中的用户名/用户 ID。
    值为 notificationId 列表

    当您需要清除所有通知时,您只需要求您映射并清除该 userName/userId 的所有通知 ID

    【讨论】:

      猜你喜欢
      • 2021-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多