当前位置: 首页 > 知识库问答 >
问题:

Android 11的NotificationChannel自定义声音不起作用

公良玺
2023-03-14
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);
}

共有1个答案

濮阳和泰
2023-03-14

我也为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());
        }
    }
 类似资料:
  • 我已经用Firebase实现了通知,通知工作正常。但我需要播放一个自定义声音来通知。我已经实施了 playSound:true,声音:const RawResourceAndroidNotificationSound('通知'), 并在android\app\src\main\res\raw文件夹中添加了声音,该文件夹名为“通知”。mp3’。但通知系统上没有声音。 还实现了有效负载中的代码。 主功

  • 自定义 UISlider 的外观,可以用于声音调节滑块。采用的是分隔的块表示声音的大小。 [Code4App.com]

  • 我有这个祝酒词要出现,一切都很好,除了音频。我试图将toast.mp3放在不同的位置,如项目主文件夹、资产等,甚至使用了d:/myprojectpath/Assets/toast.mp3这样的路径,但通知仍然是静默的。我是个初学者,所以也许我错过了什么...当然,我也在寻找解决方案,但没有任何帮助。下面是我的代码:

  • 字体始终显示默认字体。 我的字体存储在资源/字体中。我尝试使用其他字体,并对字体进行重新编码。此外,PixlUI库并没有解决这个问题。 主要活动。Java语言 activity\u main。xml

  • 我正在从Firebase向我的Android应用程序发送推送通知,但它仅在收到通知时播放默认声音。 我在fcm通知对象中设置了自定义声音参数{“sound”:“notificationsound.mp3”},该文件根据(https://firebase.google.com/docs/cloud-messaging/http-server-ref)但它仍然在所有应用程序状态(背景、前景和终止)上播

  • 问题内容: 我正在尝试呈现jtable的特定行(它应该具有不同的背景色,并且应该具有粗体字符)。 我发现了几个有关如何实现自定义的问题,以及有关如何使用表以及本教程(表行渲染)的问题。 我正在使用来显示。我尝试实现两种解决方案,但都没有实现。调试时,可以看到正在创建新的jtable,但是没有调用方法中的断点。 我的代码如下: 如您所见,模型是我对的实现。 当然,我缺少了一些东西。我试图打电话看看是