Invalid notification (no valid small icon)

I made a function to show notifications with Kotlin, but I’m receiving a exception. My function:

fun notification(ticker: String, contentTitle: String, contentText: String){
        val intent = Intent()
        val pending = PendingIntent.getActivity(this@MainActivity,0,intent,0)
        val notification = Notification.Builder(this@MainActivity).setTicker(ticker).setContentTitle(contentTitle)
                .setContentText(contentText)
                .setContentIntent(pending).notification
        notification.flags = Notification.FLAG_AUTO_CANCEL
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(0,notification)
    }

Exception:

01-14 03:02:24.271 8882-8882/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                 Process: tk.thallyssonklein.kapp, PID: 8882
                                                 java.lang.RuntimeException: Unable to start activity ComponentInfo{tk.thallyssonklein.kapp/tk.thallyssonklein.kapp.MainActivity}: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=tk.thallyssonklein.kapp/0x10900a7 vibrate=null sound=null tick defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE secFlags=0x0 secPriority=0)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3320)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416)
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:230)
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822)
                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                     at android.os.Looper.loop(Looper.java:148)
                                                     at android.app.ActivityThread.main(ActivityThread.java:7409)
                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                  Caused by: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(pri=0 contentView=tk.thallyssonklein.kapp/0x10900a7 vibrate=null sound=null tick defaults=0x0 flags=0x10 color=0x00000000 vis=PRIVATE secFlags=0x0 secPriority=0)
                                                     at android.app.NotificationManager.notify(NotificationManager.java:255)
                                                     at android.app.NotificationManager.notify(NotificationManager.java:211)
                                                     at tk.thallyssonklein.kapp.MainActivity.notification(MainActivity.kt:121)
                                                     at tk.thallyssonklein.kapp.MainActivity.onCreate(MainActivity.kt:39)
                                                     at android.app.Activity.performCreate(Activity.java:6904)
                                                     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3267)
                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3416) 
                                                     at android.app.ActivityThread.access$1100(ActivityThread.java:230) 
                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1822) 
                                                     at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                     at android.os.Looper.loop(Looper.java:148) 
                                                     at android.app.ActivityThread.main(ActivityThread.java:7409) 
                                                     at java.lang.reflect.Method.invoke(Native Method) 
                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Someone can to help me?

It has nothing to do with Kotlin. The problem is somewhere in Java API. Try the same in pure java.

1 Like

So, It’s meaning that it’s impossible to show notifications with Kotlin? It’s possible to put Java code into my Kotlin class? If it’s possible I can to make a method that makes the same that my function.

Kotlin just calls appropriate java class from Android API. In your case the error occurs not on kotlin side, but on java side.
Particularly, it seems that Notification.Builder requires additional parameter (small icon) that you have not provided like in example from documentation. In this case the code in kotlin will be exactly the same as in Java not counting new operator which is omitted in kotlin.

But the Notification.Builder(…).setSmallIcon does not exists in Kotlin, but in Java exists.

Kotlin replaces java getSomething/setSomething methods with single property something. Like it is mentioned here: (https://kotlinlang.org/docs/reference/java-interop.html#getters-and-setters). I think that setter methods should be still accessible. If not, you can replace it in chain by calling Notification.Builder(…).apply{smallIcon = ...}.

1 Like

I was able to show the notification, the problem was resolved, thank you for the support. A last question…I don’t found a drawable to smallicon so I put the R.drawable.notification_icon_background just for test. What’s drawable I have use?

Now this completely outside the scope of this discussion (nothing to do with kotlin). You should see Android tutorials for that.

Ok, how I mark this topic as resolved?

It is discussion forum, not an issue tracker. So no resolution.