安卓为何引入通知渠道(NotificationChannels)

Android的通知渠道(Notification Channels)是在Android 8.0(API 级别 26)中引入的一个功能。通知渠道的引入主要是为了给用户提供更好的通知管理能力,让他们能够更细致地控制和管理不同类型的通知。以下是通知渠道引入的一些主要目的:

  1. 分类管理:开发者可以根据通知的性质,将其分配到不同的通知渠道。比如,一个应用可能有消息通知、促销通知、系统更新通知等,每种都可以配置为一个独立的通知渠道。
  2. 用户控制权:用户可以针对每个通知渠道进行个性化设置,包括是否允许震动、发出声音、在锁屏上显示等。用户甚至可以完全关闭某个通知渠道,从而不再接收那个渠道的任何通知。
  3. 重要性级别:通知渠道可以设定不同的重要性级别,这决定了通知的展示方式。例如,高重要性的通知可能会以弹出窗口的形式出现,而低重要性的通知可能只在状态栏中静默展示。
  4. 避免打扰:这有助于减少用户感到被过多无关紧要的通知打扰的情况。用户可以只关注对他们来说重要的通知,而忽略或抑制其他不那么重要的通知。
  5. 用户体验改善:通过允许用户控制通知的展现方式,Android系统提供了更好的用户体验,尤其是在通知数量不断增长的今天。
  6. 系统一致性:通知渠道为所有应用程序提供了一个统一的界面和体验,这有助于用户了解如何管理不同应用的通知。

通知渠道的引入使开发者需要在设计应用时更加注意通知的管理和用户偏好的适应,同时也让用户拥有了更大的主动权,可以根据自己的需求来定制通知的行为。这些都是为了改善整个Android生态系统中通知的管理和体验。

Android的通知渠道(Notification Channels)是一个强大的系统功能,它允许应用创建用户可自定义的通知类别。以下是对通知渠道的详细解释:

创建通知渠道

开发者在应用中创建一个或多个通知渠道,每个渠道代表一组具有相似性质的通知。例如,一个社交媒体应用可能有以下渠道:

  • 直接消息通知
  • 评论和回复通知
  • 推荐更新通知

每个渠道都需要赋予一个唯一的ID、用户可见的名称和描述。这些信息有助于用户理解每个渠道的用途。

用户界面与设置

在Android 8.0及更高版本中,系统设置包括了一个专门的界面,用于管理应用的通知渠道。用户可以访问这个界面来开启或关闭特定的通知渠道,也可以调整通知的行为,比如声音、震动、显示在锁屏上等。

通知的重要性级别

每个通知渠道都可以设置一个重要性级别,这会影响通知的呈现方式。重要性级别通常包括:

  • 高重要性:发出声音并显示为弹出通知。
  • 默认重要性:发出声音,但不会以弹出方式显示。
  • 中等重要性:没有声音,仅在状态栏中显示。
  • 低重要性:没有声音也不会弹出,用户必须下拉状态栏才能看到。

如下是Android API 33, 定义在 NotificationManager.java 的通知的重要性级别。

NotificationManager 复制代码
/**
 * Value signifying that the user has not expressed an importance.
 *
 * This value is for persisting preferences, and should never be associated with
 * an actual notification.
 */
public static final int IMPORTANCE_UNSPECIFIED = -1000;

/**
 * A notification with no importance: does not show in the shade.
 */
public static final int IMPORTANCE_NONE = 0;

/**
 * Min notification importance: only shows in the shade, below the fold.  This should
 * not be used with {@link Service#startForeground(int, Notification) Service.startForeground}
 * since a foreground service is supposed to be something the user cares about so it does
 * not make semantic sense to mark its notification as minimum importance.  If you do this
 * as of Android version {@link android.os.Build.VERSION_CODES#O}, the system will show
 * a higher-priority notification about your app running in the background.
 */
public static final int IMPORTANCE_MIN = 1;

/**
 * Low notification importance: Shows in the shade, and potentially in the status bar
 * (see {@link #shouldHideSilentStatusBarIcons()}), but is not audibly intrusive.
 */
public static final int IMPORTANCE_LOW = 2;

/**
 * Default notification importance: shows everywhere, makes noise, but does not visually
 * intrude.
 */
public static final int IMPORTANCE_DEFAULT = 3;

/**
 * Higher notification importance: shows everywhere, makes noise and peeks. May use full screen
 * intents.
 */
public static final int IMPORTANCE_HIGH = 4;

/**
 * Unused.
 */
public static final int IMPORTANCE_MAX = 5;

开发者可以根据通知内容的重要性来选择合适的级别。

通知的展示和行为

用户可以为每个通知渠道设置不同的行为,例如:

  • 是否在锁屏上显示通知
  • 是否有声音,以及使用哪种声音
  • 是否震动,以及震动模式
  • 是否显示通知灯
  • 通知出现的时间(免打扰模式时的规则)

这些自定义设置让用户能够根据个人偏好来管理通知。

兼容性

对于运行Android 8.0或以上系统的设备,使用通知渠道是强制性的。但是,对于旧版本的Android系统,通知仍然可以按照之前的方式发送,不需要通知渠道。因此,开发者需要在应用中加入条件判断,以确保只在支持通知渠道的系统中使用它们。

为何引入通知渠道

引入通知渠道的主要目的是为了提高用户对通知的控制能力,增强用户体验。在以前的Android版本中,用户对通知的控制非常有限,他们只能选择开启或关闭某个应用的所有通知,而无法根据通知的类型进行区分。随着应用功能的增多,通知的种类也随之增加,用户需要更精细的控制方式来管理这些通知。通知渠道的引入恰好解决了这个问题,使得用户能够根据自己的需求来定制接收哪些通知以及如何接收。

相关推荐
落落落sss2 小时前
MybatisPlus
android·java·开发语言·spring·tomcat·rabbitmq·mybatis
代码敲上天.3 小时前
数据库语句优化
android·数据库·adb
GEEKVIP5 小时前
手机使用技巧:8 个 Android 锁屏移除工具 [解锁 Android]
android·macos·ios·智能手机·电脑·手机·iphone
model20057 小时前
android + tflite 分类APP开发-2
android·分类·tflite
彭于晏6897 小时前
Android广播
android·java·开发语言
与衫8 小时前
掌握嵌套子查询:复杂 SQL 中 * 列的准确表列关系
android·javascript·sql
500了14 小时前
Kotlin基本知识
android·开发语言·kotlin
人工智能的苟富贵15 小时前
Android Debug Bridge(ADB)完全指南
android·adb
小雨cc5566ru20 小时前
uniapp+Android面向网络学习的时间管理工具软件 微信小程序
android·微信小程序·uni-app
bianshaopeng21 小时前
android 原生加载pdf
android·pdf