Android 11中自定义通知渠道的声音无效

9

我已经实现了自定义声音的推送通知,但在安卓10版本之后,当以下拉式样式呈现通知时,与通知渠道相关联的声音不再播放。但在全屏活动中呈现时可以正常播放。

以下是创建通知渠道的示例源代码:

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is new and not in the support library
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

    String channelId = "media_playback_channel_v_01_1_sound"
    String channelName = "Channel High"
        NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_HIGH);
        channel.setDescription("My custom sound");
        channel.setLockscreenVisibility(Notification.VISIBILITY_PUBLIC);

        AudioAttributes.Builder builder = new AudioAttributes.Builder();
        builder.setUsage(AudioAttributes.USAGE_NOTIFICATION);
    String basePath = ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getPackageName() + "/" + R.raw.alarm_sound);
    Uri alarmSound = Uri.parse(basePath);
        channel.setSound(alarmSound, builder.build());

        channel.enableVibration(true);
        channel.enableLights(true);
        channel.setLightColor(Color.RED);
    }
}

我使用上面的通知频道,并按如下方式触发通知:
private void fireNotification(Context context) {
    String channelId = "media_playback_channel_v_01_1_sound"
        NotificationChannel channel = getManager().getNotificationChannel(channelId);


        PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(context, 100,
                fullScreenIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        String contentText = getString(R.string.call_notification_incoming_from, from);

        Bundle args = new Bundle();
        args.putInt(CallActivity.INTENT_CALL_NOTIFICATION_ID, ActiveCall.ANDROID_10_PUSH_CALL_NTFN_ID);
        args.putBoolean(CallActivity.INTENT_FROM_CALL_NOTIFICATION, true);
        args.putString(CallActivity.INTENT_NOTIFICATION_CALL_ID, fullScreenIntent.getStringExtra(CallActivity.INTENT_NOTIFICATION_CALL_ID));

        NotificationCompat.Builder notificationBuilder =
                new NotificationCompat.Builder(this, type)
                        .setSmallIcon(iconRes)
                        .setContentTitle(getString(R.string.app_name))
                        .setContentText(contentText)
                        .setPriority(NotificationCompat.PRIORITY_HIGH)
                        .setCategory(NotificationCompat.CATEGORY_CALL)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setOngoing(true)
                        .setGroupAlertBehavior(NotificationCompat.GROUP_ALERT_ALL)
                        .setTimeoutAfter(Consts.MINUTE)
                        .addExtras(args);

    notificationBuilder.addAction(
        R.drawable.ic_accept_call,
                getString(R.string.call_notification_incoming_answer),
                answerPendingIntent);
        notificationBuilder.addAction(
                        R.drawable.ic_decline_bttn,
                        getString(R.string.call_notification_incoming_reject),
                        rejectPendingIntent
                );
        notificationBuilder.setFullScreenIntent(fullScreenPendingIntent, true);

    // Build
        Notification notification = notificationBuilder.build();
    notification.sound = notificationSoundUri;
        notification.flags |= (Notification.FLAG_SHOW_LIGHTS | Notification.FLAG_INSISTENT Notification.FLAG_NO_CLEAR);
        notification.ledARGB = Color.RED;
        notification.ledOnMS = 300;
        notification.ledOffMS = 1000;

    // Notify
        NotificationManager notificationManager = getManager();
        notificationManager.notify(id, notification);
}

请注意,同一代码在Android 10中可以播放声音,但在Android 11上则不能。

1
你找到解决方案了吗?我在一些三星和索尼的安卓11设备上看到了这个问题,但是在一加和Pixel上没有。 - jayeffkay
1
有关此事有任何更新吗?我也遇到了完全相同的问题... - swalkner
@AngelTerziev请查看我的回答。我找到了原因,你可以检查一下。给出了解释。所有的技巧都需要在通知渠道和通知构建器中完成"Channel Id"。 - Nafis Kabbo
@AngelTerziev 我在一些Realme和Samsung设备上遇到了同样的问题。你找到任何解决方案了吗? - Oliver
@Oliver,你修好了吗? - Oliver D
显示剩余2条评论
2个回答

0

终于,在大量的研究之后,我找到了一个解决方案。你所需要的只是这个:

在创建通知渠道时,为静音通知、自定义声音通知等创建不同的通道。
// 您想要的自定义声音文件名
val soundName = "beep"
val soundUri = Uri.parse("${ContentResolver.SCHEME_ANDROID_RESOURCE}://${packageName}/raw/${soundName}")

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    val importance = NotificationManager.IMPORTANCE_HIGH
    val audioAttributes = AudioAttributes.Builder()
        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
        .setUsage(AudioAttributes.USAGE_NOTIFICATION)
        .build()

    // 这里的 CHANNEL_ID 是创建通道的主要内容。在通知构建器中也使用相同的 CHANNEL_ID。
    val channel = NotificationChannel(CHANNEL_ID, getString(R.string.app_name), importance).apply {
        description = getString(R.string.app_name)
        setSound(null, audioAttributes) // 如果您想要静音通知,请给出 Null
        // setSound(soundUri, audioAttributes)
    }
    notificationManager.createNotificationChannel(channel)
}

通知构建器:

        // Here The CHANNEL_ID needs to be same like the above created channel id. Because it defines how notification sound come.
    val builder = NotificationCompat.Builder(this, CHANNEL_ID)
        .setSmallIcon(smallIcon)

    with(notificationManager) {
        notify(notificationId, builder.build())
    }

0

我也遇到了Android 11的问题,但我尝试了不同的通道名称并且没有使用通道ID。

这个解决方案在所有Android 11版本的设备上都适用于我。

请尝试一下,如果对您不起作用,请告诉我。

public void showNotification(String title, String message) {
        count++;
        Intent intent = new Intent(this, HomePageActivity.class);
        String channel_id = "notification_channel";
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT);
        Uri soundUri = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE + "://" + getApplicationContext().getPackageName() + "/" + R.raw.coin);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), channel_id)
                .setSmallIcon(R.mipmap.ic_launcher_round)
                .setAutoCancel(true)
                .setVibrate(new long[]{1000, 1000, 1000, 1000, 1000})
                .setOnlyAlertOnce(true)
                .setSound(soundUri)
                .setContentIntent(pendingIntent);
        builder.setNumber(count);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            builder = builder.setContent(getCustomDesign(title, message));
        } else {
            builder = builder.setContentTitle(title).setContentText(message).setSmallIcon(R.mipmap.ic_launcher_round);
        }
        NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            NotificationChannel notificationChannel = new NotificationChannel(channel_id, "DIFFRENT_CHANNEL_NAME", NotificationManager.IMPORTANCE_HIGH); // Here I tried put different channel name.
            notificationChannel.setShowBadge(true);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                notificationChannel.canBubble();
            }
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION)
                    .build();

            notificationChannel.canShowBadge();
            notificationChannel.enableVibration(true);
            notificationChannel.setSound(soundUri, audioAttributes);
            notificationManager.createNotificationChannel(notificationChannel);
            notificationManager.notify(0, builder.build());
        }
    }

我正在尝试理解你在这里做了什么。你只是改成了“DIFFRENT_CHANNEL_NAME”吗? - jayeffkay
是的,我使用了不同的通道名称,但我不知道这对于Android 11如何起作用。 - Abhinav Gupta

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