BCA / B.Tech 10 min read

Displaying Notifications

Displaying Notifications

What is a Notification?

A Notification is a message that Android displays outside of your application's UI to provide the user with reminders, updates, or other timely information.

When the user taps the notification, it usually opens a specific activity within your app. Notifications appear as an icon in the user's device status bar.

Why are Notifications Important?

  • User Engagement: They are a great way to bring users back to your app.
  • Timely Information: Users get important information immediately, such as a new message or a completed download.
  • Background Actions: The app can alert the user even when it is in the background.

Main Parts of a Notification

  • Small Icon: Appears in the status bar. This is mandatory.
  • App Name: Shown automatically by the system.
  • Timestamp: Shown automatically by the system.
  • Title: The main heading of the notification.
  • Text: The detailed message of the notification.

Notification Channels (for Android 8.0 and higher)

Starting with Android 8.0 (API level 26), all notifications must be assigned to a channel. Notification Channels give users fine-grained control over managing different types of notifications from your app.

  • Example: A social media app might have one channel for "New Messages" and another for "Friend Requests." A user could choose to disable the "Friend Requests" channel while keeping "New Messages" enabled.
  • You must register the channel before you can post any notifications to it. This code should only be executed once when the app starts.

How to Create a Notification

The process of creating a notification involves a few steps:

Step 1: Create a Notification Channel

This code can be placed in your `MainActivity`'s `onCreate()` or in an `Application` class.


private void createNotificationChannel() {
    // Create the channel only on API 26+
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = "MyChannelName";
        String description = "Channel for My App Notifications";
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel("MY_CHANNEL_ID", name, importance);
        channel.setDescription(description);

        // Register the channel with the system
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

Step 2: Build and Show the Notification

This code is written where you want to trigger the notification (e.g., on a button click).


// 1. Build the notification using NotificationCompat.Builder
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MY_CHANNEL_ID")
        .setSmallIcon(R.drawable.ic_notification_icon) // Set the small icon
        .setContentTitle("My Notification Title")      // Set the title
        .setContentText("This is the notification message.") // Set the text
        .setPriority(NotificationCompat.PRIORITY_DEFAULT); // Set the priority

// 2. Get an instance of NotificationManagerCompat
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);

// 3. Show the notification
// notificationId must be unique for each notification
// If you use the same id, the old notification will be updated
notificationManager.notify(1, builder.build());

Note: To open an activity when the notification is clicked, you need to create a `PendingIntent` and add it to the notification builder using the `.setContentIntent()` method.