Notification is a message that is used to display some short messages outside of our main application. Even if the app is not running, notifications will still work. Notifications have the following contents: an icon, title of notification and some text content.
In this article we will be discussing how to produce notifications in Kotlin .
Let’s start by first creating a project in Android Studio. To do so, follow these instructions:
- Click on File, then New and then New Project and give name whatever you like
- Then, select Kotlin language Support and click next button
- Select minimum SDK, whatever you need.
- Select Empty activity and then click finish.
Modify activity_main.xml file
Second step is to design our layout page. Here, we will use the RelativeLayout to get the Scroll View from the Kotlin file.
Create a new activity named afterNotification. For this go to res/layout then right click and select new then activity, Empty Activity.When someone click on the notification, this activity will open up in our app that is the user will be redirected to this page.
Modify activity_after_notification.xml file
Note: 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 doesn't need notification channel they just need notification builder.Each channel is supposed to have a particular behavior which 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 user wants to differentiate a particular notification channel. In contrast, 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.
Modify MainActivity.kt file
Open app/src/main/java/yourPackageName/MainActivity.kt. In this file, paste the following:
Run as Emulator:
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.