logo头像
Snippet 博客主题

Android Notification

Android Notification

1
2
3
通知,是一种具有全局效果的通知,它展示在屏幕的顶端,

首先会表现为一个图标的形式,当用户向下滑动的时候,展示出通知具体的内容。

Android 的版本也快速的升级导致了一些兼容性的问题
几个重大的版本变更

android version 系统修改 简介 兼容
Android3.0 之前 使用Notification.Builder构建 - -
Android3.0 推荐使用Notification.Builder构建 android.support.v4.app.NotificationCompat.Builder 一个Android向下版本的兼容包support.v4
Android 5.0 Google为了统一 通知栏风格,要求notification 图标都变成单色,如果不是单色则会通过算法变成单色。 当然这不是所有系统都强制改变的,目前好像只有nexus 系列 和 HTC手机会遵循这一原则。 builder.setSmallIcon 设置 单一颜色图片
Android 8.0 NotificationChannel是android8.0新增的特性 如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示 创建channel
Android 9.0 Notification 提升以及 刘海的适配 暂时 没有这款手机 没有实践 没有发言权

创建

市场上基本值兼容 android 4.X.X 以上版本
android 3.0 的 创建方法 就不介绍了 support.v4.NotificationCompat.Builder 可以很好的兼容 之前的版本

适配 android 8.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);
channel.canBypassDnd();//是否绕过请勿打扰模式
channel.enableLights(true);//闪光灯
channel.setLockscreenVisibility(Notification.VISIBILITY_SECRET);//锁屏显示通知
channel.setLightColor(Color.RED);//闪关灯的灯光颜色
channel.canShowBadge();//桌面launcher的消息角标
channel.enableVibration(true);//是否允许震动
channel.getAudioAttributes();//获取系统通知响铃声音的配置
channel.getGroup();//获取通知取到组
channel.setBypassDnd(true);//设置可绕过 请勿打扰模式
channel.setVibrationPattern(new long[]{100, 100, 200});//设置震动模式
channel.shouldShowLights();//是否会有灯光
getManager().createNotificationChannel(channel);
}

NotificationCompat.Builder builder = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
builder = new NotificationCompat.Builder(Utils.getContext(), CHANNEL_ID);
} else {
builder = new NotificationCompat.Builder(Utils.getContext());
builder.setPriority(NotificationCompat.PRIORITY_DEFAULT);
}

不同

  1. android createNotificationChannel
  2. 创建 NotificationCompat.Builder 方式

设置相关属性

属性 设置方式 是否必须
setSmallIcon 小图标 YES
setContentTitle 标题 YES
setContentText 内容 YES
Action PendingIntent pi = PendingIntent.getActivity(Utils.getContext(), notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);builder.setContentIntent(pi); NO
setAutoCancel 点击自动删除通知 NO
setWhen 设置时间 按照时间排序显示 NO
setGroupSummary 设置分组显示 NO
setGroup 设置分组名 NO
1
2
3
只有三个必要的属性   还有 其他很多不一一介绍了
这些属性 不同手机上面 会存在不同的问题 国产手机 问题突出
例如设置呼吸灯 小米有的手机不能达到预期(android 定制)

兼容问题

1. 图标显示白色

1
2
3
4
5
6
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
builder.setSmallIcon(R.drawable.snap_icon);
builder.setColor(Utils.getContext().getResources().getColor(R.color.new_orange));
} else {
builder.setSmallIcon(R.drawable.snap_icon_notification);
}

2. 点击 notification item 数据不对

问题 点击 notification item 获取的数据 总是 最新的那条数据

解决方案

PendingIntent.getActivity 中 requestCode
刚刚开始一直写成 0
导致的
requestCode 修改成 不一致

1
PendingIntent pi = PendingIntent.getActivity(Utils.getContext(), notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

系统显示

1
2
3
4
5
6
private static NotificationManager getManager() {
if (mManager == null) {
mManager = (NotificationManager) Utils.getContext().getSystemService(Context.NOTIFICATION_SERVICE);
}
return mManager;
}
1
2

getManager().notify(notificationId, notification);

问题

  1. 悬浮通知不显示,只有状态栏有
1
2
3
4
5
6
7
8
9
10
11
1. 首先需要打开手机设置查看通知相关的权限(横幅)是否打开

2. 对于Android8.0以下的手机,需要在设置builder时加入以下代码:

//NotificationCompat.Builder builder
builder.setPriority(NotificationCompat.PRIORITY_MAX)
builder.setDefaults(DEFAULT_ALL)

3.对于Android8.0以上的手机,需要在建立Channel时设置优先级为HIGH:

NotificationChannel chan1 = new NotificationChannel("dynamic", "Primary Channel", NotificationManager.IMPORTANCE_HIGH);

更新

更新 其实 与创建差不多

1
2
3
4
5
6
注意 
1. notificationId 一定要一直
2. PendingIntent.FLAG_UPDATE_CURRENT


PendingIntent pi = PendingIntent.getActivity(Utils.getContext(), notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT);

取消

1
NotificationManager nm = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
方法 含有
public void cancel(int id) 表示该通知能被状态栏的清除按钮清除掉
public void cancelAll() 移除所有已经显示的通知
public void notify(int id, Notification notification) 提交一个通知在状态栏中显示。如果拥有相同标签和相同id的通知已经被提交而且没有被移除,该方法会更新之前的通知
支付宝打赏 微信打赏

打赏