获取通知管理器
1
| NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
Android 8.0+,需要为通知管理器添加一个通知渠道,并传入三个参数:渠道ID、渠道名、重要级别
- 渠道ID:需要和后面创建
Notification所使用的channelId一致,创建后尽量避免修改 - 渠道名:显示给用户,传达通知的来源和内容
- 重要级别:有以下五种级别
IMPORTANCE_DEFAULT(默认):通知的重要性属于中等级别,不会打断用户,但会显示在通知栏中。IMPORTANCE_HIGH(高):通知的重要性属于高级别,会以声音和震动的方式提醒用户,并在锁屏上显示。IMPORTANCE_LOW(低):通知的重要性属于低级别,不会打扰用户,而且不会在锁屏上显示。IMPORTANCE_MIN(最低):通知的重要性属于最低级别,不会打断用户,不会发出声音,也不会在锁屏上显示。IMPORTANCE_NONE(无):通知的重要性为零,将完全隐藏通知。
1 2 3 4 5 6 7 8 9 10
| String channelId = "notification"; String name = "channel_name";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { 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
| PendingIntent pendingIntent; Intent intent = new Intent(context, TeacherLoginActivity.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { 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();
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种类别
Notification.CATEGORY_ALARM:表示通知是一个闹钟或定时器相关的通知。Notification.CATEGORY_CALL:表示通知与电话通话相关。Notification.CATEGORY_EMAIL:表示通知与电子邮件相关。Notification.CATEGORY_ERROR:表示通知是一个错误或异常相关的通知。Notification.CATEGORY_EVENT:表示通知与日历事件或日程安排相关。Notification.CATEGORY_MESSA:表示通知是一条消息或聊天相关的通知。Notification.CATEGORY_PROGRESS:表示通知与长时间运行的操作或进度相关。Notification.CATEGORY_PROMO:表示通知是营销或促销相关的通知。Notification.CATEGORY_RECOMMENDATIO:表示通知是一个推荐或建议相关的通知。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"; String name = "channel_name";
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { NotificationChannel notificationChannel = new NotificationChannel(channelId, name, NotificationManager.IMPORTANCE_DEFAULT); notificationManager.createNotificationChannel(notificationChannel); }
PendingIntent pendingIntent; Intent intent = new Intent(context, TeacherLoginActivity.class); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { 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; notificationManager.notify(channelId, id, notification); }
|