Android fundamentals 08.1:Notifications
Tutorial source : Google CodeLab
Date : 2021/04/06
Complete course : 教程目录 (java).
Note : The link in this article requires Google access
1、Welcome
This practical codelab is part of Unit 3: Working in the background in the Android Developer Fundamentals (Version 2) course. You will get the most value out of this course if you work through the codelabs in sequence:
- For the complete list of codelabs in the course, see Codelabs for Android Developer Fundamentals (V2).
- For details about the course, including links to all the concept chapters, apps, and slides, see Android Developer Fundamentals (Version 2).
Note: This course uses the terms “codelab” and “practical” interchangeably.
Introduction
Sometimes you want your app to show information to users even when the app isn’t running in the foreground. For example, you might want to let users know that new content is available, or that their favorite sports team just scored a goal in a game. The Android notification framework provides a way for your app to notify users even when the app is not in the foreground.
A notification is a message that your app displays to the user outside of your app’s normal UI. Notifications appear as icons in the device’s notification area, which is in the status bar. To see the details of a notification, the user opens the notification drawer, for example by swiping down on the status bar. The notification area and the notification drawer are system-controlled areas that the user can view at any time.
On devices running Android 8.0 and higher, when your app has a new notification to show to the user, your app icon automatically shows a badge. (Badges are also called notification dots). When the user long-presses the app icon, the notification appears above the app icon, as shown in the screenshot below.
In this practical you create an app that triggers a notification when the user taps a button in your app. The user can update the notification or cancel it.
What you should already know
You should be able to:
- Implement the
onClick()
method for buttons. - Create implicit intents.
- Send custom broadcasts.
- Use broadcast receivers.
What you’ll learn
- How to create a notification using the notification builder.
- How to use pending intents to respond to notification actions.
- How to update or cancel existing notifications.
What you’ll do
- Create an app that sends a notification when the user taps a button in the app.
- Update the notification from a button in your app, and from an action button that’s inside the notification.
2、App overview
Notify Me! is an app that lets the user trigger, update, and cancel a notification using the three buttons shown in the screenshots below. While you create the app, you’ll experiment with notification styles, actions, and priorities.
3、Task 1: Create a basic notification
1.1 Create the project
- In Android Studio, create a new project called “Notify Me!” Accept the default options, and use the Empty Activity template.
- In your
activity_main.xml
layout file, replace the defaultTextView
with a button that has the following attributes:
<Button
android:id="@+id/notify"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Notify Me!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Do the following steps in the MainActivity.java
file:
- Create a member variable for the Notify Me! button:
private Button button_notify;
- Create a method stub for the
sendNotification()
method:
public void sendNotification() {}
- In the
onCreate()
method, initialize the Notify Me! button and create anonClickListener
for it. CallsendNotification()
from theonClick
method:
button_notify = findViewById(R.id.notify);
button_notify.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendNotification();
}
});
1.2 Create a notification channel
In the Settings app on an Android-powered device, users can adjust the notifications they receive. Starting with Android 8.0 (API level 26), your code can assign each of your app’s notifications to a user-customizable notification channel:
- Each notification channel represents a type of notification.
- In your code, you can group several notifications in each notification channel.
- For each notification channel, your app sets behavior for the channel, and the behavior is applied to all the notifications in the channel. For example, your app might set the notifications in a channel to play a sound, blink a light, or vibrate.
- Whatever behavior your app sets for a notification channel, the user can change that behavior, and the user can turn off your app’s notifications altogether.
On Android-powered devices running Android 8.0 (API level 26) or higher, notification channels that you create in your app appear as Categories under App notifications in the device Settings app.
For example, in the screenshot below of a device running Android 8.0, the Notify Me! app has one notification channel, Mascot Notification.
When your app targets Android 8.0 (API level 26), to display notifications to your users you must implement at least one notification channel. To display notifications on lower-end devices, you’re not required to implement notification channels. However, it’s good practice to always do the following:
- Target the latest available SDK.
- Check the device’s SDK version in your code. If the SDK version is 26 or higher, build notification channels.
If your targetSdkVersion
is set to 25 or lower, when your app runs on Android 8.0 (API level 26) or higher, it behaves the same as it would on devices running Android 7.1 (API level 25) or lower.
Create a notification channel:
- In
MainActivity
, create a constant for the notification channel ID. Every notification channel must be associated with an ID that is unique within your package. You use this channel ID later, to post your notifications.
private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel";
- The Android system uses the
NotificationManager
class to deliver notifications to the user. InMainActivity.java
, create a member variable to store theNotificationManager
object.
private NotificationManager mNotifyManager;
- In
MainActivity.java
, create acreateNotificationChannel()
method and instantiate theNotificationManager
inside the method.
public void createNotificationChannel()
{
mNotifyManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
}
- Create a notification channel in the
createNotificationChannel()
method. Because notification channels are only avail