Notification是通过建造者模式来创建。
为了兼容低版本,v4 Support Library中提供了NotificationCompat.Builder()这个替代方法。
它与原来的Notification.Builder()类似,二者没有太大区别。
先来一段代码demo:
NotificationCompat.Builder builder = new NotificationCompat.Builder(context); // 旧API
Intent intent = new Intent(this, MainActivity.class); //点击之后进入MainActivity
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher) //设置小图标
.setTicker("hello world") //设置文字
.setWhen(System.currentTimeMillis()) //通知的时间
.setAutoCancel(true) //点击后消失
.setContentIntent(pendingIntent) //设置意图
.build(); //创建通知对象完成
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(1, notification); //显示通知
属性
Notification.DEFAULT_VISIBLE //添加默认震动提醒 需要VIBRATE permission
Notification.DEFAULT_SOUND //添加默认声音提醒
Notification.DEFAULT_LIGHTS //添加默认三色灯提醒
Notification.DEFAULT_ALL //添加默认以上三种全部提醒
需要用到RemoteViews。我们先写好自定义通知的布局文件:
android:layout_height="48dp"
android:orientation="vertical"
android:paddingEnd="48dp"
android:paddingStart="48dp">
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerInParent="true"
android:src="@mipmap/ic_launcher_round" />
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:gravity="center_vertical"
android:text="Left" />
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:gravity="center_vertical"
android:text="Right" />
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.layout_notificaiton);
remoteViews.setTextViewText(R.id.tv_5_s, "Start");//通过id-内容的方式设置remoteview中控件的内容,底层实现是通过Binder跨进程通信
remoteViews.setTextViewText(R.id.tv_5_e, "End");
remoteViews.setImageViewResource(R.id.icon_5, R.mipmap.ic_launcher);
remoteViews.setOnClickPendingIntent(R.id.tv_5_e, pendingIntent);
Notification notification = builder.setSmallIcon(R.mipmap.ic_launcher)//通知的构建过程基本与默认相同
.setTicker("hello world")
.setWhen(System.currentTimeMillis())
.setAutoCancel(true)
.setContent(remoteViews)//在这里设置自定义通知的内容
.build();
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
manager.notify(2, notification);可以看到Remoteview更新内容的方式比较特别,因为不能通过findViewById的方式获取控件(跨进程了获取不到),所以更新实际上是通过Binder发送更新信息到remoteview来更新UI的。
NotificationChannel是android8.0新增的特性,如果App的targetSDKVersion>=26,没有设置channel通知渠道的话,就会导致通知无法展示。
String channelID = "1";
String channelName = "channel_name";
NotificationChannel channel = new NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH);
NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
manager.createNotificationChannel(channel);
// 利用channelName可以在系统设置页面对app某个名称的通知进行管理
NotificationCompat.Builder builder =new NotificationCompat.Builder(context, channelName);
builder.setContentText(msgDesc);
builder.setContentTitle(msgTitle);
//创建通知时指定channelID
builder.setChannelId(channelID);
Notification notification = builder.build();
注意:修改NotificationChannel 属性,升级app后该修改不生效,必须卸载app重新安装才能生效,原代码如下:
public void notifyDownloading(long progress, long num, String chName) {
Notification.Builder mBuilder = new Notification.Builder(MainActivity.this, TAG );
NotificationChannel channel = new NotificationChannel(TAG , chName, NotificationManager.IMPORTANCE_HIGH);
mNotifyManager.createNotificationChannel(channel);
mBuilder.setSmallIcon(R.drawable.notification_download_icon);
mBuilder.setProgress((int) num, (int) progress, false);
mBuilder.setContentInfo(getPercent((int) progress, (int) num));
mBuilder.setOngoing(true);
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setContentTitle(chName);
mBuilder.setContentText("download");
PendingIntent pendIntent = PendingIntent.getActivity(
MainActivity.this, NOTIFY_ID, getCurActivityIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendIntent);
mNotifyManager.notify(NOTIFY_ID, mBuilder.build());
}
这里将IMPORTANCE_HIGH修改为IMPORTANCE_LOW,通过Android Studio直接安装,发现修改不生效。
解决方法:修改创建Notification.Builder的id和NotificationChannel的id,修改后代码如下:
public void notifyDownloading(long progress, long num, String chName) {
Notification.Builder mBuilder = new Notification.Builder(MainActivity.this, TAG + System.currentTimeMillis());
NotificationChannel channel = new NotificationChannel(TAG + System.currentTimeMillis(), chName, NotificationManager.IMPORTANCE_LOW);
mNotifyManager.createNotificationChannel(channel);
mBuilder.setSmallIcon(R.drawable.notification_download_icon);
mBuilder.setProgress((int) num, (int) progress, false);
mBuilder.setContentInfo(getPercent((int) progress, (int) num));
mBuilder.setOngoing(true);
mBuilder.setWhen(System.currentTimeMillis());
mBuilder.setContentTitle(chName);
mBuilder.setContentText("download");
PendingIntent pendIntent = PendingIntent.getActivity(
MainActivity.this, NOTIFY_ID, getCurActivityIntent(), PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(pendIntent);
mNotifyManager.notify(NOTIFY_ID, mBuilder.build());
}
通过System.currentTimeMillis()保证每次创建对象的Id不同。
参考:
https://blog.csdn.net/qq_25749749/article/details/80449108
https://www.jianshu.com/p/ca92797d925a
https://www.jianshu.com/p/33a18f224a60
https://blog.csdn.net/agaghd/article/details/79016812
https://www.jianshu.com/p/33a18f224a60
https://blog.csdn.net/agaghd/article/details/79016812