Lab Tasks: Implementing Notifications in
Android
Lab Title: Simple Notifications in Android
Objective:
Learn to create and display basic notifications in an Android app.
Implement actionable notifications with buttons for interaction.
Task 1: Creating a Simple Notification
Task Description:
Create an Android app that displays a simple notification when a button is clicked. The
notification should include a title, content, and a default icon.
Steps to Complete:
1. Setup the UI:
o Add a button labeled "Show Notification" in activity_main.xml.
Code Snippet:
<Button
android:id="@+id/btnSimpleNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Notification" />
2. Create a Notification Channel:
o Add the following function in MainActivity.kt to create a notification channel
for Android Oreo or higher.
Code Snippet:
private fun createNotificationChannel() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val channel = NotificationChannel(
"simple_channel",
"Simple Notifications",
NotificationManager.IMPORTANCE_DEFAULT
).apply {
description = "Channel for simple notifications"
}
val notificationManager =
getSystemService(NotificationManager::class.java)
notificationManager.createNotificationChannel(channel)
}
}
3. Display the Notification:
o Add a function to trigger a notification when the button is clicked.
Code Snippet:
private fun showSimpleNotification() {
val builder = NotificationCompat.Builder(this, "simple_channel")
.setSmallIcon(android.R.drawable.ic_dialog_info)
.setContentTitle("Simple Notification")
.setContentText("This is a simple notification.")
.setPriority(NotificationCompat.PRIORITY_DEFAULT)
NotificationManagerCompat.from(this).notify(1, builder.build())
}
4. Set Up the Button Click:
o Attach an OnClickListener to trigger the notification.
Code Snippet:
btnSimpleNotification.setOnClickListener {
showSimpleNotification()
}
Task 2: Adding an Action Button to the Notification
Task Description:
Create a notification with an action button that redirects the user to the app when clicked.
Steps to Complete:
1. Modify the UI:
o Add another button labeled "Notification with Action" in activity_main.xml.
Code Snippet:
<Button
android:id="@+id/btnActionNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notification with Action"
android:layout_marginTop="16dp" />
2. Set Up a PendingIntent:
o Define an intent to navigate the user back to the app when the action button is
clicked.
Code Snippet:
val intent = Intent(this, MainActivity::class.java)
val pendingIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT)
3. Create the Notification with Action:
o Add a notification builder with an action button.
Code Snippet:
private fun showNotificationWithAction() {
val builder = NotificationCompat.Builder(this, "simple_channel")
.setSmallIcon(android.R.drawable.ic_dialog_alert)
.setContentTitle("Notification with Action")
.setContentText("Tap to return to the app.")
.setPriority(NotificationCompat.PRIORITY_HIGH)
.addAction(android.R.drawable.ic_menu_view, "Open App",
pendingIntent)
NotificationManagerCompat.from(this).notify(2, builder.build())
}
4. Set Up the Second Button Click:
o Attach an OnClickListener to trigger the notification with action.
Code Snippet:
btnActionNotification.setOnClickListener {
showNotificationWithAction()
}