android-notifications - 适用于 Android 11 的 NotificationChannel 的自定义声音不起作用

标签 android-notifications android-11 notification-channel

我有自定义声音的推送通知,直到 android 10。从 Android 11 开始,当通知显示为下拉样式时,附加到通知 channel 的声音停止播放。当它显示为全屏事件时,它会起作用。
这是如何创建通知 channel 的示例源代码

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);
    }
}
我使用上面的通知 channel 并按如下方式触发通知:
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 上则不然。

最佳答案

我也被 android 11 困住了,但我试过了 不同 channel 名称 并且不要 channel channel 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());
        }
    }

关于android-notifications - 适用于 Android 11 的 NotificationChannel 的自定义声音不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65937093/

相关文章:

Android 通知再次出现

android - 以编程方式点击 Android 通知

android - Flutter Android 将 alarmmanager 与通知相结合

java - 由于不符合所有文件访问权限政策,Google Play 商店不断被拒绝

android - 如何访问另一个 Android 应用程序的通知 channel ?

android - 如何在 Android 8 中为 FCM 推送消息指定 Android 通知 channel

android - 在 Android 上触摸通知时显示对​​话框

Android 11 后台 FCM,仅数据且屏幕关闭

android - 无法启动 AVD

Android O 设置以启用应用程序的后台限制