1、Notification的作用
用于通知用户关于APP的一些信息。(例如:推送的聊天消息,升级消息等等)
2、Notification的用法
a、
//创建NotificationBuilder
//创建PendingIntent
//将PendingIntent设置到NotificationBuilder中
//创建Notification
//创建RemoteViews
//将RemoteViews设置到Notification的contentView中
//通过NotificationManager的notify方法,发送通知。
b、Flags属性:
a、FLAG_AUTO_CANCEL:用户点击这个消息,这个消息便消失。
b、FALG_NO_CLEAR:只有全部清空消息时,这个消息才消失。
c、FLAG_SHOW_LIGHTS:三色灯提示,在使用三色灯时,必须加上该标记。
d、FLAG_INSISTENT:让声音,震动,无限循环,直到用户操作。或者代码取消。
e、FLAG_ONLY_ALERT_ONCE:发起通知后,铃声和震动只执行1次。
f、FLAG_ONGOING_EVENT:发起通知正在运行的事件。
3、默认通知的创建步骤:
1、Notification创建,通过系统服务创建
2、创建Notification Builder
3、创建 Builder
4、创建PendingIntent
5、通过NotificationManager来唤醒提交通知
代码:
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
public void showNotification01(NotificationManager notificationManager) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1000,
new Intent(this, FromNotificationActivity.class),
PendingIntent.FLAG_UPDATE_CURRENT);
//创建Notification.Builder
Notification.Builder builder = new Notification.Builder(this);
//配置Builder属性
//设置通知栏图案
builder.setSmallIcon(R.mipmap.ic_launcher);
//设置通知栏缩略内容
builder.setTicker("XXX发来一条语音消息,请注意查收!");
builder.setContentTitle("Notification ID: " + ID_NOTIFICATION);
builder.setContentText("XXX巴拉巴拉说了一堆详细内容");
builder.setNumber(1);
builder.setContentIntent(pendingIntent);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
notification = builder.getNotification();
} else {
Toast.makeText(this, "您的手机太破了,扔了吧!", Toast.LENGTH_SHORT).show();
}
notification.flags = Notification.FLAG_NO_CLEAR;
notificationManager.notify(ID_NOTIFICATION, notification);
}
2、创建自定义通知:
public void showNotification02(NotificationManager notificationManager) {
PendingIntent pendingIntent = PendingIntent.getActivity(this, 1000,
new Intent(this, FromNotificationActivity.class), PendingIntent.FLAG_UPDATE_CURRENT);
Notification.Builder builder = new Notification.Builder(this);
builder.setSmallIcon(R.drawable.icon);
//设置通知栏缩略内容
builder.setTicker("'这是一条自定义通知消息,请查看!");
builder.setContentTitle("Notification ID: " + ID_NOTIFICATION);
builder.setContentText("XXX巴拉巴拉说了一堆详细内容");
builder.setNumber(1);
builder.setContentIntent(pendingIntent);
Notification notification = null;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification = builder.build();
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
notification = builder.getNotification();
} else {
Toast.makeText(this, "您的手机太破了,扔了吧!", Toast.LENGTH_SHORT).show();
}
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
remoteViews.setTextViewText(R.id.textview_title, "'英国成功脱欧");
remoteViews.setTextViewText(R.id.textview_content, "'英国不仅成功脱欧,欧洲杯 英格兰VS冰岛 1:2 ");
remoteViews.setImageViewResource(R.id.imageview_icon, R.drawable.icon);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = remoteViews;
} else {
notification.contentView = remoteViews;
}
notification.contentView = remoteViews;
notification.flags = Notification.FLAG_NO_CLEAR;
notificationManager.notify(ID_NOTIFICATION, notification);
}