NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("ContentTitle")
.setContentText("ContentText!");
Intent resultIntent = new Intent(this, ResultActivity.class);
// The stack builder object will contain an artificial back stack for the started Activity.
// This ensures that navigating backward from the Activity leads out of your application to the Home screen.
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(ResultActivity.class);
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0,PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId 用于之后更新Notification
mNotificationManager.notify(mId, mBuilder.build());
将扩展布局应用于通知
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
String[] events = new String[6];
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.
更新通知
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText).setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is
// updated.
mNotificationManager.notify(notifyID,mNotifyBuilder.build());
...
删除通知的方式
用户点击"全部清除"清除该通知
用户点击通知且创建通知时调用了 setAutoCancel()
您针对特定的通知调用了 cancel(),删除当前通知
您调用 cancelAll() 该方法删除之前发出的所有通知
锁屏通知
可见性 setVisibility()
VISIBILITY_PUBLIC
VISIBILITY_SECRET
VISIBILITY_PRIVATE
播放多媒体
RemoteControlClient (Since API21)废弃
以下代码仅在5.0可用,未包含在support库中
Notification notification = new Notification.Builder(context)
// Show controls on lock screen even when user hides sensitive content.
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setSmallIcon(R.drawable.ic_stat_player)
// Add media control buttons that invoke intents in your media service
.addAction(R.drawable.ic_prev, "Previous", prevPendingIntent) // #0
.addAction(R.drawable.ic_pause, "Pause", pausePendingIntent) // #1
.addAction(R.drawable.ic_next, "Next", nextPendingIntent) // #2
// Apply the media style template
.setStyle(new Notification.MediaStyle()
.setShowActionsInCompactView(1 /* #1: pause button */)
.setMediaSession(mMediaSession.getSessionToken())
.setContentTitle("Wonderful music")
.setContentText("My Awesome Band")
.setLargeIcon(albumArtBitmap)
.build();
Notification
Notification 概述
创建通知
NotificationCompat.Builder.build()
发出通知
NotificationManager.notify()
必须的通知内容
setSmallIcon()
setContentTitle()
setContentText()
其他可选内容 NotificationCompat.Builder
通知的操作
通知的优先级
NotificationCompat.Builder.setPriority()
创建简单的通知
将扩展布局应用于通知
更新通知
删除通知的方式
setAutoCancel()
cancel()
,删除当前通知cancelAll()
该方法删除之前发出的所有通知锁屏通知
setVisibility()
RemoteControlClient(Since API21)废弃自定义布局
Notification.Style
Notification.Style的子类 (Since API 16)
详解各部分
1.BigPictureStyle
2.BigTextStyle
3.DecoratedCustomViewStyle
使用下面三个方法设置合适的自定义View
4.InboxStyle
包含字符串列表的通知(最多5段字符串)
5.MediaStyle
使用MediaStyle创建的Notification有自己的分类,可以使用setCategory()方法设置分类
6.MessageStyle
包含多次往返消息类型的notification,只有大notification才有效果,
RemoteInput (Since API 20)