Android的通知渠道(Notification Channels)是在Android 8.0(API 级别 26)中引入的一个功能。通知渠道的引入主要是为了给用户提供更好的通知管理能力,让他们能够更细致地控制和管理不同类型的通知。以下是通知渠道引入的一些主要目的:
- 分类管理:开发者可以根据通知的性质,将其分配到不同的通知渠道。比如,一个应用可能有消息通知、促销通知、系统更新通知等,每种都可以配置为一个独立的通知渠道。
- 用户控制权:用户可以针对每个通知渠道进行个性化设置,包括是否允许震动、发出声音、在锁屏上显示等。用户甚至可以完全关闭某个通知渠道,从而不再接收那个渠道的任何通知。
- 重要性级别:通知渠道可以设定不同的重要性级别,这决定了通知的展示方式。例如,高重要性的通知可能会以弹出窗口的形式出现,而低重要性的通知可能只在状态栏中静默展示。
- 避免打扰:这有助于减少用户感到被过多无关紧要的通知打扰的情况。用户可以只关注对他们来说重要的通知,而忽略或抑制其他不那么重要的通知。
- 用户体验改善:通过允许用户控制通知的展现方式,Android系统提供了更好的用户体验,尤其是在通知数量不断增长的今天。
- 系统一致性:通知渠道为所有应用程序提供了一个统一的界面和体验,这有助于用户了解如何管理不同应用的通知。
通知渠道的引入使开发者需要在设计应用时更加注意通知的管理和用户偏好的适应,同时也让用户拥有了更大的主动权,可以根据自己的需求来定制通知的行为。这些都是为了改善整个Android生态系统中通知的管理和体验。
Android的通知渠道(Notification Channels)是一个强大的系统功能,它允许应用创建用户可自定义的通知类别。以下是对通知渠道的详细解释:
创建通知渠道
开发者在应用中创建一个或多个通知渠道,每个渠道代表一组具有相似性质的通知。例如,一个社交媒体应用可能有以下渠道:
- 直接消息通知
- 评论和回复通知
- 推荐更新通知
每个渠道都需要赋予一个唯一的ID、用户可见的名称和描述。这些信息有助于用户理解每个渠道的用途。
用户界面与设置
在Android 8.0及更高版本中,系统设置包括了一个专门的界面,用于管理应用的通知渠道。用户可以访问这个界面来开启或关闭特定的通知渠道,也可以调整通知的行为,比如声音、震动、显示在锁屏上等。
通知的重要性级别
每个通知渠道都可以设置一个重要性级别,这会影响通知的呈现方式。重要性级别通常包括:
- 高重要性:发出声音并显示为弹出通知。
- 默认重要性:发出声音,但不会以弹出方式显示。
- 中等重要性:没有声音,仅在状态栏中显示。
- 低重要性:没有声音也不会弹出,用户必须下拉状态栏才能看到。
如下是Android API 33, 定义在 NotificationManager.java 的通知的重要性级别。
/**
* 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版本中,用户对通知的控制非常有限,他们只能选择开启或关闭某个应用的所有通知,而无法根据通知的类型进行区分。随着应用功能的增多,通知的种类也随之增加,用户需要更精细的控制方式来管理这些通知。通知渠道的引入恰好解决了这个问题,使得用户能够根据自己的需求来定制接收哪些通知以及如何接收。
下面展示一个创建通知的demo
package com.android.notificationdemo;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.app.NotificationCompat;
import androidx.core.app.NotificationManagerCompat;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import com.android.notificationdemo.databinding.ActivityMainBinding;
import java.util.concurrent.atomic.AtomicInteger;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
createNotificationChannel();
AtomicInteger notificationId = new AtomicInteger(0);
binding.btnCreateNotification.setOnClickListener(v -> {
Log.d(TAG, "--> click ");
sendNotification(notificationId.incrementAndGet());
});
}
private void createNotificationChannel() {
Log.d(TAG, "--> createNotificationChannel: ");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
CharSequence name = getString(R.string.channel_name); // 渠道名
String description = getString(R.string.channel_description); // 渠道描述
int importance = NotificationManager.IMPORTANCE_DEFAULT;
NotificationChannel channel = new NotificationChannel("MY_CHANNEL_ID", name, importance);
channel.setDescription(description);
NotificationManager notificationManager = getSystemService(NotificationManager.class);
notificationManager.createNotificationChannel(channel);
Log.d(TAG, "--> notificationManager.createNotificationChannel(channel) ");
}
}
private void sendNotification(int notificationId) {
Log.d(TAG, "--> sendNotification, notificationId: " + notificationId);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "MY_CHANNEL_ID")
.setSmallIcon(R.drawable.ic_notification) // 设置通知图标
.setContentTitle("My Notification Title") // 设置通知标题
.setContentText("This is the notification content. notificationId: "+ notificationId) // 设置通知内容
.setPriority(NotificationCompat.PRIORITY_DEFAULT); // 设置通知的优先级
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
// int notificationId = 1; // 通知的id
if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.POST_NOTIFICATIONS) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
Log.d(TAG, "Have no android.Manifest.permission.POST_NOTIFICATIONS");
return;
}
notificationManager.notify(notificationId, builder.build()); // 发送通知
Log.d(TAG, "--> notificationManager.notify(notificationId, builder.build());");
}
}
如果你看到了这里,觉得文章写得不错就给个赞呗?
更多Android进阶指南 可以扫码 解锁更多Android进阶资料
敲代码不易,关注一下吧。ღ( ´・ᴗ・` )