Android NotificationManager giving me “no valid small icon” error

做~自己de王妃 提交于 2019-12-03 09:33:53

You're actually not setting a icon for the push notification. Add .setSmallIcon(R.drawable.your_icon) to your notification.

 NotificationCompat.Builder notification = new NotificationCompat.Builder(this)
            .setContentTitle("New Password Request From " + (String) data.get("ip"))
            .setSmallIcon(R.drawable.your_icon)
            .addAction(acceptAction)
            .addAction(declineAction);

You have not called setSmallIcon() on the NotificationCompat.Builder. This provides the icon that will go in the status bar while the Notification is active.

According to the Android NotificationManager Source Code

if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
    if (notification.getSmallIcon() == null) {
        throw new IllegalArgumentException("Invalid notification (no valid small icon): "
                + notification);
    }
}

This error only happened when you set target API > LOLLIPOP_MR1(22) and notification do not have a small icon.

This suddenly started happening to my application. After some research, I found this change in my manifest file actually started causing it.

from:

  <uses-sdk android:minSdkVersion="21" />

to:

  <uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />

I saw somewhere that somebody noted that it only checks for the icon if the target sdk version is set. Sure enough, in the source, there is this :

if (mContext.getApplicationInfo().targetSdkVersion > Build.VERSION_CODES.LOLLIPOP_MR1) {
    if (notification.getSmallIcon() == null) {
        throw new IllegalArgumentException("Invalid notification (no valid small icon): "
                + notification);
    }
}

Luckily, the solution was simple, just define the icon in the manifest:

  <application android:label="MyApp" android:icon="@mipmap/icon" >
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!