简体   繁体   中英

App crashes on API level 23 : Exception java.lang.IllegalArgumentException: Invalid notification (no valid small icon):

I am displaying a custom view notification, it is working perfect except in marshmallow ,while i am using 24x24 ,36x36, 48x48, 72x72 px icon

    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);
    //...........................................................

This is happening because above lollypop(21) android doesn't support any colorized image for small icon.

you have to use a image that has white foreground and transparent background.

And use it like this.

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;
}

Edit

Here are icons that supports on above 21 https://material.io/icons/

Creating notification icons from Image Asset option would be the good practice. If you are working with vectors, user Vector Asset
Attaching screenshot of my Android studio.

在此处输入图片说明

Note : Select notification icons from the dropdown in next screen.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM