Notification Program
Notification Program
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:
1. Status Bar Notification (appears in the same layout as the current time, battery percentage)
2. Notification drawer Notification (appears in the drop-down menu)
3. Heads-Up Notification (appears on the overlay screen, ex: Whatsapp notification, OTP messages)
4. Lock-Screen Notification
Procedure:
<Button
android:id="@+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Send Notification" />
</RelativeLayout>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Welcome To GeeksforGeeks"
android:textSize="15sp"
android:textStyle="bold" />
</RelativeLayout>
// declaring variables
lateinit var notificationManager: NotificationManager
lateinit var notificationChannel: NotificationChannel
lateinit var builder: Notification.Builder
private val channelId = "i.apps.notifications"
private val description = "Test notification"
// accessing button
val btn = findViewById<Button>(R.id.btn)
builder = Notification.Builder(this)
.setContent(contentView)
.setSmallIcon(R.drawable.ic_launcher_background)
.setLargeIcon(BitmapFactory.decodeResource(this.resources,
R.drawable.ic_launcher_background))
.setContentIntent(pendingIntent)
}
notificationManager.notify(1234, builder.build())
}
}
}
With this, we have now successfully created a “Notification” for our application.
Note: 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.
Output: