41

I am having a strange error with notification manager.

@Override
public void onMessageReceived(String from, Bundle data)
{
     Log.i(TAG, "IP : " + (String) data.get("ip"));
     NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
     Intent acceptNextIntent = new Intent(MainActivity.BROADCAST_KEY_ACCEPT);
//        acceptNextIntent.putExtra("ip", (String) data.get("blah")); //add stuff here
     PendingIntent acceptNextPendingIntent = PendingIntent.getBroadcast(this, 0, acceptNextIntent, 0);

     Intent declineNextIntent = new Intent(MainActivity.BROADCAST_KEY_DECLINE);
     PendingIntent declineNextPendingIntent = PendingIntent.getBroadcast(this, 0, declineNextIntent, 0);

     NotificationCompat.Action acceptAction = new NotificationCompat.Action
                .Builder(R.drawable.common_signin_btn_icon_disabled_focus_light, "Grant Request", acceptNextPendingIntent).build();

     NotificationCompat.Action declineAction = new NotificationCompat.Action
                .Builder(R.drawable.common_signin_btn_icon_focus_dark, "Decline Request", declineNextPendingIntent).build();

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

     notificationManager.notify(1, notification.build()); //ERROR HERE

Error Message:

9.474 9327-9371/com.inh.amnesia_application I/MyGcmListenerService: IP : 128.239.213.39
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime: Process: com.inh.amnesia_application, PID: 9327
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=com.inh.amnesia_application/0x1090085 vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 actions=2 vis=PRIVATE)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at android.app.NotificationManager.notify(NotificationManager.java:222)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at android.app.NotificationManager.notify(NotificationManager.java:194)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.inh.amnesia_application.MyGcmListenerService.onMessageReceived(MyGcmListenerService.java:65)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService.zzt(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService.zzk(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService.zza(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at com.google.android.gms.gcm.GcmListenerService$1.run(Unknown Source)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
11-10 19:49:59.477 9327-9371/com.inh.amnesia_application E/AndroidRuntime:     at java.lang.Thread.run(Thread.java:818)

Does this mean that the icons I am trying to access does not exist? I am not sure how to interpret this error and searching this error message does not yield anything.

mrQWERTY
  • 4,039
  • 13
  • 43
  • 91

4 Answers4

48

You're actually not setting an 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);
dabo248
  • 3,367
  • 4
  • 27
  • 37
  • 2
    I have found the source [code](https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/NotificationManager.java#306) , it seems only target api > lolipop will have the error. – einverne Oct 09 '16 at 09:20
  • 2
    I'm getting this error using Notification to set the LEDs, in which case the icon should be irrelevant as it's not displayed... – Michael Sep 18 '17 at 00:04
10

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    Hi,@CommonsWare, I added a notification on a `Service` and called `setSmallIcon()`. But still getting same error `Caused by: java.lang.IllegalArgumentException: Invalid notification (no valid small icon)` – AGM Tazim Aug 07 '18 at 08:01
  • @AGMTazim: I suggest that you ask a separate Stack Overflow question, where you provide a [mcve], including the code where you create the `Notification` and the resulting stack trace. Perhaps there is an issue with the drawable resource that you are using with `setSmallIcon()`. – CommonsWare Aug 07 '18 at 10:46
7

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.

einverne
  • 6,454
  • 6
  • 45
  • 91
1

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" >
James John McGuire 'Jahmic'
  • 11,728
  • 11
  • 67
  • 78