It's late! Try implementing Android Notification in Java (Beginner)


Here is the basic usage of Android Notification. The content is simple for beginners. I would like to explain based on the sample application.

The environment of the sample application is as follows.

What is Notification?

According to the Official Page, there is the following description.

Notifications are messages that Android displays outside of the app's UI, providing users with reminders, messages from others, timely information from the app, and more. Users can tap the notification to open the app or interact directly from the notification.

Well, it's difficult ... In short, it is the one displayed on the status bar of the smartphone.

This sample app

This time, I would like to make an application that "presses a button and displays a notification". This time it is the application so far. If you tap the content after that, "Open app" etc. will not be implemented this time. Currently, Kotlin is the mainstream on Android, but I would like to implement it in Java this time as well.

The sample application this time will behave as follows.



Let's implement it now. The screen has a button in the center of the screen. and ʻactivity_main.xml` are as follows.


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

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {        //・ ・ ・(1)
            int importance = NotificationManager.IMPORTANCE_DEFAULT;

            NotificationChannel channel                              //・ ・ ・(2)
                = new NotificationChannel("CHANNEL_ID", "Sample app", importance);

            channel.setDescription("Description / Explanation You can write a description of the notification here.");     
            NotificationManager notificationManager = getSystemService(NotificationManager.class); 
        Button button = findViewById(;
        button.setOnClickListener(v -> notifyTest());                //・ ・ ・(3) 

    public void notifyTest() {                     
        NotificationCompat.Builder builder 
            = new NotificationCompat.Builder(this, "CHANNEL_ID")     //・ ・ ・(4)
                .setContentText("Message message")                      

        NotificationManagerCompat notificationManager 
                = NotificationManagerCompat.from(this);

                                                                     //・ ・ ・(5)

(1) To deliver notifications on Android 8.0 or above, you need to register the notification channel of the app in the system. Run this code as soon as the app launches.

Creating an existing notification channel does not perform any operations, so it is safe to call it repeatedly.

So, if you call it with ʻonCreate`, there will be no problem.

(2) Implementation of notification channel. The notification channel looks like the image below. This screen can be viewed from the smartphone settings-> apps / notifications.

スクリーンショット 2020-09-05 23.30.30.png

(3) Implement the processing when the button is pressed.

(4) Create a notification. You can set the icon, title, message, etc.

スクリーンショット 2020-09-05 23.36.35.png

(5) Display the notification. Display notifications with notificationManager.notify. Pass a unique ID and the result of () as an argument.

Operation check

When you start the app and press the displayed button ... you should see a notification. If the movement is the same as the one shown in the sample app above, you are successful!


Did the sample work? Maybe it works with copy and paste. When delivering notifications on Android 8.0 and above, additional implementation is required, and I think the rest is relatively easy.

Notifications still have many features. You can also add a progress bar or an implementation when you tap the notification. I would like to write about the implementation around here in the future.

see you!

Recommended Posts

It's late! Try implementing Android Notification in Java (Beginner)
It's late! Try implementing Android Work Manager in Java (Beginner)
Try implementing Android Hilt in Java
Try implementing GraphQL server in Java
Try implementing Yubaba in Kinx
Try calling JavaScript in Java
Try developing Spresense in Java (1)
Solve AtCoder Beginner Contest 151 in java
Solve AtCoder Beginner Contest 150 in java
Try implementing asynchronous processing in Azure
Solve AtCoder Beginner Contest 153 in java
Try running Selenuim 3.141.59 in eclipse (java)
Try an If expression in Java
Solve AtCoder Beginner Contest 175 in java
Solve AtCoder Beginner Contest 160 in java
Try running AWS X-Ray in Java
Try to implement Yubaba in Java
Solve AtCoder Beginner Contest 152 in java
Solve AtCoder Beginner Contest 156 in java
Try to solve Project Euler in Java
Try to implement n-ary addition in Java
Try using the Stream API in Java
Call the Windows Notification API in Java
Try using JSON format API in Java
Try calling the CORBA service in Java 11+
Try making a calculator app in Java
Do TensorFlow in Java. It's easy, though. .. .. ..
Java to C and C to Java in Android Studio
java beginner 4
java beginner 3
java beginner
Try using Firebase Cloud Functions on Android (Java)
[AWS IoT] Implementing Authorizing Direct Calls in Java [Java]
Try scraping about 30 lines in Java (CSV output)
[Android / Java] Operate a local database in Room
Send push notifications using Notification Hubs in Java
Try to create a bulletin board in Java
Second decoction: Try an If expression in Java
Try using Sourcetrail (win version) in Java code
Try using GCP's Cloud Vision API in Java
Try using Sourcetrail (macOS version) in Java code
Basics of threads and Callable in Java [Beginner]
Try communication using gRPC on Android + Java server
Difficulties when implementing Alarm Manager in Android Studio
Try using the COTOHA API parsing in Java
Represents "next day" and "previous day" in Java / Android
[Beginner] Install java development tool in cloud9 development environment.