如何创建通知
随着Android系统不断升级,Notification的创建方式也随之变化,主要变化如下:
Android 3.0之前
Android 3.0 (API level 11)之前,使用new Notification()方式创建通知:
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(this, title, content, contentIntent);
mNotifyMgr.notify(NOTIFICATIONS_ID, notification);
Android 3.0 (API level 11)及更高版本
Android 3.0开始弃用new Notification()方式,改用Notification.Builder()来创建通知:
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(
this, 0, new Intent(this, ResultActivity.class), 0);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!")
.setContentIntent(contentIntent)
.build();// getNotification()
mNotifyMgr.notify(NOTIFICATIONS_ID, notification);
这里需要注意: "build()" 是Androdi 4.1(API level 16)加入的,用以替代
"getNotification()"。API level 16开始弃用"getNotification()"
通知基本用法
- 一个通知必须包含以下三项属性:
小图标,对应 setSmallIcon()
通知标题,对应 setContentTitle()
详细信息,对应 setContentText()
其他属性均为可选项,更多属性方法请参考https://blog.csdn.net/yxncl/article/details/72801230。 - 创建通知
1、实例化一个NotificationCompat.Builder对象
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("My notification")
.setContentText("Hello World!");
NotificationCompat.Builder自动设置的默认值:
priority: PRIORITY_DEFAULT
when: System.currentTimeMillis()
audio stream: STREAM_DEFAULT
- 定义并设置一个通知动作(Action)
Intent resultIntent = new Intent(this, ResultActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(
this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
- 生成Notification对象
Notificatioin notification = mBuilder.build();
- 使用NotificationManager发送通知
// Sets an ID for the notification
int mNotificationId = 001;
// Gets an instance of the NotificationManager service
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Builds the notification and issues it.
mNotifyMgr.notify(mNotificationId, notification);
- 更新通知
更新通知很简单,只需再次发送相同ID的通知即可,如果之前的通知依然存在则会更新通知属性,如果之前通知不存在则重新创建。示例代码:
NotificationManager mNotifyMgr =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
NotificationCompat.Builder mNotifyBuilder =
new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status);
int numMessages = 0;
...
mNotifyBuilder.setContentText("new content text")
.setNumber(++numMessages);
mNotifyMgr.notify(notifyID, mNotifyBuilder.build());
...
- 取消通知
取消通知有如下4种方式:
点击通知栏的清除按钮,会清除所有可清除的通知
设置了 setAutoCancel() 或 FLAG_AUTO_CANCEL的通知,点击该通知时会清除它
通过 NotificationManager 调用 cancel() 方法清除指定ID的通知
通过 NotificationManager 调用 cancelAll() 方法清除所有该应用之前发送的通知
通知类型
通知有两种视图:普通视图和大视图。
普通视图:
![](https://img.haomeiwen.com/i10968127/79d14d3bb3dda0b9.png)
大视图:
[图片上传失败...(image-f0e56-1560913700779)]
默认情况下为普通视图,可通过NotificationCompat.Builder.setStyle()设置大视图。
注: 大视图(Big Views)由Android 4.1(API level 16)开始引入,且仅支持Android 4.1及更高版本。
网友评论