[JAVA] Try sending a notification.

Overview

From this time, I would like to write something that can be used for developing Android applications as a memorandum. This time, I would like to send a notification.

Send a notification

 Intent i = new Intent(Service.this, MainActivity.class);
            PendingIntent pendingIntent
                    = PendingIntent.getActivity(
                    this,
                    0,
                    i,
                    0);
                Notification notification = new Notification.Builder(this)
                        .setContentTitle("notification")
                        .setContentText("This is a test notification! !!")
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.drawable.img)
                        .setAutoCancel(true)
                        .build();
                NotificationManager nm = (NotificationManager)
                        getSystemService(Context.NOTIFICATION_SERVICE);

nm.notify(1000, notification);

I will explain briefly.

 Intent i = new Intent(Service.this, MainActivity.class);

First, the first line is the definition of Intent. Write where to fly to the right and where to fly from to the left.

PendingIntent pendingIntent
                    = PendingIntent.getActivity(
                    this,
                    0,
                    i,
                    0);

Next is the setting of the Intent argument. this is Context 0 is called Request Code, which is simply the number attached to the notification. It is a lot of trouble to wear it with different contents, so it is recommended to attach different ones. i contains the Intent defined earlier. This time 0 is Flag. If I have a chance, I will explain in detail.

Notification notification = new Notification.Builder(this)
                        .setContentTitle("notification")
                        .setContentText("This is a test notification! !!")
                        .setContentIntent(pendingIntent)
                        .setSmallIcon(R.drawable.img)
                        .setAutoCancel(true)
                        .build();

This is a notification setting. setContentTitle is the title of the notification setContentText is the content of the notification. setContentIntent is the Intent you set earlier. setSmallIcon is a notification icon. You can't specify something too complicated, so let's specify something simple. setAutoCancel is to automatically cancel the notification when tapped.

    NotificationManager nm = (NotificationManager)
                        getSystemService(Context.NOTIFICATION_SERVICE);

nm.notify(1000, notification);

This is where you'll pack everything up and send a notification. 1000 is the notification number. If you set each of these, you should receive a notification. Please try to reference.

Reference site

Recommended Posts

Try sending a notification.
Try implementing a WebFlux session
Try to make a simple callback
[Swift] How to send a notification
Try to create a server-client app
Try to make a peepable iterator
Try running a Kubernetes Job from Java
Try making a calculator app in Java