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

时间:2018-11-05 08:44:05

标签: android notifications mi

MI Note 5 Pro 中,该版本具有最新的带有Oreo的MI UI 10.0 ,因此当我尝试默认发送推送通知 sound 已禁用,因此,当我为此创建一个频道时,无法以编程方式启用声音

在其他Oreo设备中,将发出通知声音,但在MI自定义Oreo OS中,声音默认为禁用

让我显示我的通知代码

    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())

我还在cannel中设置了 channel.setSound(uri,属性),但声音没有发出

这是通知频道的屏幕截图,看到声音图标被禁用,如何启用?

请帮助

enter image description here

4 个答案:

答案 0 :(得分:1)

我面临着同样的问题,但仍然没有得到满意的答案,但是直到那时我们都可以这样解决它:

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

您需要在之后呼叫此呼叫:

notificationManager.notify(notificationId, notification);

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

为避免一次播放两个声音(对于没有此问题的设备),可以这样关闭声音:

1-对于构建器:

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

2-对于频道:

channel.setSound(null, null);

答案 1 :(得分:0)

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

首先要考虑的是在哪里执行通知通道的注册。它必须在Application.onCreate中执行,以便甚至在通知到达之前就创建通道。我在onMessageReceived中做到了。

第二,我说过它适用于默认通知声音,而不适用于自定义声音,我在创建通知通道时插入了以下代码,它起作用了。

NotificationCompat.Builder notificationBuilder =新的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);
    }

答案 2 :(得分:0)

mBuilder.setPriority(NotificationCompat.PRIORITY_HIGH)

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

答案 3 :(得分:-2)

我在小米Redmi 5上的NotificationChannel加上Andorid 8.1.0上的“ MEG7”也遇到类似的问题。

我使用NotificationManager.IMPORTANCE_HIGH创建一个通知频道:

NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
String CHANNEL_ID_CALL = "call";
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID_CALL, context.getResources().getString(R.string.notification_call_channel_name), NotificationManager.IMPORTANCE_HIGH);
mChannel.setDescription(context.getResources().getString(R.string.notification_call_channel_description));
Uri notificationSound = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
AudioAttributes attributes = new AudioAttributes.Builder().setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .setLegacyStreamType(AudioManager.STREAM_RING)
        .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE).build();
mChannel.setSound(notificationSound, attributes);
mChannel.setVibrationPattern(new long[]{0, 700, 500, 700});
mChannel.enableVibration(true);
mChannel.enableLights(true);

mNotificationManager.createNotificationChannel(mChannel);

当我检查在设备上创建的通知频道时,该频道已成功创建,但是当我从应用程序设置中打开通知设置时,通知声音被禁用。

在其他设备中,通知通道已正确创建并启用了声音。

有人知道如何解决这个问题?