2017-09-10 60 views
2

我有現有的應用程序,是能夠甚至產生可聽通知聲音時,該設備有其環體積通過使用Alarm音頻流沉默(假設警報音量不爲零)。這在Android 7.1.2聽覺通知(Android奧利奧)

工作得很好我的設備升級到奧利奧(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); 

任何想法?

回答

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呢,除了從開發者文檔中描述的任何信息,稱

標誌定義了系統將確保聲音的可聽度的行爲。