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.
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 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. MainActivity.java
and ʻactivity_main.xml` are as follows.
activtiy_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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
notificationManager.createNotificationChannel(channel);
}
Button button = findViewById(R.id.button);
button.setOnClickListener(v -> notifyTest()); //・ ・ ・(3)
}
public void notifyTest() {
NotificationCompat.Builder builder
= new NotificationCompat.Builder(this, "CHANNEL_ID") //・ ・ ・(4)
.setSmallIcon(android.R.drawable.ic_menu_info_details)
.setContentTitle("title")
.setContentText("Message message")
.setPriority(NotificationCompat.PRIORITY_DEFAULT);
NotificationManagerCompat notificationManager
= NotificationManagerCompat.from(this);
notificationManager.notify(R.string.app_name, builder.build());
//・ ・ ・(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.
(3) Implement the processing when the button is pressed.
(4) Create a notification. You can set the icon, title, message, etc.
(5) Display the notification.
Display notifications with notificationManager.notify
. Pass a unique ID and the result of NotificationCompat.Builder.build ()
as an argument.
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