概述
隨著android版本的不斷升級,google在4.1, 5.0,7.0上不斷的推出了幾款通知樣式來滿足不同的需求。相對于普通樣式,可能很多同學相對陌生。筆者也是逐一寫demo嘗試了不同樣式,其中也有許多需要注意的地方,在這里做個總結,希望能幫助到有需要的童鞋。
前提
既然是Notification,有3個要素一定要記住設置,不論是什么樣式,不然可能會出現無法顯示出通知的結果。
- 小圖標,由 setSmallIcon()設置
- 標題,由 setContentTitle()設置
- 詳細文本,由 setContentText()設置
API Level 14(ICE_CREAM_SANDWICH)
Progress
builder.setTicker("ProcessNotification");
builder.setContentTitle("ProcessNotification");
builder.setContentText("ProcessNotificationProcessNotificationProcessNotification");
new Thread(
new Runnable() {
@Override
public void run() {
int incr;
for (incr = 0; incr <= 100; incr+=5) {
builder.setProgress(100, incr, false);
manager.notify(0, builder.build());
try {
Thread.sleep(1*1000);
} catch (InterruptedException e) {}
}
builder.setContentText("Download complete");
// Removes the progress bar
builder.setProgress(0,0,false);
manager.notify(0, builder.build());
}
}
).start();
如果需要使用進度條,可以不自定義view直接試用process就行了。
效果如下
API Level 16(JB)樣式
BigTextStyle
String content = "BigTextNotificationBigTextNotificationBigTextNotificationBigTextNotification
BigTextNotificationBigTextNotificationBigTextNotificationBigTextNotificationBigTextNotification
BigTextNotificationBigTextNotificationBigTextNotificationBigTextNotificationBigTextNotification
BigTextNotificationBigTextNotificationBigTextNotificationBigTextNotification
BigTextNotificationBigTextNotificationBigTextNotificationBigTextNotification12312312312";
builder.setTicker("Ticker");
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
builder.setContentTitle("ContentTitle");
builder.setContentText(content);
builder.setStyle(new NotificationCompat.BigTextStyle()
.bigText(content)
.setSummaryText("Summary")
.setBigContentTitle("BigContentTitle"));
效果圖如下
默認情況下,只顯示標準的樣式,但是當你下拉該條通知的時候,就會顯示出bigText的樣式,該樣式最高高度為256dp
InboxStyle
builder.setTicker("InBoxNotification");
builder.setContentTitle("InBoxNotification");
builder.setContentText("InBoxNotificationInBoxNotification");
builder.setStyle(new NotificationCompat.InboxStyle()
.setBigContentTitle("BigContentTitle")
.setSummaryText("summartText")
.addLine("1. first").addLine("2. second") .addLine("3. third") .addLine("4. fouth")
.addLine("5. five").addLine("6. six") .addLine("7. seven").addLine("8. eight")
.addLine("9. nine").addLine("10. ten").addLine("11. eleven"));
效果圖如下
默認情況下,只顯示標準的樣式,但是當你下拉該條通知的時候,就會顯示出Inbox的樣式,該樣式最高高度為256dp
BigPic
builder.setTicker("PicNotification");
builder.setContentTitle("ContentTitle");
builder.setContentText("PicNotificationPicNotificationPicNotification");
builder.setStyle(new NotificationCompat.BigPictureStyle()
.setBigContentTitle("BigContentTitle")
.setSummaryText("SummaryText")
.bigPicture(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bigpic))
.bigLargeIcon(BitmapFactory.decodeResource(mContext.getResources(), R.drawable.bigpic)));
效果圖如下
默認情況下,只顯示標準的樣式,但是當你下拉該條通知的時候,就會顯示出BigPic的樣式,該樣式最高高度為256dp。 LargeIcon是左上角的相對較小的圖, Picture是下方最大的圖
API Level 21(L)
hands-up
builder.setTicker("FloatNotification");
builder.setContentTitle("FloatNotification title");
builder.setContentText("FloatNotification ContentText");
//must set vibrate or ringtong with high/max priority
builder.setVibrate(new long[0]);
builder.setPriority(Notification.PRIORITY_HIGH);
//or set full screen intent
//builder.setFullScreenIntent(pendingIntentA, true);
Intent intentA = new Intent(mContext, ActivityA.class);
PendingIntent pendingIntentA = PendingIntent.getActivity(mContext, 0, intentA,
PendingIntent.FLAG_UPDATE_CURRENT);
Intent intentB = new Intent(mContext, ActivityB.class);
PendingIntent pendingIntentB = PendingIntent.getActivity(mContext, 0, intentB,
PendingIntent.FLAG_UPDATE_CURRENT);
builder.addAction(R.mipmap.ic_launcher, "ActivityA", pendingIntentA);
builder.addAction(R.mipmap.ic_launcher, "ActivityB", pendingIntentB);
效果圖如下
當我們處于亮屏狀態時,會出現一個懸浮的通知欄告訴用戶有通知來了,并且可以設置Action做相對應的動作。
如果我們收起了懸浮通知,在狀態欄中會顯示默認樣式,點擊或者下拉展開后就又是懸浮的樣式了
注意:
如果想要出現懸浮通知欄是有條件的,以下2個必須至少滿足一個。
- 設置PRIORITY_HIGH或者PRIORITY_MAX的優先級 并且同時設置震動或者鈴聲
- 使用setFullScreenIntent的方式
Visibility
嚴格來說,這只是屬于notification的一個顯示策略,不算樣式,不過也列出來.
builder.setTicker("NormalNotification");
builder.setContentTitle("NormalNotification");
builder.setContentText("NormalNotification ContentText");
builder.setVisibility(NotificationCompat.VISIBILITY_PRIVATE);
效果圖如下
在鎖屏界面如果設置了VISIBILITY_PRIVATE,是不會顯示內容的,如果設置了VISIBILITY_SECRET則在鎖屏界面什么都不會顯示
解鎖以后,和普通通知是一樣的。
注意:
要讓該設置起效,必須滿足下面的條件
- 鎖屏界面是密碼鎖屏(PIN 圖案等)
- 在系統設置中選擇密碼鎖屏后,還需要設置"隱藏敏感信息"(Hide sensitive notification content)
如果沒有同時滿足上面2個條件,這個設置將不會起效果
API Level 24(N)
MessagingStyle
builder.setStyle(new NotificationCompat.MessagingStyle("DisplayName2")
.addMessage("addMessageA", (int)System.currentTimeMillis(), "sender1")
.addMessage("addMessageB", (int)System.currentTimeMillis(), "sender2")
.addMessage("addMessageC", (int)System.currentTimeMillis(), "sender3")
.addMessage("addMessageD", (int)System.currentTimeMillis(), "sender4")
.setConversationTitle("ConversationTitle"));
在代碼中,顯示了2條MessagingStyle樣式的通知,效果如下
收起前就是一個普通通知,點擊展開后可以看出是按照DisplayName來進行分組的,這個樣式和Inbox樣式十分相似。
備注
可以看出,在android N上,通知的默認樣式已經做了修改
注意
如果想在N上使用該樣式,v4包也必須是相對應的24版本,不然找不到該樣式.
總結
以上就是在android各個版本中默認的通知樣式,大家可以到v4包里面看下,不同的樣式所支持的方法,其實并不多,掌握起來會比較方便。上文講了一些筆者遇到的坑,如果還有錯過的地方歡迎大家留言指出.。-
More
如果這些樣式不能滿足你的需求,那么就只有自定義通知欄樣式了,自定義樣式也要根據不同的android版本來做不同的定義,比如在大于android JB的版本上就可以支持展開默認通知顯示bigview,詳情請點擊Notification之----自定義樣式
相關閱讀
Notification之----Android5.0實現原理(二)
Notification之----Android5.0實現原理(一)
Notification之---NotificationListenerService5.0實現原理
Notification之----自定義樣式
Notification之----任務棧