2016-03-04 78 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) 
       ; 

}