2016-03-04 76 views
6

當我將代碼mNotificationBuilder.setLargeIcon(BitmapFactory.decodeResource( getResources(), R.drawable.ic_large_icon));添加到我的通知中時,它會停止工作,不會出現錯誤或警告。這隻發生在Lollipop之前,Lollipop之外,它的效果很好。與「作品」我的意思是通知顯示。NotificationCompat.Builder setLargeIcon()不工作?

我的示例代碼:

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this); 

mBuilder.setSmallIcon(R.drawable.icon); 
mBuilder.setContentTitle("Content Title"); 
mBuilder.setContentText("Content Text"); 
mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_large_icon)); 

startForeground(1, mBuilder.build()); 

我試圖加載方式不同的位圖,但一直沒有... 的圖標爲128×128所以它的大小不應該是一個問題嗎?

我也嘗試了不同的id,但都沒有解決這個問題。

我會非常樂意提供任何建議,請任何推動正確的方向將意味着我的世界。

編輯1#

該通知是由機構出具。該服務處於活動狀態,Log打印告訴我「startForeground()」運行後的代碼。

+0

「的圖標爲128×128所以它的大小不應該是一個問題嗎?」 - 你有什麼目錄(或多個目錄)? – CommonsWare

+0

Res/drawable,也嘗試使用Android Asset Studio(按羅馬)將多個分辨率放入不同的子目錄。但它也沒有幫助。它有可能在什麼地方放置? 要嘗試使用常規NotificationManager類而不是「startForeground()」,但如果這是造成問題的原因,那將會很奇怪。 –

+0

'res/drawable /'幾乎從來都不是正確的答案。這是'res/drawable-mdpi /'的同義詞,因此您的映像將在更高密度的設備上進行升級。因此,在真正高密​​度的設備上,以128x128開始的映像可能會升至512x512,屆時您將超過1MB IPC事務大小限制。你可能想看看你從'decodeResource()'返回的'Bitmap'。 – CommonsWare

回答

11

你必須先設置大圖標,然後是小圖標。

在我的情況下,該代碼工作:

mBuilder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_message)); 
    mBuilder.setSmallIcon(R.mipmap.ic_message); 
    mBuilder.setContentTitle("Inbox"); 
    mBuilder.setContentText("New message received"); 
+2

對我無效 – vgarzom

+0

@vgarzom您找到任何解決方案嗎?這對我也不適用 –

2

棒棒糖之前曾有的通知沒有大的圖標。小圖標應該是64x64,創建時請記住它將以兩種顏色呈現:白色和透明。

NotificationCompat.Builder mBuilder; 

if (SystemTools.isAndroidApiVersionBeforeLollipop()) { 
       mBuilder = 
         new NotificationCompat.Builder(context) 
           .setContentIntent(pendingIntent) 
           .setSmallIcon(iconRid) 
           .setColor(ContextCompat.getColor(context, R.color.transparent)) 
           .setContentTitle(caption) 
           .setContentText(text) 
           .setOngoing(true) 
           .setWhen(0) 
           .setPriority(NotificationCompat.PRIORITY_LOW) 
       ; 
      } else { 
       mBuilder = 
         new NotificationCompat.Builder(context) 
           .setContentIntent(pendingIntent) 
           .setSmallIcon(iconRid) 
           .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), bigIconRid)) 
           .setColor(ContextCompat.getColor(context, R.color.transparent)) 
           .setContentTitle(caption) 
           .setContentText(text) 
           .setOngoing(true) 
           .setWhen(0) 
           .setPriority(NotificationCompat.PRIORITY_LOW) 
       ; 

}