Notification in Android Kotlin.

Harshita Bambure
1 min readOct 4, 2021

--

In android, we used notifications so today we will learn about how to use notifications in android Kotlin.

Let's start with the designing part in activity_main.xml

<?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=".MainActivity">

<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Send Notification" />

</RelativeLayout>

now we need to create a design for the Notification Message activity.

activity_notification.xml

<?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=".NotificationMessage">

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello, Harshita welcome to notification."
android:textSize="15sp"
android:textStyle="bold" />

</RelativeLayout>

now we write a code in MainActivity.kt file

package com.example.mynotification

import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
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.widget.Button
import android.widget.RemoteViews

class MainActivity : AppCompatActivity() {

lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
private val channelId = "id.notifications"
private val description = "my notification"

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)



val btn = findViewById<Button>(R.id.btn)


notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager


btn.setOnClickListener {


val intent = Intent(this, NotificationMessage::class.java)


val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)


val contentView = RemoteViews(packageName, R.layout.activity_notification)

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
notificationChannel = NotificationChannel(channelId, description, NotificationManager.IMPORTANCE_HIGH)
notificationChannel.enableLights(true)
notificationChannel.lightColor = Color.GREEN
notificationChannel.enableVibration(false)
notificationManager.createNotificationChannel(notificationChannel)

builder = Notification.Builder(this, channelId)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_message)
.setLargeIcon(BitmapFactory.decodeResource(this.resources,
R.drawable.ic_message
))
.setContentIntent(pendingIntent)
} else {

builder = Notification.Builder(this)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_message)
.setLargeIcon(BitmapFactory.decodeResource(this.resources,
R.drawable.ic_message
))
.setContentIntent(pendingIntent)
}
notificationManager.notify(1234, builder.build())
}
}
}

That’s it. Happy Coding.

--

--

Harshita Bambure
Harshita Bambure

Written by Harshita Bambure

Android Developer || WomenTech Global Ambassador at WomenTech Network. || Yoga Teacher || Member @WTM .