2017-06-22 27 views
2

按我已經在應用中使用的變化和context.startForegroundService(Intent)然後像以前一樣調用服務startForeground開始在後臺服務的文檔。的Android O不可顯示前景化的服務通知

NotificationCompat.Builder notification = new 
NotificationCompat.Builder(getApplicationContext()) 
      .setSmallIcon(R.mipmap.icon) 
      .setContentText("Content") 
      .setOngoing(true) 
      .setContentInfo("Info") 
      .setContentTitle("Title"); 
startForeground(1, notification.build()); 

這正確地顯示在Android N器件上,但是在Android O設備不顯示通知它只顯示新「是在後臺運行...點擊有關電池更多的細節和所述通知數據使用」

有缺什麼去在AndroidØ正確顯示的通知?

回答

2

是,使用一個信道作爲NotificationCompat類的構造符參數。

+0

你能對你所說的這是什麼展開notfication? – Ryan

+0

哦,你的意思是在beta支持庫的新構造?不幸的是,由於外部限制,我無法使用beta庫。我只是做一個庫面向未來的用戶,我會更新庫,當他們移動到V26 – Ryan

+0

是,使用v26.0.0-β2 – greywolf82

1

答案上面並沒有爲我工作。您必須創建頻道才能工作,然後傳遞頻道ID。

NotificationManager notificationManager = 
     (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

String channelId = "some_channel_id"; 
CharSequence channelName = "Some Channel"; 
int importance = NotificationManager.IMPORTANCE_LOW; 
NotificationChannel notificationChannel = new NotificationChannel(channelId, channelName, importance); 
notificationManager.createNotificationChannel(notificationChannel); 

然後創建具有相同cannelId

new NotificationCompat.Builder(this, channelId)