在Android N中對Notification進行了很大的更新爷怀,添加了一些新的API
Android N,Notification的新特性
- 新的樣式模板-更簡約碧囊,顯的不是那么凌亂
- 不在和status bar捆綁
- 不需要打開應(yīng)用,就可以直接回復(fù)
- 個性化定制
Notification 新模板
在Android N中引入了一些新的通知模板,使得通知看起來更簡潔,這些模板是系統(tǒng)提供的,不
需要開發(fā)者修改自己的代碼
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_phonelink_ring_primary_24dp)
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(largeIcon)
.setAutoCancel(true);
在Android N之前的版本上的效果圖如下
同樣的代碼在Android N的效果圖如下

不需要修改任何代碼脊阴,在Android N上就可以體驗到新的通知模板
使用 setColor()
在創(chuàng)建通知時也可以使用方法 setColor()
,來改變字體的顏色
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Other properties
.setColor(ContextCompat.getColor(context, R.color.primary));
效果圖
捆綁通知
在Android N中捆綁通知-就是一個通知組可以顯示很多條通知,對于同一個應(yīng)用的多個通知
就可以使用這個特性,這樣可以避免很多的通知對用戶造成干擾
必須為這組通知設(shè)置組的描述
在創(chuàng)建通知時使用setGroup()
NotificationCompat.Builder builderOne = new NotificationCompat.Builder(context)
// Other properties
.setGroupSummary(true)
.setGroup(KEY_NOTIFICATION_GROUP);
NotificationCompat.Builder builderTwo = new NotificationCompat.Builder(context)
// Other properties
.setGroupSummary(true)
.setGroup(KEY_NOTIFICATION_GROUP);
NotificationCompat.Builder builderThree = new NotificationCompat.Builder(context)
// Other properties
.setGroupSummary(true)
.setGroup(KEY_NOTIFICATION_GROUP);
效果圖
和創(chuàng)建標(biāo)準(zhǔn)的通知的方式一樣嘿期,創(chuàng)建不同的通知的實例品擎,然后將這些實例設(shè)置一個 setGroup()
PendingIntent archiveIntent = PendingIntent.getActivity(...);
NotificationCompat.Action replyAction = ...;
NotificationCompat.Action archiveAction = ...;
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
// Other properties
.setGroup(KEY_NOTIFICATION_GROUP)
.addAction(replyAction)
.addAction(archiveAction);
直接回復(fù)
使用API RemoteInput 就可以在通知中直接進行回復(fù)
添加直接回復(fù)操作
需要創(chuàng)建一個RemoteInput的實例
RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
.setLabel(LABEL_REPLY)
.build();
- 通過創(chuàng)建PendingIntent 實例,和應(yīng)用進行交互
PendingIntent replyIntent = PendingIntent.getActivity(context,
REPLY_INTENT_ID,
getDirectReplyIntent(context, LABEL_REPLY),
PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent archiveIntent = PendingIntent.getActivity(context,
ARCHIVE_INTENT_ID,
getDirectReplyIntent(context, LABEL_ARCHIVE),
PendingIntent.FLAG_UPDATE_CURRENT);
private static Intent getDirectReplyIntent(Context context, String label) {
return MessageActivity.getStartIntent(context)
.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
.setAction(REPLY_ACTION)
.putExtra(CONVERSATION_LABEL, label);
}
- 創(chuàng)建
NotificationCompat.Action
有了PendingIntents
,我們就需要結(jié)合RemoteInput
來創(chuàng)建 NotificationCompat.Action
NotificationCompat.Action replyAction =
new NotificationCompat.Action.Builder(R.drawable.ic_reply,
LABEL_REPLY, replyIntent)
.addRemoteInput(remoteInput)
.build();
NotificationCompat.Action archiveAction =
new NotificationCompat.Action.Builder(R.drawable.ic_archive,
LABEL_ARCHIVE, archiveIntent)
.build();
- 創(chuàng)建
Notification
實例
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_phonelink_ring_primary_24dp)
.setContentTitle(title)
.setContentText(message)
.setLargeIcon(largeIcon)
.addAction(replyAction);
.addAction(archiveAction);
.setAutoCancel(true);
應(yīng)用接收
創(chuàng)建了直接回復(fù)秽五,還需要在應(yīng)用端對回復(fù)的內(nèi)容做處理
Bundle bundle = RemoteInput.getResultsFromIntent(intent);
if (remoteInput != null) {
return bundle.getCharSequence(NotificationUtil.KEY_TEXT_REPLY);
}
定制通知
定制盡量的按照Android Design 的規(guī)則進行定制化孽查,主要是使用 RemoteViews
來定制通知
RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.notif_custom_view);
remoteViews.setImageViewResource(R.id.image_icon, iconResource);
remoteViews.setTextViewText(R.id.text_title, title);
remoteViews.setTextViewText(R.id.text_message, message);
remoteViews.setImageViewResource(R.id.image_end, imageResource);