android notification manager banner

Android Notification Manager: Create Notification in Android

Let us look at how we can create use the Android Notification Manager class to create a simple Notification in Android. In this tutorial I will be explaining how we might create a simple Notification.

Quick Navigation

What is Notification Manager in Android ?

The Notification Manager is simply a class in the Android SDK. Which can be used to show the notification to the user. There are different forms of notifications that can be shown to the user. And by forms I mean the layouts.

You can create your own layout or use the default notification layout provided by the system.

Now that we have learned about the Notification Manager class in Android, let us move on.

What is a Notification Channel in Android ?

Before we dive right into creating notification channel. Let us first understand what it really is. It was introduced in API level 26 or Android 8.0.

But, what really is a notificaion channel then ?

Channel are just grouping of the type of notification your application shows to the user. You can essentially group multiple notifications and mange how they look or sound.

It also makes the users to manage the notifications from your application. For example: they can completely disable the notification from one channel but still allow the notifications to be shown from another one.

Watch the video below if you want more details about Notification Channels in Android.

Also Read: Install Scrcpy: Mirror Your Android Device Wired or Wirelessly

TECHENUM

Creating a Notification Channel

Let us first create our notification channel in our code. Yes we must compulsorily use the notification channel.

Since we will be using Kotlin let us create an object instance as below. And then add the createNotificationChannel() method.

object NotificaionObj { // we will initialize it when we create our notificaion private lateinit var context: Application // this will be initialized lazily private val notificationManager: NotificationManager by lazy { context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } private fun createNotificationChannel(context: Application) { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val name = context.getString(R.string.channel_name) val descriptionText = context.getString(R.string.channel_description) val importance = NotificationManager.IMPORTANCE_DEFAULT val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { description = descriptionText } // Register the channel with the system notificationManager.createNotificationChannel(channel) } } }
Code language: Kotlin (kotlin)

The code above is pretty straight forward still it is not complete. It only shows how we can create a simple notification channel.

One thing to note here is you can create notification channel as soon as your application starts. Even before you want to show any notification at all.

Also Read: Android RecyclerView: How to Insert, Update and Delete Item

TECHENUM

Creating Notification in Android

Now that we have set up the base for creating our notification. Add the method below to the NotificaionObj object created before.

Here I have provided the complete object code, see below.

object NotificationObj { private const val CHANNEL_ID = "random" // pass in your channel name identifier private lateinit var context: Application private val random = Random(System.currentTimeMillis()) private val notificationManager: NotificationManager by lazy { context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager } fun show(context: Application, title: String, content: String) { // store the application as context this.context = context // create channel before doing anything // can be even moved to the Application class createNotificationChannel(context) // prepare notificaion here val builder = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.ic_launcher_foreground) // the icon that will be shown .setContentTitle(title) // title of the notification .setContentText(content) // body of the notification .setPriority(NotificationCompat.PRIORITY_DEFAULT) // adjust the notification priority val notification = builder.build() notificationManager.notify(1996, notification) } private fun createNotificationChannel(context: Application) { // Create the NotificationChannel, but only on API 26+ because // the NotificationChannel class is new and not in the support library if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val name = context.getString(R.string.channel_name) val descriptionText = context.getString(R.string.channel_description) val importance = NotificationManager.IMPORTANCE_DEFAULT val channel = NotificationChannel(CHANNEL_ID, name, importance).apply { description = descriptionText } // Register the channel with the system notificationManager.createNotificationChannel(channel) } } }
Code language: Kotlin (kotlin)

The code above is pretty straight forward. If you are confused please do comment below.

Wrap Up

This was only the basic of how to create a Notification in Android using the ‘Notification Manager’ class. There are possibilities of customizing the notification as you wish.

Keep exploring and keep learning. If you are just starting out with Android you might want to read this: Learn Retrofit in Android GET with Example

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *