为什么API级别26及以上的通知声音不起作用?

问题描述 投票:1回答:2

我有这个应用程序,我想要设置自定义通知声音,

我有这个方法检查Android版本并相应地创建通知..

在Android 6及更低版本上它工作正常并播放自定义声音。

但在Android 7及以上它不播放我想要的自定义通知声音

1-堆栈溢出的答案都没有解决这个问题

2-我的代码是以android开发人员docs指定的最佳方式编写的

3-我的文件是.wav和它的44100HZ

这是代码,我错过了什么?

private void createLocalNotificationx(Intent intent) {
    NotificationManager notificationManager;
    PendingIntent pendingIntent = PendingIntent.getActivity(this, (int) System.currentTimeMillis(), intent, 0);

    Uri alarmSound;

    if (type.equals(VALUE_ROUTINE)) {
        alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + getPackageName() + "/" + R.raw.alarm);
    } else {
        alarmSound = Uri.parse(ContentResolver.SCHEME_ANDROID_RESOURCE
                + "://" + getPackageName() + "/" + R.raw.emergency);
    }

    if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.O) {
        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Notification mNotification = new Notification.Builder(this)
                .setOngoing(false)
                .setContentTitle(title)
                .setContentText(body)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pendingIntent)
                .setAutoCancel(true)
                .setSound(alarmSound).setPriority(Notification.PRIORITY_DEFAULT)
                .build();

        notificationManager.notify(0, mNotification);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

        Log.d(TAG, "alarmSound =" + alarmSound.getPath());
        /*First create the notification channel:*/
        String CHANNEL_ID = getPackageName() + "/ID";
        String name = getString(R.string.app_name);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;

        NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
        mChannel.enableVibration(true);
        mChannel.setVibrationPattern(new long[]{100, 200, 500});
        mChannel.enableLights(true);

        // Register the channel with the system; you can't change the importance
        // or other notification behaviors after this
        notificationManager.createNotificationChannel(mChannel);

        Notification mNotification = new Notification.Builder(this, CHANNEL_ID)
                .setDefaults(Notification.DEFAULT_ALL | Notification.DEFAULT_LIGHTS)
                .setOngoing(false)
                .setContentTitle(title)
                .setContentText(body)
                .setSound(alarmSound)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentIntent(pendingIntent)
                .build();


        notificationManager.notify(0, mNotification);

    }

}

注意:它可能是更干净的代码但它应该没问题!

android notifications android-8.0-oreo android-7.1-nougat
2个回答
0
投票

从“Oreo”开始,您无法为通知设置自定义音调。但您可以为特定频道设置自定义音调。该频道的所有通知都将具有此自定义通知音。要为频道设置音调,请按以下步骤操作:

 NotificationChannel reminderChannel = new NotificationChannel(<id>,
            context.getString(R.string.id), NotificationManager.IMPORTANCE_HIGH);
    reminderChannel.setDescription(context.getString(R.string.desc));
    reminderChannel.enableLights(true);
    reminderChannel.enableVibration(true);
    // Creating an Audio Attribute
    AudioAttributes audioAttributes = new AudioAttributes.Builder()
            .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
            .setUsage(AudioAttributes.USAGE_NOTIFICATION_RINGTONE)
            .build();
    Uri alarmSound;
    alarmSound = Uri.parse("android.resource://" + HealthifymeApp.getInstance().getPackageName() + "/" + R.raw.reminder_notification);
    reminderChannel.setSound(alarmSound, audioAttributes);
    mNotificationManager.createNotificationChannel(reminderChannel);

-1
投票

看看你的.setDefaults()方法:

Notification mNotification = new Notification.Builder(this, CHANNEL_ID)
                .setDefaults(Notification.DEFAULT_ALL | Notification.DEFAULT_LIGHTS)

可能Notification.DEFAULT_ALL导致你的问题。

© www.soinside.com 2019 - 2024. All rights reserved.