Create a Simple Notification in Android

Here we are going to look at How to create a simple notification in Android. A notification is a message that Android displays outside your app’s UI. In this example there is Button and if we click in the Button the Notification will appear on the notification bar.

The App looks like


Before we get started you need some understand about Notification anatomy. The design of a notification is determined by system templates your app simply defines the contents for each portion of the template. The android official document provided here

Let’s get the start, 1. Create an android project in Android studio. I named it as Notification.
2. Add an image to ‘res’ folder for the notification icon. (I selected one from vector assets)
3. In ‘activity_main.xml’ create one-‘button’ give an id to that and attach to ‘’ using ‘findviewbyid’.


<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android=""

        android:text="Notification Button"
        app:layout_constraintTop_toTopOf="parent" />


In 4.create setonClickListener for the button.
inside click listener, we will write code.
Before delivering the notification on Android 8.0(Oreo) and higher, you must create a notification channel. otherwise, it will not work.
Why we create Notification Channel: After android 8, the user can override notification settings over the application and the developer can set up separate notification channels for each conversation group. To see ‘notification channels’ go-to ‘manage notification’ under any of the android application above oreo.

public class MainActivity extends AppCompatActivity {

    private Button notification;

    //To identify channel
    String CHANNEL_ID = "simple_notification";
    //To differentiate channel by id
    int NOTIFICATION_ID = 001;

    protected void onCreate(Bundle savedInstanceState) {


        //onClickListener for button
        notification.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //android oreo and hire versions you need to create a notification channel
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
                        .setContentText("this is a simple notification")

                //calling notification
                NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(MainActivity.this);

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            //this is the name that appears in android system
            CharSequence name = "channel name";
            String description = "description";
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);

What is channel ID: As of Android O, creating a Notification instance now requires a channel ID to be set by the use of the setChannel() method. This is to ensure that our notification belongs to a channel that can be managed by the user from their device settings

What is notification ID: notificationID is you can send any integer variable to make it unique. When you have to cancel the notification or differentiate other notifications or any updation needs to make then it is needed.

Keep exploring more tutorial from this site. In case of any queries make a comment. Happy coding!!!!

Add a Comment

Your email address will not be published. Required fields are marked *