Android Notification不显示标题图像JPG或PNG格式

时间:2016-05-16 17:44:12

标签: android

我正在尝试在通知栏中显示图片。但是当我设置图像是JPG格式我的应用程序关闭与android停止进程。当我设置PNG图标时,图标不显示在那里。我调整了我的两种图标32dp的大小,并尝试24dp大小。继承我的代码和截图 -

  public void shownotification(View view){

    Intent intent = new Intent();
    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
    Notification notification = new Notification.Builder(MainActivity.this)
            .setTicker("Tittle")
            .setContentTitle("Content Tittle")
            .setContentText("All details of Content")
            .setSmallIcon(R.drawable.cdc)
            .setContentIntent(pendingIntent).getNotification();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(0,notification);
}

enter image description here

1 个答案:

答案 0 :(得分:0)

  

问题原因是5.0 Lollipop及以上版本   “通知图标必须完全是白色的。”

请参阅以下内容以创建通知图标https://design.google.com/icons/

并将您的代码更改为使用以下代码设置图标

    public void shownotification(View view){    
            Intent intent = new Intent();
            PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,0);
            Notification notification = new Notification.Builder(MainActivity.this)
                    .setTicker("Tittle")
                    .setContentTitle("Content Tittle")
                    .setContentText("All details of Content")            
                    .setContentIntent(pendingIntent).getNotification();
        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            notification.setSmallIcon(R.drawable.cdc_icon_transperent);
        } else { 
            notification.setSmallIcon(R.drawable.cdc);
        } 

            notification.flags = Notification.FLAG_AUTO_CANCEL;
            NotificationManager notificationManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(0,notification);
}

其中R.drawable.cdc_icon_transperent是新的白色图标