Notification Channels (Android 8.0+) in Android Development


With the release of Android 8.0 (Oreo, API level 26), Android introduced the concept of Notification Channels. This feature allows developers to categorize notifications into different channels that users can manage individually. This gives users more control over the types of notifications they receive, such as controlling sound, vibration, and visibility for each category.

Why Notification Channels?

Before Android 8.0, all notifications were handled in the same way and could not be individually customized by the user. With notification channels, users can fine-tune the settings for different types of notifications. For example, users can choose to receive sound notifications for chat messages but silence promotional notifications.

Each notification channel represents a category of notifications, and each category can have specific settings. The main goal is to improve the user experience by giving users better control over notifications.

Creating a Notification Channel

In order to create a notification channel, you must first check if the device is running Android 8.0 or higher. If so, you can create and register notification channels using the NotificationManager system service.

Here is a basic example of how to create a notification channel:

    import android.app.NotificationChannel
    import android.app.NotificationManager
    import android.content.Context
    import android.os.Build
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity

    class MainActivity : AppCompatActivity() {

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

            // Check if the device is running Android Oreo or higher
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                // Create a notification channel
                val channelId = "default_channel"
                val channelName = "Default Channel"
                val channelDescription = "This is the default notification channel."

                val importance = NotificationManager.IMPORTANCE_DEFAULT
                val channel = NotificationChannel(channelId, channelName, importance).apply {
                    description = channelDescription
                }

                // Register the channel with the system
                val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
                notificationManager.createNotificationChannel(channel)
            }
        }
    }
        

In the example above, we first check if the Android version is Oreo or higher using Build.VERSION.SDK_INT. If the version is Android 8.0 or higher, we proceed to create a new notification channel using NotificationChannel. We specify the channel ID, name, description, and importance level (e.g., IMPORTANCE_DEFAULT).

Setting Notification Importance

The importance level defines how interruptive the notification will be. Here are the available importance levels:

  • IMPORTANCE_NONE: Notifications are not shown to the user at all.
  • IMPORTANCE_MIN: Notifications will appear in the background, but without sound or vibration.
  • IMPORTANCE_LOW: Notifications will appear in the notification drawer with a sound.
  • IMPORTANCE_DEFAULT: Notifications will appear with sound and vibration (default level).
  • IMPORTANCE_HIGH: Notifications will appear as heads-up notifications (shown on top of the screen).

The level you choose will affect how the notification is displayed and interacted with by the user.

Sending Notifications to a Channel

Once you’ve created and registered a notification channel, you can send notifications to that channel by specifying the channel ID when building the notification.

    import android.app.Notification
    import android.app.NotificationManager
    import android.content.Context
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    import androidx.core.app.NotificationCompat

    class MainActivity : AppCompatActivity() {

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

            // Build the notification
            val notification = NotificationCompat.Builder(this, "default_channel")
                .setSmallIcon(android.R.drawable.ic_dialog_info)
                .setContentTitle("New Notification")
                .setContentText("This notification is sent to a channel.")
                .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                .build()

            // Get NotificationManager
            val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

            // Show the notification
            notificationManager.notify(1, notification)
        }
    }
        

In this example, we create a notification and specify the default_channel ID (the one we created earlier). The notification will be displayed using the settings defined in that channel. We then use the notify() method of NotificationManager to display the notification.

Managing Notification Channels

Once a notification channel is created, you cannot modify its properties, such as the importance level or sound, without the user manually changing them in the system settings. However, you can delete a notification channel if it is no longer needed.

To delete a notification channel:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.deleteNotificationChannel("default_channel")
    }
        

In the code above, we use deleteNotificationChannel() to remove the notification channel with the ID default_channel.

Notification Channel Settings

Users can control the behavior of notification channels through the system settings. To access the notification channel settings, users can go to:

  • Settings > Apps & notifications > [Your app] > Notifications
  • Settings > Sound & notifications > [Your app]

Here, users can toggle notifications, change sounds, enable/disable vibrations, and set importance levels for each notification channel. This allows users to customize their notification experience based on the channel type.

Conclusion

Notification channels are a powerful feature introduced in Android 8.0 (Oreo) that help improve user experience by giving users better control over the notifications they receive. By categorizing notifications into different channels, developers can offer users a way to customize the notification experience, such as sound, vibration, and visibility. Understanding how to create and manage notification channels is essential for modern Android app development, especially to support Android 8.0 and above.





Advertisement