API 26上的通知声音

Rod*_*nho 29 android push-notification

我有一个我在通知中使用的自定义mp3声音.它适用于API 26以下的所有设备.我也尝试在Notification Channel上设置声音,但仍无法正常工作.它播放默认声音.

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, channelId)
            .setAutoCancel(true)
            .setSmallIcon(R.drawable.icon_push)
            .setColor(ContextCompat.getColor(this, R.color.green))
            .setContentTitle(title)
            .setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification))
            .setDefaults(Notification.DEFAULT_VIBRATE)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentText(message);
        Notification notification = builder.build();
        NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            NotificationChannel channel = new NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT);
            AudioAttributes audioAttributes = new AudioAttributes.Builder()
                    .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                    .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
                    .build();
            channel.setSound(Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.notification), audioAttributes);
            notificationManager.createNotificationChannel(channel);
        }
        notificationManager.notify(1, notification);
Run Code Online (Sandbox Code Playgroud)

Paw*_*ski 43

您可能最初使用默认声音创建了频道.一旦创建了频道,就无法更改.您需要重新安装应用程序或使用新的渠道ID创建渠道.

  • 要更改重要性、声音、灯光、振动、锁定屏幕或 DND 设置,请卸载应用程序并重新安装以清除频道。请参阅 https://developer.android.com/guide/topics/ui/notifiers/notifications.html#ManageChannels,特别是标题为“删除通知渠道”的部分 (2认同)

Fax*_*yev 11

我使用了RingtoneManager,它对我有用.试试thius代码:

 NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this);
    builder.setSmallIcon(android.R.drawable.ic_dialog_alert);
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com/"));
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, intent, 0);
    builder.setContentIntent(pendingIntent);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
    builder.setContentTitle("Notification title");
    builder.setContentText("Notification message.");
    builder.setSubText("Url link.");

    try {
        Uri notification = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.custom_ringtone);
        Ringtone r = RingtoneManager.getRingtone(getApplicationContext(), notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }

    NotificationManager notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(1, builder.build());
Run Code Online (Sandbox Code Playgroud)

  • 此解决方案的问题是用户无法将通知声音静音.它总是播放声音. (6认同)
  • "NotificationCompat.Builder(Context)"是depreacted,通知必须使用"NotificationCompat.Builder(context,String)"指定NotificationChannel Id.所以它不是Oreo/API 26的最佳解决方案. (3认同)
  • 我想知道为什么`setSound`函数不起作用?如文档中所述,通知通道的重要性必须至少为"IMPORTANCE_HIGH"才能播放声音.但无论如何它都不起作用 (3认同)
  • 这个问题针对API 26,但这个答案甚至没有提到NotificationChannel.您根本不会收到有关Android 8.0+设备的通知,甚至没有提及更改通知声音. (3认同)