CodeLab:Android fundamentals 08.1:Notifications

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:

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

  1. In Android Studio, create a new project called “Notify Me!” Accept the default options, and use the Empty Activity template.
  2. In your activity_main.xml layout file, replace the default TextView 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:

  1. Create a member variable for the Notify Me! button:
private Button button_notify;
  1. Create a method stub for the sendNotification() method:
public void sendNotification() {}
  1. In the onCreate() method, initialize the Notify Me! button and create an onClickListener for it. Call sendNotification() from the onClick 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:

  1. 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";
  1. The Android system uses the NotificationManager class to deliver notifications to the user. In MainActivity.java, create a member variable to store the NotificationManager object.
private NotificationManager mNotifyManager;
  1. In MainActivity.java, create a createNotificationChannel() method and instantiate the NotificationManager inside the method.
public void createNotificationChannel() 
{
     mNotifyManager = (NotificationManager)
            getSystemService(NOTIFICATION_SERVICE);
}
  1. Create a notification channel in the createNotificationChannel() method. Because notification channels are only avail
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值