Open In App

Notification Manager in Android

Last Updated : 14 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Firstly, let’s understand what notifications are on Android. Notifications are short, timely messages that inform the user about events that occur within an app. They are a crucial component of the user experience on an Android device, as they provide users with important information and help them stay informed about what’s happening in their apps.

A notification manager in Android is a system service that manages the display of notifications on an Android device. It allows developers to create and display notifications to users on behalf of their apps. The Notification Manager API provides the ability to create notifications and control their behavior. Notifications can be displayed in the notification shade, on the lock screen, and in the status bar. The notification manager also provides the ability to customize notification sounds, vibration patterns, and LED colors.

Developers can use the NotificationManager class to create and manage notifications within their apps. To create a notification, developers first need to create a NotificationCompat.Builder object, which is a helper class that provides a convenient way to construct notifications with various properties, such as title, text, icon, and actions.

  • Notifications can be generated in Android for a variety of reasons. Here are some common scenarios where you might generate a notification:
  • New message or email: You can generate a notification when the user receives a new message or email in your app.
  • Reminder or alarm: You can generate a notification when the user has a scheduled reminder or alarm in your app.
  • Event or appointment: You can generate a notification when the user has an upcoming event or appointment in your app.
  • Download or upload completion: You can generate a notification when a download or upload is completed in your app.
  • Progress updates: You can generate a notification to show progress updates for a long-running operation in your app.
  • App updates: You can generate a notification to inform the user about updates to your app.
  • News or weather updates: You can generate a notification to provide the user with news or weather updates.
  • Low battery or data usage warning: You can generate a notification to warn the user about low battery or high data usage in your app.

These are just a few examples of when you might generate a notification in your Android app. The decision to generate a notification will depend on the specific needs of your app and the user experience you want to provide. To use the notification manager in an Android app, developers need to create a NotificationCompat.Builder object and set its properties, such as title, text, icon, and actions. They can then use the NotificationManager system service to issue the notification. Let’s look at the code.

Implementation

Kotlin




package com.example.gfg
  
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.core.app.NotificationCompat
  
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
           
          // Create a notification builder object
        val builder = NotificationCompat.Builder(this, "channel_id")
            .setSmallIcon(android.R.drawable.stat_notify_chat)
            .setContentTitle("New message")
            .setContentText("You have a new message.")
            .setPriority(NotificationCompat.PRIORITY_DEFAULT)
  
        // Create a notification manager object
        val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  
        // Check if the device is running Android Oreo or higher
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            // Create a notification channel
            val channel = NotificationChannel("channel_id", "Channel Name", NotificationManager.IMPORTANCE_DEFAULT)
            channel.description = "Channel Description"
            // Register the channel with the system
            notificationManager.createNotificationChannel(channel)
        }
        // To see the message in logcat
        Log.i("Notify","$builder")
        // Issue the notification
        notificationManager.notify(1, builder.build())
    }
}


Output

 

Code Explanation

In this example, we’re creating a notification with a small icon, a title, and a message text using NotificationCompat.Builder class. We’re also setting the priority level to PRIORITY_DEFAULT and specifying a notification channel ID “channel_id”. We’re using the getSystemService() method to obtain a reference to the NotificationManager system service, and we’re casting it to a NotificationManager object.

The code also checks if the device is running Android Oreo or higher, and if so, it creates a notification channel and registers it with the system using the NotificationManager class. Finally, we’re using the notify() method of the NotificationManager class to issue the notification to the system, and we’re passing a unique notification ID and the notification builder object to the method.

Note that you’ll need to import the appropriate classes and permissions in your Kotlin file in order to use this code. For example, you’ll need to have android.permission.VIBRATE permission in your AndroidManifest.xml file if you want to vibrate the device when a notification is issued.

Conclusion

In conclusion, notifications are an important feature of Android apps that allow developers to inform users about important events and updates. Android provides a NotificationManager system service that allows developers to create and issue notifications to the system. Notifications can be generated for a variety of reasons, including new messages or emails, reminders or alarms, events or appointments, download or upload completion, progress updates, app updates, news or weather updates, and warnings about low battery or data usage. By using notifications effectively, developers can enhance the user experience of their Android apps and provide users with timely and relevant information.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads