Create Notification Alerts in Android Studio with Kotlin

Create notifications in Android using Kotlin with this example. Learn the full process of adding notifications in your Android app at RRTutors.

Last updated Dec 21, 2021

In this android example , we will see how to create a notification alert in Android Studio by using Kotlin Language.

A notification is a message that can be displayed to the user outside of the standard UI of your programme. When you instruct the system to send a notification, it displays as an icon in the notification area at first. The user opens the notification drawer to view the notification's details.


IMPLEMENTATION:

Step 1: 
Create a new Project in android studio.

Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish

 

Step 2. Go to activity_main.xml file and add the following code

   <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Notification" />
</RelativeLayout>

 

Notification Alert Output

 

Step 3. Open MainActivity.kt file and add the following code.

lateinit var notificationChannel: NotificationChannel
    lateinit var notificationManager: NotificationManager
    lateinit var builder: Notification.Builder
    private val channelId = "1000"
    private val description = "Kotlin Demo Notification"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val btn = findViewById(R.id.btn)
        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager

 

        btn.setOnClickListener {
            val intent = Intent(this, AfterNotification::class.java)
            val pendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationChannel =
                    NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
                notificationChannel.lightColor = Color.BLUE
                notificationChannel.enableVibration(true)
                notificationManager.createNotificationChannel(notificationChannel)
                builder = Notification.Builder(this, channelId).setContentTitle(
                    "Notification Alert "
                ).setContentText("Kotlin Demo Notification")
                    .setSmallIcon(R.drawable.ic_baseline_settings_24).setLargeIcon(
                        BitmapFactory.decodeResource(
                            this.resources, R.drawable
                                .ic_launcher_background
                        )
                    ).setContentIntent(pendingIntent)
            }
            notificationManager.notify(1000, builder.build())
        }
    }

 

In the above code we created NotificationChannel, what is Notification Channel, From android version 8 and above we need to create a channel for Notifications which will give access to similar notifications will comes under same group and user can enable/disable respected notifications based on Notification channel.

 

Build.VERSION_CODES.O - is a reference to API level 26 (Android Oreo which is Android 8)
if ((Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) is TRUE - means the device running the app has Android SDK 26 or up - and the Block of code inside you "if" statement will be executed.


Notification Builder and Notification Properties:
Once you have Builder object, you can set its Notification properties using Builder object as per your requirement. But this is mandatory to set at least following −

 

  • A small icon, set by setSmallIcon()
  • A title, set by setContentTitle()
  • Detail text, set by setContentText()

builder = Notification.Builder(this, channelId).setContentTitle(
                    "Notification Alert "
                ).setContentText("Kotlin Demo Notification")
                    .setSmallIcon(R.drawable.ic_baseline_settings_24).setLargeIcon(
                        BitmapFactory.decodeResource(
                            this.resources, R.drawable
                                .ic_launcher_background
                        )
                    ).setContentIntent(pendingIntent)
            }

 

You pass the Notification object to the system by calling NotificationManager.notify() to send your notification.

notificationManager.notify(1000, builder.build())

 

Step 5: Create a new Empty Activity and enter name (AfterNotificationActivity) and Finish.

Step6: Open activity_after_notification.xml and add the following code

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AfterNotification">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Welcome To RRTutors"
        android:textSize="15sp"
        android:textStyle="bold" />

</RelativeLayout>

 

Step 4. Run the app on emulator or real device, you will get the output as in Video.


Complete Source Code of Detect Connected Network type Android Example 

activity_main.xml file

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Show Notification" />
</RelativeLayout>


 

MainActivity.kt file

import android.app.*
import android.content.Context
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {
    lateinit var notificationChannel: NotificationChannel
    lateinit var notificationManager: NotificationManager
    lateinit var builder: Notification.Builder
    private val channelId = "1000"
    private val description = "Kotlin Demo Notification"
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val btn = findViewById(R.id.btn)
        notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as
                NotificationManager

 

        btn.setOnClickListener {
            val intent = Intent(this, LauncherActivity::class.java)
            val pendingIntent =
                PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                notificationChannel =
                    NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
                notificationChannel.lightColor = Color.BLUE
                notificationChannel.enableVibration(true)
                notificationManager.createNotificationChannel(notificationChannel)
                builder = Notification.Builder(this, channelId).setContentTitle(
                    "Notification Alert "
                ).setContentText("Kotlin Demo Notification")
                    .setSmallIcon(R.drawable.ic_baseline_settings_24).setLargeIcon(
                        BitmapFactory.decodeResource(
                            this.resources, R.drawable
                                .ic_launcher_background
                        )
                    ).setContentIntent(pendingIntent)
            }
            notificationManager.notify(1000, builder.build())
        }
    }
}

 

activity_after_notification.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".AfterNotification">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Welcome To RRTutors"
        android:textSize="15sp"
        android:textStyle="bold" />

</RelativeLayout>

 

Conclusion : In this article we have covered how to create a notification alert and show on a click of button in Android Studio by using Kotlin Language.

Related Tutorials & Resources