应用程序在API级别23崩溃:异常java.lang.IllegalArgumentException:无效的通知(无有效的小图标):

时间:2017-03-03 07:17:54

标签: android android-notifications android-6.0-marshmallow

我正在显示自定义视图通知,除了棉花糖外,它工作正常,而我使用的是24x24,36x36,48x48,72x72像素图标

    android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(m_context);
    Intent i = new Intent(m_context, RunningAppsActivity.class);
    i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent intent = PendingIntent.getActivity(m_context, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
    builder.setTicker(m_context.getResources().getString(com.soopermo.batterybooster.R.string.click_to_optimize));
    builder.setSmallIcon(R.drawable.battry_notify);
    builder.setAutoCancel(true);
    Notification notification = builder.build();
    RemoteViews contentView = new RemoteViews(m_context.getPackageName(),R.layout.status_bar);
    // Set text on a TextView in the RemoteViews programmatically.
    final String text = m_context.getResources().getString(com.soopermo.batterybooster.R.string.draining_apps) ;
    contentView.setTextViewText(com.soopermo.batterybooster.R.id.status_bar_content_desc2, text);
    contentView.setOnClickPendingIntent(com.soopermo.batterybooster.R.id.optimization, intent);
    notification.contentView = contentView;

    // Add a big content view to the notification if supported.
    // Support for expanded notifications was added in API level 16.
    // (The normal contentView is shown when the notification is collapsed, when expanded the
    // big content view set here is displayed.)
    if (Build.VERSION.SDK_INT >= 16) {
        // Inflate and set the layout for the expanded notification view
        RemoteViews expandedView =
                new RemoteViews(m_context.getPackageName(), com.soopermo.batterybooster.R.layout.status_bar);
        notification.bigContentView = expandedView;
        expandedView.setOnClickPendingIntent(com.soopermo.batterybooster.R.id.optimization, intent);
    }
    NotificationManager nm = (NotificationManager) m_context.getSystemService(NOTIFICATION_SERVICE);
    nm.notify(POWER_ISSUES_ID, notification);
    //...........................................................

2 个答案:

答案 0 :(得分:0)

这是因为上面的lollypop(21)android不支持小图标的任何彩色图像。

您必须使用具有白色前景和透明背景的图像。

并像这样使用它。

private int getNotificationSmallIcon()
{
    boolean useWhiteIcon = (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP);
    return useWhiteIcon ? R.drawable.icon_for_above_lollypop : R.drawable.normal_icon;
}

修改

以下是支持21 https://material.io/icons/

以上的图标

答案 1 :(得分:0)

Image Asset选项创建通知图标将是一种很好的做法。如果您正在使用向量,则用户Vector Asset
附上我的Android工作室的截图。

enter image description here

注意从下一个屏幕的下拉列表中选择通知图标。