铃声音量静音时使用闹钟流发出声音通知(Android Oreo)

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

我有一个现有的应用程序能够产生声音通知声音,即使设备通过使用Alarm音频流使其铃声音量静音(假设警报音量非零)。这在Android 7.1.2上运行良好

将我的设备升级到Oreo(8.0.0)后,除非响铃音量非零,否则通知将不再可听。该应用程序没有变化。

所以我试图解决这个问题是重新编译Oreo(SDK 26)API并包含新的NotificationChannel内容,看看它是否有帮助,但事实并非如此。

请注意,此代码是在Service中执行的。

这是一段代码摘录:

int importance = NotificationManager.IMPORTANCE_MAX;
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);

NotificationChannel mChannel = new NotificationChannel("channel1", "Alarms", importance);
mChannel.setDescription("Alarms that alert you immediately");
mChannel.enableLights(true);

mChannel.enableVibration(true);
mChannel.setSound(defaultSoundUri,
   new AudioAttributes.Builder()
      .setUsage(AudioAttributes.USAGE_ALARM)
      .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
      .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
   .build()
);

notificationManager.createNotificationChannel(mChannel);  

Notification.Builder notificationBuilder = new Notification.Builder(this,"channel1")
        .setSmallIcon(R.drawable.ic_stat_ic_notification)
        .setContentTitle("Alarm Message")
        .setContentText(messageBody)
        .setAutoCancel(true)
        .setSound(defaultSoundUri, new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_ALARM)
                .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build())
        .setContentIntent(pendingIntent)
        .setCategory(Notification.CATEGORY_ALARM)
        .setPriority(Notification.PRIORITY_MAX);


Notification n = notificationBuilder.build();
n.flags = n.flags | Notification.FLAG_INSISTENT;
notificationManager.notify(0 /* ID of notification */, n);

有任何想法吗?

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

我也在努力解决这个问题。我还没有发现为什么会这样,但我使用MediaPlayer解决了这个问题。我删除了声音添加到通知中的部分,并用以下内容替换了该部分:

        Uri ringtoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        final MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setDataSource(this.getApplicationContext(), ringtoneUri);
        mediaPlayer.setAudioAttributes(new AudioAttributes.Builder().setLegacyStreamType(AudioManager.STREAM_ALARM).build());
        mediaPlayer.setLooping(false);
        mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
           public final void onCompletion(MediaPlayer it) {
              mediaPlayer.stop();
              mediaPlayer.release();
           }
        });
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
           public final void onPrepared(MediaPlayer it) {
              mediaPlayer.start();
           }
        });
        mediaPlayer.prepareAsync();

0
投票

我有类似的问题,但它出现在Android 7.1.1上

我的解决方案是删除以下行

.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)

我已经搜索了解释为什么删除此标志有效,但我没有找到FLAG_AUDIBILITY_ENFORCED所做的任何信息,除了开发人员文档中的说明,说

定义系统确保声音可听性的行为的标志。

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