We are going to do a quick tutorial today on how we can generate notifications in Android. Although the standard Android Guide does explain it, but I dont think its a very quick way to understand stuff.

So here we go.

Here are the basic steps –

  1. Create a notification channel
  2. Build a notification
  3. Send a notitification.

How do we create a notitification channel, we do it simply by keeping in mind that we need a unique channel id(“1000111”) and channel name(“cld_notif_channel”). The values do not matter, however they need to be unique.

private void createNotificationChannel() {
NotificationChannel channel = new NotificationChannel("1000111", "cld_notif_channel", NotificationManager.IMPORTANCE_DEFAULT);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
view raw CreateNotif.java hosted with ❤ by GitHub

The notitification channel can be invoked as many number times as required, although once invoked and available it will just not create another if its already present. I would suggest to create your notitification channels as soon as your application opens.

The way you do it is simply by calling the createNotificationChannel() function in your Main Activity.

Step 2 involves creating the notification itself. There are few basic things we need to take care of, it must have a Small Icon available, this one is mandatory along with the Channel Id, which we created above.

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "1000111")
.setContentTitle("Notification Title")
.setContentText("Notification Text")
view raw CreateNotif.java hosted with ❤ by GitHub

Step 2.5 involves creating a notification manager which is actually going to be responsible for sending out notifications.

final NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
view raw CreateNotif.java hosted with ❤ by GitHub

Step 3 involves obviously firing the notification and sadly this again requires a unique id(“1001”) to uniquely identify the notification and send it.

notificationManager.notify("1001", builder.build());
view raw CreateNotif.java hosted with ❤ by GitHub

And there you go, its so simple it hardly would take 10 minutes of your time to learn how to send notifications in your Android application.

*Note – where you see *this* that is android.content.Context so this goes inside your class for the activity.