Android学习笔记 89. 通知

本文介绍了如何在Android应用中创建和管理通知。内容涵盖创建基本通知、建立通知通道、更新和取消通知,以及添加通知动作按钮。通过示例代码展示了如何在用户点击按钮时发送通知,并提供了完整的项目源码链接。此外,还强调了通知在应用后台运行的重要性,以及用户如何在设备设置中自定义通知行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Android学习笔记

Android 开发者基础知识 (Java) —— Google Developers 培训团队

第3单元 在后台运行

第8课 闹钟和调度程序

89. 通知
你会做什么
  • 创建一个应用程序,当用户点击应用程序中的按钮时发送通知。
  • 从应用中的按钮和通知内的操作按钮更新通知。

这里也贴一篇笔者关于通知Notification的文章

https://blog.csdn.net/weixin_44226181/article/details/126206172

89.1 创建基本通知
  1. 创建项目

    在这里插入图片描述

    布局

    <?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/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" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>
    

    MainActivity.java

    package com.dingjiaxiong.notifyme;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        private Button button_notify;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button_notify = findViewById(R.id.notify);
            button_notify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendNotification();
                }
            });
    
        }
    
        public void sendNotification() {
            
        }
    }
    
  2. 创建通知通道

    在 Android 设备上的“设置”应用中,用户可以调整他们收到的通知。从 Android 8.0(API 级别 26)开始,您的代码可以将应用的每个通知分配给用户可自定义的 通知通道:

    • 每个通知通道代表一种通知类型。
    • 在您的代码中,您可以在每个通知通道中对多个通知进行分组。
    • 对于每个通知通道,您的应用程序都会为通道设置行为,并且该行为将应用于通道中的所有通知。例如,您的应用可能会将频道中的通知设置为播放声音、闪烁灯光或振动。
    • 无论您的应用程序为通知通道设置什么行为,用户都可以更改该行为,并且用户可以完全关闭您的应用程序的通知。

    在运行 Android 8.0(API 级别 26)或更高版本的 Android 设备上,您在应用程序中创建的通知渠道会在设备设置应用程序的应用程序通知下显示为类别。

    package com.dingjiaxiong.notifyme;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.app.NotificationChannel;
    import android.app.NotificationManager;
    import android.graphics.Color;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    
    public class MainActivity extends AppCompatActivity {
    
        private static final String PRIMARY_CHANNEL_ID = "primary_notification_channel";
    
        private NotificationManager mNotifyManager;
    
        private Button button_notify;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            button_notify = findViewById(R.id.notify);
            button_notify.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    sendNotification();
                }
            });
    
        }
    
        public void sendNotification() {
    
        }
    
        public void createNotificationChannel()
        {
            mNotifyManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            if (android.os.Build.VERSION.SDK_INT >=
                    android.os.Build.VERSION_CODES.O) {
                // Create a NotificationChannel
                NotificationChannel notificationChannel = new NotificationChannel(PRIMARY_CHANNEL_ID,
                        "Mascot Notification", NotificationManager
                        .IMPORTANCE_HIGH);
                notificationChannel.enableLights(true);
                notificationChannel.setLightColor(Color.RED);
                notificationChannel.enableVibration(true);
                notificationChannel.setDescription("Notification from Mascot");
                mNotifyManager.createNotificationChannel(notificationChannel);
            }
        }
    
    }
    
  3. 构建第一个通知

  4. 添加内容intent并关闭通知

  5. 为通知添加优先级和默认值以实现向后兼容性

89.2 更新或取消通知
  1. 添加更新按钮和取消按钮
  2. 实现取消和更新通知方法
  3. 切换按钮状态
89.3 添加通知动作按钮
  1. 实现一个调用updateNotification( )的广播接收器
  2. 为更新操作创建一个图标
  3. 将更新操作添加到通知中

项目地址:https://github.com/google-developer-training/android-fundamentals-apps-v2/tree/master/NotifyMe

89.4 小结

通知是您可以在应用的正常 UI 之外向用户显示的消息:

  • 通知为您的应用程序提供了一种与用户交互的方式,即使在应用程序未运行时也是如此。
  • 当 Android 发出通知时,通知首先以图标的形式出现在设备的通知区域中。
  • 要指定通知的 UI 和操作,请使用NotificationCompat.Builder.
  • 要创建通知,请使用NotificationCompat.Builder.build()
  • 要发出通知,请使用NotificationManager.notify()将通知对象传递给 Android 运行时系统。
  • 要使更新或取消通知成为可能,请将通知 ID 与通知相关联。
  • 通知可以有几个组件,包括一个小图标(setSmallIcon(), 必需的);标题(setContentTitle());和详细的文字(setContentText())。
  • 通知还可以包括待处理的意图、扩展样式、优先级等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

祝我天天开心,平安健康

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值