android - 如何为 channel 上的通知更新禁用声音/振动(API 26+)?

标签 android notifications

我有一个允许用户与通知交互的应用程序。这是一个简单的用例:当用户点击“操作”时,应用程序会进行一些处理并更新通知以显示进度并再次更新以显示操作是否成功。

example flow

在 26 之前,我能够在单个通知上设置声音/振动,因此一旦用户单击“操作”,转换到进度状态不会发出声音/振动(我想要的行为),但是 26,似乎这些参数不再受到尊重,声音/振动设置仅在 channel 级别受到尊重。

我的初始通知应该发出声音/振动,但如果我正在更新现有的(即更改为进度状态),那么它不应该发出声音/振动。有没有办法在 API 26 及更高版本上实现这一点?

下面是设置初始状态的代码:

private fun sendNotification() {

        val builder = NotificationCompat.Builder(this, "channel_id")
        val defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

        val intent = Intent(this, MyIntentService::class.java)
        val pIntent = PendingIntent.getService(this, ID, intent, PendingIntent.FLAG_UPDATE_CURRENT)
        val action = NotificationCompat.Action.Builder(
                R.drawable.ic_lock_open_white_24dp,
                "Action",
                pIntent
        ).build()

        builder.setSmallIcon(R.drawable.ic_home_teal_600_24dp)
                .setContentTitle("My Title")
                .setContentText("My content text")
                .setSound(defaultSoundUri)
                .addAction(action)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
            val channelName = "My Channel"
            val description = "Channel Description"
            val importance = NotificationManager.IMPORTANCE_DEFAULT
            val channel = NotificationChannel("channel_id", channelName, importance)
            channel.description = description
            notificationManager.createNotificationChannel(channel)

        }
        val manager = NotificationManagerCompat.from(this)
        manager.notify(ID, builder.build())
    }

并过渡到进度状态(使用相同的 id)
private fun updateNotification(notificationId: Int, title: String) {

        //This should NOT make sound or vibrate but it does on 26
        val builder = NotificationCompat.Builder(this, "channel_id");
        builder
                .setSmallIcon(R.drawable.ic_home_teal_600_24dp)
                .setContentTitle(title)
                .setProgress(0, 0, true)
                .setContentText("Processing...")

        val manager = NotificationManagerCompat.from(this)
        manager.notify(notificationId, builder.build())
    }

最佳答案

在所有 API 级别上,您可以使用 setOnlyAlertOnce() 禁用通知更新的声音和振动。 :

Set this flag if you would only like the sound, vibrate and ticker to be played if the notification is not already showing.


builder.setOnlyAlertOnce(true)

这将确保对现有通知的更新不会发出声音/振动。

关于android - 如何为 channel 上的通知更新禁用声音/振动(API 26+)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52349288/

相关文章:

android - 使用 accessibilityservice android 获取通知消息

java - android:如何使用Retrofit从以下json接收响应?

java - 从 setOnChronometerTickListener() 重置时,计时器崩溃

android - 如何通过 SharedPreferences 保存解析的推送通知

ios - 带有 fireDate 的通知会立即触发

CSS通知设计问题

java - 在 Android 中通过 HTTP/2 发布流媒体音频

Android Image Picker 从图库中选择多张图片,最多 5 张

android - Ionic Framework Android 构建中的@font-face 加载问题

iphone - 注册地址簿更改,使用 ABAddressBookRegisterExternalChangeCallback