Android Notification通知欄開發詳解

MellissaXhw 8年前發布 | 78K 次閱讀 Android開發 移動開發

 Notification是在你的應用常規界面之外展示的消息。當app讓系統發送一個消息的時候,消息首先以圖表的形式顯示在通知欄。要查看消息的詳情需要進入通知抽屜(notificationdrawer)中查看。通知欄和通知抽屜(notificationdrawer)都是系統層面控制的,你可以隨時查看,不限制于app。


圖 1.通知欄的通知


圖 2. notificationdrawer中的通知。


Notification 的設計

 

作為android UI中很重要的組成部分,notification擁有專屬于自己的設計準則。如果想了解如何設計notification以及其交互,請閱讀android 設計指南的notification一章。


Notification的界面元素


在通知抽屜中的notification有兩種顯示方式,取決于你的android版本以及notification drawer的狀態:

普通視圖

這種風格是notification drawer的標準顯示方式。

寬視圖

指你的notification被展開的時候會顯示更大的視圖,這種風格是android4.1之后才有的新特性。

下面將詳細介紹兩種情況。

 

普通視圖


在圖通視圖中,notification最高64dp,即使你創建了一個寬視圖風格的notification,在未展開的情況下也是以普通大小顯示出來。下面是一個普通的notification。

圖 3.正常狀態下的notification


藍色指示框所代表的的意思如下:

1.標題

2.大圖標

3.通知內容

4.通知數據

5.小圖標

6.Notification的發布時間。可以通過調用setWhen()設置一個明確的時間,默認是系統收到該notification的時間。

 

寬視圖

 

只有當notification被展開的時候這種寬視圖的notification才會出現,通過手勢操作可以展開一個普通的notification(如果能展開的話)。這種風格的notification從android4.1以后才開始支持。下面的截圖展示了inbox風格的notification:

圖 4.寬視圖notification


你應該注意到了這種notification其實跟普通的沒多大差別,唯一的區別在于數字7-詳情區域。不同寬視圖notification這里的顯示是有區別的,有如下幾種風格:

大圖標風格

詳情區域顯示一個最高位256dpbitmap

文字風格

詳情區域顯示一段文字

消息盒子風格(Inbox style

詳情區域顯示幾行文字

至于如何設置寬視圖風格的notification可以參考這篇文章Applyinga big view style to a notification.


創建notification

 

首先將notification的一些UI信息以及相關動作賦予NotificationCompat.Builder對象,然后通過NotificationCompat.Builder.build()來獲得notification對象自己;然后調用NotificationManager.notify()向系統轉交這個通知。一個notification對象需要包含如下內容:

小圖標(setSmallIcon()獲取)

標題(setContentTitle()獲取)

詳情文字(setContentText()獲取)

 

除此之外,其余內容都是可選的,要了解這些可選的內容參考NotificationCompat.Builder的文檔。

 

Notification的動作與行為

 

雖然這也是可選的,但是你還是應該為你的notification至少添加一種行為:允許用戶通過點擊notification進入一個activity中進行更多的查看或者后續操作。一個notification可以提供多種動作,而且你也應該讓用戶點擊一個notification之后能總是有相應的響應動作,通常是打開一個activity。你還可以在notification中添加能響應點擊事件的button,比如延遲一下鬧鐘,或者立即回復一條短消息。

在notification內部,一個動作本身是被定義在一個PendingIntent中,PendingIntent包含了一個用于啟動你app中activity的intent。要將PendingIntent和一個手勢聯系起來,你需要調用合適的NotificationCompat.Builder方法。比如你想在點擊notification文字的時候啟動activity,你需要調用NotificationCompat.Builder的setContentIntent()來添加PendingIntent。啟動一個activity是notification動作響應中最普遍的一類。


創建一個簡單的notification


下面的代碼片段演示了一個打開activity的notification,注意這里獲取PendingIntent是通過創建TaskStackBuilder對象:

NotificationCompat.Builder mBuilder =
        new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.notification_icon)
        .setContentTitle("My notification")
        .setContentText("Hello World!");
// Creates an explicit intent for an Activity in your app
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);
// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(
            0,
            PendingIntent.FLAG_UPDATE_CURRENT
        );
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
    (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(mId, mBuilder.build());

就是這么簡單,通過上面的代碼你就能為你的用戶發出一個系統通知。


notification設置寬視圖樣式


要獲得一個能展開的寬視圖樣式的notification首選創建一個普通的NotificationCompat.Builder對象,然后調用Builder.setStyle()。記住這種notification只有4.1版本以上才支持。

下面的例子演示了在上面的普通notification的基礎上添加寬視圖風格的notification:

 

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 style big view
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the big view
for (int i=0; i < events.length; i++) {
    inboxStyle.addLine(events[i]);
}
// Moves the big view style object into the notification object.
mBuilder.setStyle(inBoxStyle);
...
// Issue the notification here.

 

 

 

 本文由用戶 MellissaXhw 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!