在通知渠道中启用声音按钮

14

在最新版本的MI UI 10.0 with Oreo中的 MI Note 5 Pro上,当我尝试通过默认方式发送通知推送(push notification)时,声音(sound)被禁用,所以当我为其创建一个通道(channel)时,无法通过程序启用声音。

在其他Oreo设备上,通知声音是正常的,但在MI定制的Oreo操作系统中,声音默认是禁用的。

让我展示一下我的通知代码::

    var intent = Intent(mContext, HomeActivity::class.java)
    intent.putExtra(Constants.EXTRA_FROM_NOTIFICATION, true)
    var pendingIntent = PendingIntent.getActivity(mContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)

    var uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)

    var mBuilder = NotificationCompat.Builder(mContext, NOTIFICATION_CHANNEL_ID)
            .setContentTitle(mContext.getString(R.string.app_name))
            .setContentText(mFirstContactName + " : " + mListChatWindow[0].message)
            .setPriority(if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) NotificationManager.IMPORTANCE_HIGH else Notification.PRIORITY_HIGH)
            .setContentIntent(pendingIntent)
            .setSound(uri)
            .setSmallIcon(R.drawable.ic_app_icon)
            .setColor(ContextCompat.getColor(mContext, R.color.colorPrimary))
            .setVibrate(longArrayOf(0, 100, 1000, 300))
            .setAutoCancel(true)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        var channel = NotificationChannel(NOTIFICATION_CHANNEL_ID, "NOTIFICATION_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH)
        channel.description = "NOTIFICATION_DESCRIPTION"
        channel.lightColor = Color.LTGRAY
        channel.enableVibration(true)
        channel.lockscreenVisibility = Notification.VISIBILITY_PUBLIC

        val attributes = AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_NOTIFICATION).build()
        channel.setSound(uri, attributes)
        var notificationManager = mContext.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.createNotificationChannel(channel)
    }

    var notificationManager = NotificationManagerCompat.from(mContext)
    notificationManager.notify(NOTIFICATION_CHAT_ID, mBuilder.build())

我在通道中设置了 channel.setSound(uri, attributes),但没有听到声音。

这是通知通道的截图,请注意声音图标已禁用,如何启用?

请帮忙。

enter image description here


1
似乎是 MIUI 10 的问题,在 Pocofone 上遇到了类似的问题。 - aksh1618
6
我也有同感...一定有诀窍,因为WhatsApp、Facebook和其他应用程序都能做到这一点。我们缺少某些东西... - Nishanth Sreedhara
我认为,MiUI有一个白名单应用程序默认启用声音通知。 - San Juan
1
到目前为止有任何解决方案吗?我在MIUI设备上的Flutter应用程序中遇到了类似的问题,无法接收默认声音。 - Parth Rajani
仍然面临Xiaominexus6p设备的问题。有没有人有解决方案? - Abu Yousuf
显示剩余3条评论
3个回答

3

我也遇到了同样的问题,但是还没有得到令人满意的答案,不过在那之前我们可以通过以下方式解决:

final Uri NOTIFICATION_SOUND = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
RingtoneManager.getRingtone(context, NOTIFICATION_SOUND).play();

在此之后,您需要调用此函数:

notificationManager.notify(notificationId, notification);

这样,即使“允许声音”被关闭,您的应用程序仍将始终播放声音,并且播放的声音将来自系统而不是媒体(这是通知的预期行为)。

为了避免在某些设备上同时播放两个声音(对于没有此问题的设备),您可以像以下方式关闭声音:

1- 对于Builder:

notificationBuilder.setContentTitle(title)
    .set.....
    .set.....
    .setSound(null);

2- 对于通道:

channel.setSound(null, null);

3

我在MIUI Global 10.1中使用oreo时也遇到了类似的问题,但只出现在使用自定义声音而不是默认声音时。无论如何,让我解释一下我是如何解决它的,希望它也能解决你的问题。

首先要考虑的是通知频道的注册位置。必须在Application.onCreate中执行,以便在通知到达之前就创建频道。我曾在onMessageReceived中执行此操作。

第二点,正如我所说,对于默认通知声音可以工作,但对于自定义声音则不能。我在创建通知频道时插入了以下代码,然后它就起作用了。

NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this,CHANNEL_ID);

    if (Build.VERSION.SDK_INT>= Build.VERSION_CODES.O) {
        notificationBuilder.setDefaults(Notification.DEFAULT_SOUND); // This line did the magic for me.

        Uri sound = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sound_notification_plucky);
        AudioAttributes audioAttributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                .build();

        CharSequence name = "MyChild";
        String description = "All MyChild messages";
        int importance = NotificationManager.IMPORTANCE_HIGH;

        NotificationChannel notificationChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        notificationChannel.setDescription(description);
        notificationChannel.enableVibration(true);
        notificationChannel.setSound(sound, audioAttributes);

        NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        notificationManager.createNotificationChannel(notificationChannel);
    }

0
mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)

这行代码对我很有用,它启用了所有通知设置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接