Notification作爲一個事件觸發通知型的交互提示接口,讓我們可以再獲得消息的時候,再狀態欄、鎖屏界面得到相應的提示信息。從QQ、微信到各種推送通知、短信,這些Notification常常出現在狀態欄。
Google在Android5.0上又進一步改進了通知欄,優化了Notification。現在,在Android5.X設備上,一個標準的Notification界面如下圖所示。
當長按Notification的時候,會顯示消息來源,如圖12.33所示。
Notification會有一個從白色到灰色的動畫切換效果,最終顯示發出這個Notification的調用者。同時,在Android 5.X設備上,鎖屏狀態下我們也可以看見Notification通知了,如圖所示。
1.基本的Notification
通過NotificationCompat.Builder創建一個NotificationCompat的builder,代碼如下所示。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "basic_notification");
這個與AlterDialog的使用方法是不是非常相似呢?接下來,給點擊Notification後要執行的操作增進一個Intent,由於這個Intent不是馬上就執行的,而是由用戶觸發的,所以Android給這樣的Intent提供了一個PendingIntent來幫助我們完成這樣的延遲操作。
PendingIntent的使用非常簡單,只需要在普通的Intent的基礎上包裝一層就可以了,代碼如下所示。
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
//構造PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
這樣當點擊Notification之後,就會出發PendingIntent事件,打開瀏覽器開始瀏覽網頁。
與使用AlertDialog一樣,有了builder對象,可以給他增加各種屬性。
builder.setSmallIcon(R.drawable.blue);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.red));
builder.setContentTitle("Basic Notification");
builder.setContentText("I am a basic Notification");
builder.setSubText("it is really basic");
這些屬性的具體含義,也不必一個個去仔細理解,結合後面演示的效果和命名,很容易就能理解了。最後一步,自然是將
Notification顯示出來,Android系統通過NotificationManager系統服務來幫助我們管理Notification,並通過調用notify方法來發出Notification。
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_BASIC, builder.build());
調用NotificationManager的notify()方法時,需要穿進去一個ID。每個Notification都會有一個ID,這個ID就是用來區分不同的APP的Notification的。
通過以上很簡單的幾個步驟,就完成了基本的Notification。Notification還可以配置LED燈和震動等選項,不過這些都只是增加一個屬性罷了。最後來看看BasicNotification的效果如何,如圖所示。
相信結合運行出來的例子,大家應該可以很清楚的理解上面配置的參數的意義了.
2.摺疊式Notification
摺疊式Notification也是一種自定義視圖的Notification,常常用於顯示長文本。它擁有兩個視圖狀態,一個是普通狀態下的視圖狀態,另一個是展開狀態下的視圖狀態。在Notification中,使用RemoteViews來幫助我們創建一個自定義的Notification視圖,代碼如下所示。
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification);
contentView.setTextViewText(R.id.tv, "show me when collapsed");
Notification的佈局如下所示。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tv"
android:textColor="#ff43aebe"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="@+id/iv"
android:src="@drawable/robot"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
通過如下代碼,就可以將一個視圖指定爲Notification正常狀態下的視圖。
notification.contentView = contentView;
通過如下代碼,就可以將一個視圖指定爲Notification展開狀態下的視圖。
notification.bigContentView = expandView;
完整代碼如下所示。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, "basic_notification");
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.baidu.com"));
//構造PendingIntent
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
builder.setSmallIcon(R.drawable.blue);
builder.setContentIntent(pendingIntent);
builder.setAutoCancel(true);
builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.red));
Notification notification = builder.build();
//通過RemoteViews來創建自定義的Notification視圖
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.notification);
//指定視圖
notification.contentView = contentView;
//通過RemoteViews來創建自定義的Notification視圖
RemoteViews expandView = new RemoteViews(getPackageName(),R.layout.notification_expand);
//指定視圖
notification.bigContentView = expandView;
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_COLLAPSE, notification);
摺疊時和展開式的圖片如下所示。
3.懸掛式Notification
懸掛式Notification時再Android5.0中新增的方式,Google系統通過這種方式來給用戶帶來更好的體驗,這種被稱爲Headsup的Notification方式,可以再屏幕上方產生Notification且不會打斷用戶操作,能給用戶已Notification形式的通知。
再Android Sample中,Google給我們展示瞭如何完成這樣一個效果,代碼如下所示。
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,"Push Notification")
.setSmallIcon(R.mipmap.ic_launcher)
.setPriority(Notification.PRIORITY_DEFAULT)
.setCategory(Notification.CATEGORY_MESSAGE)
.setContentTitle("Headsup Notification")
.setContentText("I am a Headsup Notification");
Intent push = new Intent();
push.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
push.setClass(this, MyNotification.class);
PendingIntent pi = PendingIntent.getActivity(this,0,push,PendingIntent.FLAG_CANCEL_CURRENT);
builder.setContentText("Heads-Up Notification on Android 5.0")
.setFullScreenIntent(pi,true);
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFICATION_ID_COLLAPSE, builder.build());
如上所示,通過setFullScreenIntent,我們很輕鬆地將一個Notification變成了懸掛式Notification,程序顯示效果如下所示。
4. 顯示等級的Notification
最後也就是再Android 5.X中新加入的一種模式——Notification的顯示等級。Android 5.X將Notification分成了三個等級。
- VISIBILIETY_PRIVATE——表面當前沒有鎖屏的時候會顯示
- VISIBILIETY_PUBLIC——表面任何情況下都會顯示
- VISIBILIETY_SECRET——表面在pin、password等安全鎖和沒有鎖屏的情況下才能夠顯示
設置Notification等級的方式非常簡單,同樣時藉助builder對象,代碼如下所示。
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
在前面的示例代碼中,我們使用了一個RadioGroup來演示VISIBILIETY等級,實現代碼如下所示。
private void selectNotofovatiomLevel(Notification.Builder builder) {
switch (radioGroup.getCheckedRadioButtonId()) {
case R.id.rb_public:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText("public");
break;
case R.id.rb_private:
builder.setVisibility(Notification.VISIBILITY_PRIVATE);
builder.setContentText("private");
break;
case R.id.rb_secret:
builder.setVisibility(Notification.VISIBILITY_SECRET);
builder.setContentText("secret");
break;
default:
builder.setVisibility(Notification.VISIBILITY_PUBLIC);
builder.setContentText("public");
break;
}
}
代碼結構非常清晰,通過build對象的setVisibility方法,就可以輕鬆地設置Notification顯示等級了。
Notification在Android 5.X中地改動非常多,這裏只是讓大家有一個初步地概念,還有其他地改動,比如以下兩種。
增加了設置Notification背景顏色地接口,代碼如下所示。
builder.setColor(Color.RED)
增加了設置Notification的category接口,category用來確定Notification顯示的位置,參數就是各種category類型,代碼如下所受。
setCategory(Notification.CATEGORY_MESSAGE)