0

Notification does not appear (android 4.0) after pressing the button even though the code is mostly copied from their documentation website. Here is my onCreate().

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button);
        final Context context = this;

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL1_ID);
                builder.setSmallIcon(R.drawable.ic_sms_notification);
                builder.setContentTitle(textTitle);
                builder.setContentText(textContent);
                builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);

                NotificationManagerCompat notificationManager = NotificationManagerCompat.from(context);

                notificationManager.notify(0, builder.build());

            }

        });

    }
  • Could you post logs? – Genesis Mar 13 '20 at 01:32
  • Answer could be found in https://stackoverflow.com/questions/16045722/android-notification-is-not-showing – RandomThoughts2468 Mar 13 '20 at 01:42
  • Is it _only_ Android 4.0 that has the issue? What kind of drawable is `ic_sms_notification`? PNG? JPG? Some sort of XML? Are you sure that both `textTitle` and `textContent` are not null or empty? – Mike M. Mar 13 '20 at 01:54
  • @MikeM. I only tried on Android 4.0 cause it says that my app will run on 100% of all devices. ic_sms_notification is an xml which I added as a vector asset. textTitle and textContent are not null caused I initialized them with some values before. – Kamruzzaman Tauhid Mar 13 '20 at 17:40
  • OK, that drawable is the issue; at least, it's one of them. Vector drawables don't work on < 5.0, directly, and since it's the system that's ultimately trying to handle that, even using the support/androidx library compatibility helpers won't work. If you want to use that vector image on versions that support them, you'll need to move it to a `drawable-v21` folder. You need an "old school" drawable for older versions – e.g., PNG, JPG, etc. – and that would go in the plain `drawable` folder. If you don't want to mess with separate versions, just replace what you've got with one of those oldies. – Mike M. Mar 13 '20 at 17:47
  • If you just want to do a quick fix for testing, then, yeah, I would simply replace it with a PNG or JPG. After you get the `Notification` working, you can decide how you want to handle the drawables for various API levels. – Mike M. Mar 13 '20 at 17:57
  • @MikeM. I just tried using a png, it still doesn't work. – Kamruzzaman Tauhid Mar 13 '20 at 18:08
  • At this point, all I can suggest further is to try a clean/rebuild to make sure that that image got replaced correctly in the build. I don't have a 4.0 system available atm to test, though I don't really think there was anything particular with Notifications on older versions, but it's been while. I guess you could also try not using the compat classes, in case there's some bug in those that I'm not aware of. That is, `Notification.Builder` instead of `NotificationCompat.Builder`, and `NotificationManager` instead of `NotificationManagerCompat`. – Mike M. Mar 13 '20 at 18:20

0 Answers0