Android-Notification

获取通知管理器

1
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

  Android 8.0+,需要为通知管理器添加一个通知渠道,并传入三个参数:渠道ID、渠道名、重要级别

  1. 渠道ID:需要和后面创建Notification所使用的channelId一致,创建后尽量避免修改
  2. 渠道名:显示给用户,传达通知的来源和内容
  3. 重要级别:有以下五种级别
    1. IMPORTANCE_DEFAULT(默认):通知的重要性属于中等级别,不会打断用户,但会显示在通知栏中。
    2. IMPORTANCE_HIGH(高):通知的重要性属于高级别,会以声音和震动的方式提醒用户,并在锁屏上显示。
    3. IMPORTANCE_LOW(低):通知的重要性属于低级别,不会打扰用户,而且不会在锁屏上显示。
    4. IMPORTANCE_MIN(最低):通知的重要性属于最低级别,不会打断用户,不会发出声音,也不会在锁屏上显示。
    5. IMPORTANCE_NONE(无):通知的重要性为零,将完全隐藏通知。
1
2
3
4
5
6
7
8
9
10
String channelId = "notification";  // 通知渠道ID
String name = "channel_name"; // 通知渠道名

// Android 8.0+ 需要创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 创建一个新的通知渠道,并指定渠道ID、名称和重要级别。
NotificationChannel notificationChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_DEFAULT);
// 将通知渠道与通知管理器绑定
notificationManager.createNotificationChannel(notificationChannel);
}

  另外通知渠道有以下公共方法

方法名作用
setDescription(CharSequence description)设置通知渠道的描述
setSound(Uri sound, AudioAttributes audioAttributes)设置通知渠道的声音和音频属性
setVibrationPattern(long[] vibrationPattern)设置通知渠道的振动模式
enableLights(boolean lights)启用或禁用通知渠道的LED灯光
enableVibration(boolean vibration)启用或禁用通知渠道的振动
setLockscreenVisibility(int visibility)设置通知渠道在锁屏时的可见性
setGroup(String groupId)将通知渠道分组到指定的组
getImportance()获取通知渠道的重要性级别

创建意图

1
2
3
4
5
6
7
8
9
// 创建一个启动Activity的延迟意图
PendingIntent pendingIntent;
Intent intent = new Intent(context, TeacherLoginActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12+ 必须指定为PendingIntent.FLAG_IMMUTABLE或PendingIntent.FLAG_MUTABLE之一
pendingIntent = PendingIntent.getActivity(context, 10001, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 10001, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

发送通知

  构建通知时所需要的channelId与之前创建NotificationChannel所使用的channelId一致
  每个通知都需要一个唯一的通知ID,用于标识通知。如果使用相同的通知ID多次调用notify()方法,将会覆盖之前的通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// 构建通知
Notification notification = new NotificationCompat.Builder(context, channelId)
.setAutoCancel(true) // 点击通知后自动清除
.setContentTitle("消息标题") // 通知标题
.setContentText("消息内容") // 通知内容
.setWhen(System.currentTimeMillis()) // 通知时间
.setSmallIcon(R.drawable.ic_launcher) // 设置通知显示在系统状态栏的小图标
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)) // 设置下拉状态栏后通知的图标
.setContentIntent(pendingIntent) // 点击通知后跳转
.build();

// 发送通知,确保每个通知使用唯一的通知ID
int id = 1;
notificationManager.notify(channelId, id, notification);

  Notification类中常用的公共方法

方法名作用
setContentTitle(CharSequence title)设置通知的标题文本
setContentText(CharSequence text)设置通知的内容文本
setSmallIcon(int icon)设置通知的小图标
setLargeIcon(Bitmap icon)设置通知的大图标
setContentIntent(PendingIntent intent)设置点击通知时要执行的操作
setAutoCancel(boolean autoCancel)设置通知是否在用户点击后自动取消
setDefaults(int defaults)设置通知的默认行为,如声音、震动等
setPriority(int priority)设置通知的优先级
setVisibility(int visibility)设置通知在锁屏时的可见性
addAction(int icon, CharSequence title, PendingIntent intent)添加操作按钮到通知中
setStyle(Notification.Style style)设置通知的样式,如大文本样式、大图样式等
setGroup(String groupKey)将通知分组到指定的组
setGroupSummary(boolean isGroupSummary)设置是否为组的摘要通知
setGroupAlertBehavior(int groupAlertBehavior)设置组通知的提醒行为
setOngoing(boolean ongoing)设置通知是否为持久通知
setSound(Uri sound)设置通知的声音
setVibrate(long[] pattern)设置通知的震动模式。例如,long[] pattern = {0, 1000, 500, 1000, 500, 1000}; 表示在通知到达时先静止,然后以1秒振动、0.5秒静止、1秒振动、0.5秒静止、1秒振动的模式进行震动。
setColor(int argb)设置通知的颜色
setCategory(String category)设置通知类别
setFullScreenIntent(PendingIntent intent, boolean b)设置全屏意图。全屏意图在特定条件下可以使通知以全屏方式显示

  setCategory(String category)设置通知类别时填入如下10种类别

  1. Notification.CATEGORY_ALARM:表示通知是一个闹钟或定时器相关的通知。
  2. Notification.CATEGORY_CALL:表示通知与电话通话相关。
  3. Notification.CATEGORY_EMAIL:表示通知与电子邮件相关。
  4. Notification.CATEGORY_ERROR:表示通知是一个错误或异常相关的通知。
  5. Notification.CATEGORY_EVENT:表示通知与日历事件或日程安排相关。
  6. Notification.CATEGORY_MESSA:表示通知是一条消息或聊天相关的通知。
  7. Notification.CATEGORY_PROGRESS:表示通知与长时间运行的操作或进度相关。
  8. Notification.CATEGORY_PROMO:表示通知是营销或促销相关的通知。
  9. Notification.CATEGORY_RECOMMENDATIO:表示通知是一个推荐或建议相关的通知。
  10. Notification.CATEGORY_SERVICE:表示通知与后台服务或系统状态相关。

发送通知例子

  这是一个简单的通知例子,调用该方法并传入Activity后,将向系统发送一个通知

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
public void sendNotify(Context context) {
// 通知管理器
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

String channelId = "notification"; // 通知渠道ID
String name = "channel_name"; // 通知渠道名

// Android 8.0+ 需要创建通知渠道
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
// 创建一个新的通知渠道,并指定渠道ID、名称和重要性级别。
NotificationChannel notificationChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_DEFAULT);
// 将通知渠道与通知管理器绑定
notificationManager.createNotificationChannel(notificationChannel);
}

// 创建一个启动Activity的延迟意图
PendingIntent pendingIntent;
Intent intent = new Intent(context, TeacherLoginActivity.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
// Android 12+ 必须指定为PendingIntent.FLAG_IMMUTABLE或PendingIntent.FLAG_MUTABLE之一
pendingIntent = PendingIntent.getActivity(context, 10001, intent, PendingIntent.FLAG_IMMUTABLE);
} else {
pendingIntent = PendingIntent.getActivity(context, 10001, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}

// 创建通知
Notification notification = new NotificationCompat.Builder(context, channelId)
.setAutoCancel(true) // 点击通知后自动清除
.setContentTitle("消息标题") // 通知标题
.setContentText("消息内容") // 通知内容
.setWhen(System.currentTimeMillis()) // 通知时间
.setSmallIcon(R.drawable.ic_launcher) // 设置通知显示在系统状态栏的小图标
.setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher)) // 设置下拉状态栏后通知的图标
.setContentIntent(pendingIntent) // 点击通知后跳转
.setPriority(NotificationCompat.PRIORITY_MAX) // 设置重要级别
.build();

// 发送通知
int id = 1; // 确保每个通知使用唯一的通知ID
notificationManager.notify(channelId, id, notification);
}