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

GitHub:https://github.com/rincemathew/Notification

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 ‘MainActivity.java’ using ‘findviewbyid’.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/notification"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Notification Button"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

In MainActivity.java 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.

MainActivity.java

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;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        notification=findViewById(R.id.notification);

        //onClickListener for button
        notification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //android oreo and hire versions you need to create a notification channel
                createNotificationChannel();
                NotificationCompat.Builder builder = new NotificationCompat.Builder(MainActivity.this, CHANNEL_ID)
                        .setSmallIcon(R.drawable.ic_add_alarm_black_24dp)
                        .setContentTitle("Notification")
                        .setContentText("this is a simple notification")
                        .setPriority(NotificationCompat.PRIORITY_DEFAULT);

                //calling notification
                NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(MainActivity.this);
                notificationManagerCompat.notify(NOTIFICATION_ID, builder.build());
            }
        });

    }
    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);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            NotificationManager notificationManager = getSystemService(NotificationManager.class);
            notificationManager.createNotificationChannel(channel);
        }
    }
}

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 *