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?