Notification is a kind of message, alert, or status of an application (probably running in the background) that is visible or available in the Android’s UI elements. This application could be running in the background but not in use by the user. The purpose of a notification is to notify the user about a process that was initiated in the application either by the user or the system. This article could help someone who’s trying hard to create a notification for developmental purposes.
Notifications could be of various formats and designs depending upon the developer. In General, one must have witnessed these four types of notifications:
- Status Bar Notification (appears in the same layout as the current time, battery percentage)
- Notification drawer Notification (appears in the drop-down menu)
- Heads-Up Notification (appears on the overlay screen, ex: Whatsapp notification, OTP messages)
- Lock-Screen Notification (I guess you know it)
In this article, we will be discussing how to produce notifications in Kotlin.
Step by Step Implementation
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.
Step 2: Working with the activity_main.xml file
Go to the activity_main.xml file and refer to the following code. In this step, we are going to design our layout page. Here, we will use the RelativeLayout to get the Scroll View from the Kotlin file. Below is the code for the activity_main.xml file.
Step 3: Create a new empty activity
Name the activity as afterNotification. When someone clicks on the notification, this activity will open up in our app that is the user will be redirected to this page. Below is the code for the activity_after_notification.xml file.
Note 1: Without configuring Notification Channels, you cannot build notification for applications with Android API >=26. For them generating a notification channel is mandatory. Apps with API<26 don’t need notification channels they just need notification builder. Each channel is supposed to have a particular behavior that will be applicable to all the notifications which are a part of it. Every channel will, therefore, have a Channel ID which basically will act as a unique identifier for this Channel which will be useful if the user wants to differentiate a particular notification channel. In contrast, the Notification builder provides a convenient way to set the various fields of a Notification and generate content views using the platform’s notification layout template but is not able to target a particular notification channel.
Note 2: If you have referred any other documentation or any other blog before this one, you might have noticed them appealing to implement the following dependency“com.android.support:support-compat:28.0.0”. What I’ve personally experienced is that there’s no need to implement it, things go far and good without this also.
Step 5: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
With this, we have now successfully created a “Notification” for our application. Note that the parameters listed in the above code are required and the absence of any single parameter could result in crashing or not starting the application. The content title, content text, small icon are customizable parameters but are mandatory also. One can change their values according to the requirement.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.