【问题标题】:Audible notifications using alarm stream when ring volume is silenced (Android Oreo)铃声静音时使用警报流的声音通知(Android Oreo)
【发布时间】:2017-09-10 05:04:18
【问题描述】:

我有一个现有的应用程序,即使设备使用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


    【解决方案1】:

    我也在这个问题上苦苦挣扎。我还没有发现为什么会发生这种情况,但我使用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();
    

    【讨论】:

      【解决方案2】:

      我遇到了类似的问题,但它在 Android 7.1.1 上弹出

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

      .setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED)
      

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

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

      【讨论】:

        猜你喜欢
        • 2014-01-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-04-24
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        相关资源
        最近更新 更多