-3

Hello Can someone advise me how to make a notification without receiving this exception: java.lang.IllegalArgumentException: Invalid notification (no valid small icon): Notification(channel=null pri=0 contentView=null vibrate=null sound=null defaults=0x0 flags=0x0 color=0x00000000 vis=PRIVATE)

The code:

public void notifyThis(String text) {

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    Intent notificationIntent = new Intent(this, RegisterActivity.class);
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationManager manager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
    if(Build.VERSION.SDK_INT >= 26) {
        //When sdk version is larger than26
        String id = "channel_1";
        String description = "143";
        int importance = NotificationManager.IMPORTANCE_LOW;
        NotificationChannel channel = new NotificationChannel(id, description, importance);
        channel.enableLights(true);
        channel.enableVibration(true);//
        manager.createNotificationChannel(channel);
        Notification notification = new Notification.Builder(MapsActivity.this, id)
                .setCategory(Notification.CATEGORY_MESSAGE)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.screenmec))
                .setContentTitle("MECTS.SA")
                .setContentText(text)
                .setContentIntent(contentIntent)
                .setAutoCancel(true)
                .build();
        manager.notify(1, notification); }
    else {
        //When sdk version is less than26
        Notification notification = new NotificationCompat.Builder(MapsActivity.this)
                .setSmallIcon(R.drawable.ic_notification)
                .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.screenmec))
                .setContentTitle("MECTS.SA")
                .setContentText(text)
                .setContentIntent(contentIntent)
                .build();
        manager.notify(1,notification); }
    NotificationManager mannager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
    mannager.notify(0,builder.build());
}
Luthermilla Ecole
  • 666
  • 1
  • 6
  • 14
  • possible duplicate of [Android NotificationManager giving me no valid small icon error](https://stackoverflow.com/questions/33642484/android-notificationmanager-giving-me-no-valid-small-icon-error). – Sayuri Mizuguchi May 21 '20 at 19:09

1 Answers1

3

You're building two notifications. The other is based on an empty builder that does not have small icon or any other content:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

...

NotificationManager mannager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
mannager.notify(0,builder.build());

You can remove this code.

laalto
  • 150,114
  • 66
  • 286
  • 303