1. 程式人生 > >通知欄Notification的整理

通知欄Notification的整理

異步 onclick 新的 sum local etl dap pack lag

一、介紹

  通知欄適用於交互事件的通知,是位於頂層可以展開的通知列表。

二、功能作用

  1.顯示接收到短消息,及時消息等信息(如QQ、微信、新浪、短信)
2.顯示客戶端的推送消息(如有新版本發布,廣告。推薦新聞等)
  3.顯示正在進行的事物(例如:後臺運行的程序)(如音樂播放器、版本更新時候的下載進度等)

三、Notification

  1.屬性:設置提醒標識符Flags
   功能:提醒標誌符,向通知添加聲音、閃光燈和震動效果等設置達到通知提醒效果,可以組合多個屬性。
   例子:有兩種設置方法:
    (1)實例化通知欄之後通過給他添加flags屬性賦值。

Notification notification = mBuilder.build();
notification.flags = Notification.FLAG_AUTO_CANCEL;

    (2)通過setContentIntent(PendingIntent intent)方法中的意圖設置對應的flags。

PendingIntent pendingIntent = PendingIntent.getActivity(this, 1, new Intent(), flags);

   提醒標識符成員:

Notification.FLAG_SHOW_LIGHTS //
三色燈提醒,在使用三色燈提醒時候必須加該標誌符 Notification.FLAG_ONGOING_EVENT //發起正在運行事件(活動中) Notification.FLAG_INSISTENT //讓聲音、震動無限循環 Notification.FLAG_ONLY_ALERT_ONCE //發起Notification後,鈴聲和震動均只執行一次 Notification.FLAG_AUTO_CANCEL //用戶點擊通知後自動消失 Notification.FLAG_NO_CLEAR //只有全部清除時,Notification才會清除,不清除該通知(QQ的通知無法清除,使用這個) Notification.FLAG_FOREGROUND_SERVICE //
表示正在運行的服務

  2.屬性:Action[] actions
   功能:與NotificationCompat的addAction(int icon, CharSequence title, PendingIntent intent)方法功能相同。
   註意:要求API為19或者以上才能使用。
   例子:

Notification notification = mBuilder.build();
Notification.Action action = new Notification.Action(R.drawable.icon,"test action",actionPendingIntent);
Notification.Action[] actions = new Notification.Action[1];
actions[0] = action;
notification.actions = actions;

四、NotificationManager

狀態欄通知的管理類,負責發通知、清除通知等。

  1.方法:notify(int id, Notification notification)、notify (String tag, int id, Notification notification)
   功能:發送通知。
   例子:notify(1, mBuilder.build())。
  2.方法:cancel (int id)、cancel (String tag, int id)、cancelAll ()
   功能:取消通知。
   例子:cancel(1)。

五、NotificationCompat

  1.方法:setContentTitle(CharSequence title)
   功能:設置通知欄標題。
   例子:setContentTitle("測試標題")。
  2.方法:setContentText(CharSequence text)
   功能:設置通知欄顯示內容。
   例子:setContentText("測試內容")。
  3.方法:setContentIntent(PendingIntent intent)
   功能:設置通知欄點擊意圖。
   例子:setContentIntent(PendingIntent.getActivity(this, 1, new Intent(), flags))。
  4.方法:setTicker(CharSequence tickerText)、setTicker(CharSequence tickerText, RemoteViews views)
   功能:設置通知在第一次到達時在狀態欄中顯示的文本。
   註意:5.0及之後沒有效果。
   例子:setTicker("測試通知來啦")。
  5.方法:setWhen(long when)
   功能:通知產生的時間,會在通知欄信息裏顯示,一般是系統獲取到的時間。
   例子:setWhen(System.currentTimeMillis())。
  6.方法:setPriority(int pri)
   功能:設置通知優先級。
   例子:setPriority(Notification.PRIORITY_DEFAULT)。
   參數屬性:

Notification.PRIORITY_DEFAULT //默認優先級,用於沒有特殊優先級分類的通知
Notification.PRIORITY_HIGH //高優先級,用於重要的通信內容,例如短消息或者聊天
Notification.PRIORITY_LOW //低優先級,可以通知用戶但又不是很緊急的事件
Notification.PRIORITY_MAX //重要而緊急的通知,通知用戶這個事件是時間上緊迫的或者需要立即處理的
Notification.PRIORITY_MIN //用於後臺消息(例如天氣或者位置信息)。最低優先級通知將只在狀態欄顯示圖標,只有用戶下拉通知抽屜才能看到內容。

  7.方法:setNumber(int number)
   功能:設置通知集合的數量。
   例子:setNumber(10)。
  8.方法:setAutoCancel(boolean autoCancel)
   功能:true,當用戶點擊面板就可以讓通知自動取消。
   例子:setAutoCancel(true)。
  9.方法:setOngoing(boolean ongoing)
   功能:true,設置它為一個正在進行的通知,通常表示一個後臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,因此占用設備(如一個文件下載,同步操作,主動網絡連接)。
   例子:setOngoing(false)。
  10.方法:setDefaults(int defaults)
    功能:向通知添加聲音、閃燈和震動效果,最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,可以組合Notification.DEFAULT_ALL、Notification.DEFAULT_SOUND添加聲音。
    例子:setDefaults(Notification.DEFAULT_VIBRATE)。
    參數屬性:

Notification.DEFAULT_VISIBLE //添加默認震動提醒 需要VIBRATE permission
Notification.DEFAULT_SOUND //添加默認聲音提醒
Notification.DEFAULT_LIGHTS //添加默認三色燈提醒
Notification.DEFAULT_ALL //添加默認以上三種全部提醒

  11.方法:setSmallIcon(int icon)
   功能:設置通知的小Icon。
    例子:setSmallIcon(R.mipmap.ic_launcher)。
  12.方法:setCategory(String category)
    功能:設置通知類別。
    例子:setCategory(Notification.CATEGORY_MESSAGE)。
    參數屬性:

Notification.CATEGORY_CALL //呼入(語音或視頻)或類似的同步通信請求。
Notification.CATEGORY_MESSAGE //直接消息(短信、即時消息等)。
Notification.CATEGORY_EMAIL //異步批量消息(電子郵件)。
Notification.CATEGORY_EVENT //日歷事件。
Notification.CATEGORY_PROMO //促銷或廣告。
Notification.CATEGORY_ALARM //鬧鐘或定時器。
Notification.CATEGORY_PROGRESS //長時間後臺操作的進展。
Notification.CATEGORY_SOCIAL //社交網絡或共享更新。
Notification.CATEGORY_ERROR //後臺操作或者身份驗證狀態出錯。
Notification.CATEGORY_TRANSPORT //回放媒體傳輸控制。
Notification.CATEGORY_SYSTEM //系統或者設備狀態更新,預留給系統使用。
Notification.CATEGORY_SERVICE //運行後臺服務的指示。
Notification.CATEGORY_RECOMMENDATION //針對某一事物的具體及時的建議。
Notification.CATEGORY_STATUS //關於設備或者上下文狀態的正在進行的信息。
Notification.CATEGORY_REMINDER //用戶預定提醒。

  13.方法:setColor(@ColorInt int argb)
   功能:設置通知欄顏色。
   例子:setColor(0x667788)。
  14.方法:setContentInfo(CharSequence info)
   功能:在通知的右側設置大文本。
   例子:setContentInfo("大文本")。
  15.方法:setLocalOnly(boolean b)
   功能:設置此通知是否僅與當前設備相關,如果設置為true,通知就不能橋接到其他設備上進行遠程顯示。
   例子:setLocalOnly(true)。
  16.方法:setVibrate(long[] pattern)
   功能:設置使用震動模式。
   例子:setVibrate(new long[] {0,300,500,700})。延遲0秒,然後震動300ms,再延遲500ms,接著震動700ms。
  17.方法:setUsesChronometer(boolean b)
    功能:設置是否顯示時間計時,電話通知就會使用到。
    例子:setUsesChronometer(true)。
  18.方法:setRemoteInputHistory(CharSequence[] text)
    功能:設置遠程輸入歷史。
    例子:setRemoteInputHistory(new CharSequence[] {"1","2","3"})。
  19.方法:setOnlyAlertOnce(boolean onlyAlertOnce)
    功能:設置僅提醒一次。
    例子:setOnlyAlertOnce(true)。
  20.方法:setLights(@ColorInt int argb, int onMs, int offMs)
    功能:android支持三色燈提醒,這個方法就是設置不同場景下的不同顏色的燈。
    註意:(1)只有在設置了標誌符Flags為Notification.FLAG_SHOW_LIGHTS的時候,才支持三色燈提醒。
       (2)這邊的顏色跟設備有關,不是所有的顏色都可以,要看具體設備。
    例子:setLights(Oxff0000ff, 300, 0)

     其中argb表示燈光顏色,onMs表示亮持續時間,offMs表示暗的時間。
     還有一種寫法是:

Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_SHOW_LIGHTS;
notify.ledARGB = 0xff0000ff;
notify.ledOnMS = 300;
notify.ledOffMs = 300;

  21.方法:setSound(Uri sound)、setSound(Uri sound, int streamType)、setSound(Uri sound, AudioAttributes audioAttributes)
    功能:設置默認或者自定義的鈴聲來提醒。
    例子:setDefaults(Notification.DEFAULT_SOUND);//獲取默認鈴聲
       setSound(Uti.parse("file:///sdcard/xx/xx.mp3"));//獲取自定義鈴聲
        setSound(Uri.withAppendedPath(Audio.Media.INTERNAL_CONTENT_URL, "5"));//獲取Android多媒體庫內的鈴聲
      其中streamType指的鈴聲的使用流。
    參數屬性:

AudioManager.STREAM_MUSIC //用於音樂播放的音頻流
AudioManager.STREAM_RING //用於電話鈴聲的音頻流
AudioManager.STREAM_VOICE_CALL //用於電話呼叫的音頻流
AudioManager.STREAM_SYSTEM //用於系統聲音的音頻流
AudioManager.STREAM_ALARM //用於警報的音頻流
AudioManager.STREAM_NOTIFICATION //用於通知的音頻流
AudioManager.STREAM_BLUETOOTH_SCO //藍牙已經連接的電話呼叫的音頻流
AudioManager.STREAM_SYSTEM_ENFORCED //在特定國家強制系統聲音的音頻流(比如日本的照相機)
AudioManager.STREAM_DTMF //用於DTMF(雙音多頻電話)音調的音頻流
AudioManager.STREAM_TTS //TTS(文本到語音)的音頻流

  22.方法:setProgress(int max, int progress, boolean indeterminate)
    功能:設置帶進度條的通知,可以在下載中使用。
    註意:此方法在4.0及以後版本才有用,如果為早期版本,需要自定義通知布局,其中包含ProgressBar視圖。
    例子:setProgress(100, 66, true)。
       其中max表示精度條最大數值,progress表示當前進度,indeterminate表示進度是否不確定,true為不確定,false為確定。
       如果為確定的進度條,調用setProgress(max, progress, false)來設置通知,在更新進度的時候在此發起通知更新progress,並且在下載完成後要移除進度條,通過調用setProgress(0,0,false)即可。
       如果為不確定(持續活動)的進度條,這是在處理進度無法準確獲知時顯示活動正在持續,所以調用setProgress(0,0,true),操作結束時,調用setProgress(0,0,false)並更新通知以移除指示條。
  23.方法:build()
    功能:整合所有已設置的選項,返回一個新的Notification對象。
    例子:build()。
  24.方法:setStyle(Style style)
    功能:在構建應用時添加一個豐富的通知樣式。
     例子:setStyle(inboxStyle)
    參數屬性:

BigPictureStyle //用於生成包含大圖像附件的大格式通知的輔助類
BigTextStyle //用於生成包含大量文本的大格式通知的輔助類
MessagingStyle //用於生成大格式通知的輔助類,其中包含任意數量的不同類型的多個來回消息
InboxStyle //用於生成大格式通知的輔助類,其中包含一個(最多5個)字符串列表

  25.方法:addAction(int icon, CharSequence title, PendingIntent intent)、addAction(Notification.Action action)
    功能:向通知添加操作,操作通常與通知的content相連被系統作為按鈕來顯示。在系統的content下方顯示圖片與title,點擊這個圖片或者title就會觸發設置的intent。
    註意:Android 4.1版本及其以上才能使用,之下版本不能使用。
    例子:addAction(R.drawable.icon,"test action",actionPendingIntent)
  26.方法:setContent(RemoteViews views)
    功能:支持使用自定義視圖替代標準的視圖。
    例子:setContent(mRemoteViews)。
  27.方法:setLargeIcon(Bitmap icon)
    功能:設置下拉列表中的圖標(大圖標)
    例子:setLargeIcon(BitmapFactory.decodeResource(getResources(),R.drawable.icon))。
  28.方法:setPublicVersion(Notification n)
    功能:設置安全鎖屏下的通知
    例子:setPublicVersion(publicBuilder.build())。
  29.方法:setDeleteIntent(PendingIntent intent)
    功能:設置通知刪除時的意圖
    例子:setDeleteIntent(deletePendingIntent)。
  30.方法:setExtras(Bundle extras)
    功能:為通知設置數據。
    例子:setExtras(bundle),不太清楚主要用於做什麽。
  31.方法:setSubText(CharSequence text)
    功能:在通知模板上設置第三行文本。
    註意:如果使用了setProgress(),就不要使用setSubText()了,它們占據同一個位置。
       如果不提供大格式通知,則此方法沒有效果,第三行文本只出現在展開視圖中。
    例子:setSubText("sub text")。
  32.方法:setFullScreenIntent(PendingIntent intent, boolean highPriority)
    功能:只適用於具有極高優先級的通知,比如電話或者鬧鈴,如果設備被用用於其他東西,請給用戶一個選項關閉它並使用一個正常的通知,因為這個可能會有破壞性。
    例子:setFullScreenIntent(fullScreenIntent, true)。
  33.方法:setGroup(String groupKey)
    功能:設置該通知組的密鑰,即確認為哪一組。
    例子:setGroup(“test_notification”)。
  34.方法:setGroupSummary(boolean isGroupSummary)
    功能:設置是否為一組通知的第一個顯示。
    例子:setGroupSummary(true)。
  35.方法:setSortKey(String sortKey)
    功能:設置針對一個包內的通知進行排序的鍵值,
    例子:setSortKey("sort")

五、Notification的普通使用

  1.獲取狀態通知欄管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  2.實例化通知欄的Builder構造器

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  3.對Builder進行配置

mBuilder.setContentTitle("測試標題")//設置通知欄標題
.setContentText("測試內容")//設置通知欄顯示內容
.setTicker("測試通知來啦")//設置通知在第一次到達時在狀態欄中顯示的文本
.setWhen(System.currentTimeMillis())//通知產生的時間,會在通知欄信息裏顯示,一般是系統獲取到的時間
.setPriority(Notification.PRIORITY_DEFAULT)//設置通知優先級
.setNumber(10)//設置通知集合的數量
.setAutoCancel(true)//當用戶點擊面板就可以讓通知自動取消
.setOngoing(false)//true,設置它為一個正在進行的通知,通常表示一個後臺任務,用戶積極參與(如播放音樂)或以某種方式正在等待,因此占用設備(如一個文件下載,同步操作,主動網絡連接)。
.setDefaults(Notification.DEFAULT_VIBRATE)//向通知添加聲音、閃燈和振動效果的最簡單、最一致的方式是使用當前的用戶默認設置,使用defaults屬性,可以組合Notification.DEFAULT_ALL、 Notification.DEFAULT_SOUND添加聲音
.setSmallIcon(R.mipmap.ic_launcher)//設置通知小Icon
.setCategory(Notification.CATEGORY_MESSAGE)//設置通知類別
.setColor(0x0000ff)//設置通知欄顏色
.setContentInfo("大文本")//在通知的右側設置大文本
.setLocalOnly(true)//設置此通知是否僅與當前設備相關。如果設置為true,通知就不能橋接到其他設備上進行遠程顯示。
.setVibrate(new long[]{0, 300, 500, 700})//設置使用振動模式
.setUsesChronometer(true)//設置是否顯示時間計時,電話通知就會使用到
.setRemoteInputHistory(new CharSequence[]{"1", "2", "3"})//設置遠程輸入歷史
.setOnlyAlertOnce(true);//設置僅提醒一次。

  4.設置通知欄點擊事件

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent)//設置通知欄點擊意圖

  5.發通知

mNotifacationManager.notify(1, mBuilder.build());

六、自定義Notification樣式

  1.創建通知布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/custom_song_icon"
        android:layout_width="64dp"
        android:layout_height="64dp"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:src="@drawable/sing_icon" />

    <LinearLayout
        android:id="@+id/ll_custom_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dip"
        android:gravity="center_vertical"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/btn_custom_prev"
            style="@style/btn_custom_style"
            android:src="@drawable/btn_prev" />

        <ImageButton
            android:id="@+id/btn_custom_play"
            style="@style/btn_custom_style"
            android:contentDescription="播放"
            android:src="@drawable/btn_play" />

        <ImageButton
            android:id="@+id/btn_custom_next"
            style="@style/btn_custom_style"
            android:contentDescription="下一首"
            android:src="@drawable/btn_next" />
    </LinearLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:layout_toLeftOf="@id/ll_custom_button"
        android:layout_toRightOf="@id/custom_song_icon">

        <TextView
            android:id="@+id/tv_custom_song_singer"
            style="@style/NotificationTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:text="title"
            android:textSize="15sp" />

        <TextView
            android:id="@+id/tv_custom_song_name"
            style="@style/NotificationContent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:text="content"
            android:textSize="12sp" />
    </RelativeLayout>

  2.設置接受自定義界面按鈕點擊事件的廣播

   public class ButtonBroadcastReceiver extends BroadcastReceiver {

        public void onReceive(Context context, Intent intent) {
            Log.d(TAG, "onreceiver");
            String action = intent.getAction();
            if (action.equals(ACTION_BUTTON)) {
                int buttonId = intent.getIntExtra("ButtonId", 0);
                switch (buttonId) {
                    case 1:
                        Log.d(TAG, "上一首");
                        Toast.makeText(MainActivity.this, "上一首", Toast.LENGTH_SHORT).show();
                        break;
                    case 2:
                        String play_status = "";
                        isPlay = !isPlay;
                        if (isPlay) {
                            play_status = "開始播放";
                        } else {
                            play_status = "已暫停";
                        }
                        Log.d(TAG, "play_status = " + play_status);
                        showButonNotify();
                        Log.d(TAG, "showButonNotify");
                        Log.d(TAG, play_status);
                        Toast.makeText(MainActivity.this.getApplicationContext(), play_status, Toast.LENGTH_SHORT).show();
                        break;
                    case 3:
                        Log.d(TAG, "下一首");
                        Toast.makeText(MainActivity.this.getApplicationContext(), "下一首", Toast.LENGTH_SHORT).show();
                }
            }
        }
    }

  3.註冊廣播

public ButtonBroadcastReceiver mButtonReceicer;
mButtonReceicer = new ButtonBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(ACTION_BUTTON);
registerReceiver(mButtonReceicer, intentFilter);

  4.解綁廣播

    @Override
    protected void onDestroy() {
        if (mButtonReceicer != null) {
            unregisterReceiver(mButtonReceicer);
        }
        mNotifacationManager.cancel(2);//在應用退出的時候,關閉通知。
        super.onDestroy();
    }

  5.設置通知布局

RemoteViews mRemoteViews = new RemoteViews(getPackageName(), R.layout.view_custom_button);
        mRemoteViews.setImageViewResource(R.id.custom_song_icon, R.drawable.sing_icon);
        //API 3.0以上的時候顯示按鈕,否則消失
        mRemoteViews.setTextViewText(R.id.tv_custom_song_singer, "周傑倫");
        mRemoteViews.setTextViewText(R.id.tv_custom_song_name, "七裏香");
        //如果版本號低於3.0,那麽不顯示按鈕
        if (Build.VERSION.SDK_INT <= 9) {
            mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.GONE);
        } else {
            mRemoteViews.setViewVisibility(R.id.ll_custom_button, View.VISIBLE);
        }

        if (isPlay) {
            mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_pause);
        } else {
            mRemoteViews.setImageViewResource(R.id.btn_custom_play, R.drawable.btn_play);
        }

        //點擊的事件處理
        Intent buttonIntent = new Intent(ACTION_BUTTON);
        //上一首按鈕
        buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PREV_ID);
        //這裏加了廣播,所及INTENT的必須用getBroadcast方法
        PendingIntent intent_prev = PendingIntent.getBroadcast(this, 1, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_prev, intent_prev);
        //播放/暫停 按鈕
        buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_PALY_ID);
        PendingIntent intent_play = PendingIntent.getBroadcast(this, 2, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_play, intent_play);
        //下一首按鈕
        buttonIntent.putExtra(INTENT_BUTTONID_TAG, BUTTON_NEXT_ID);
        PendingIntent intent_next = PendingIntent.getBroadcast(this, 3, buttonIntent, PendingIntent.FLAG_UPDATE_CURRENT);
        mRemoteViews.setOnClickPendingIntent(R.id.btn_custom_next, intent_next);

  6.獲取狀態通知欄管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  7.實例化通知欄的Builder構造器

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  8.對Builder進行配置

        mBuilder.setContent(mRemoteViews)
                .setWhen(System.currentTimeMillis())
                .setTicker("正在播放")
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setOngoing(true)
                .setSmallIcon(R.drawable.sing_icon);

  9.設置通知欄點擊事件

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
mBuilder.setContentIntent(pendingIntent);

  10.發通知

Notification notify = mBuilder.build();
notify.flags = Notification.FLAG_ONGOING_EVENT;
mNotifacationManager.notify(2, notify);

七、設置通知樣式

  1.創建BigPictureStyle樣式

NotificationCompat.BigPictureStyle inboxStyle = new NotificationCompat.BigPictureStyle();
inboxStyle.setBigContentTitle("大視圖內容");
inboxStyle.bigPicture(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher));
inboxStyle.bigLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher_round));

  2.獲取狀態通知欄管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  3.實例化通知欄的Builder構造器

NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this);

  4.對Builder進行配置

        Intent intent = new Intent(this, MainActivity.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
        
        mBuilder.setContentIntent(pendingIntent)
                .setWhen(System.currentTimeMillis())
                .setTicker("大圖測試")
                .setStyle(inboxStyle)
                .setPriority(Notification.PRIORITY_DEFAULT)
                .setNumber(10)
                .setContentTitle("大圖風格")
                .setSmallIcon(R.drawable.icon)
                .setContentText("大圖");

  6.發通知

mNotifacationManager.notify(3, mBuilder.build());

八、設置分組通知  

  1.獲取狀態通知欄管理

NotificationManager mNotifacationManager;
mNotifacationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

  2.實例化多個通知欄的Builder構造器

NotificationCompat.Builder mBuilder1 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder2 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder3 = new NotificationCompat.Builder(this);
NotificationCompat.Builder mBuilder4 = new NotificationCompat.Builder(this);

  3.對多個Builder進行配置

Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);

mBuilder1.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("first ticker")
         .setContentTitle("first content title")
         .setContentText("first content text")
         .setContentInfo("first content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("a")
         .setGroupSummary(true)//作為這組信息的第一個顯示
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

mBuilder2.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("second ticker")
         .setContentTitle("second content title")
         .setContentText("second content text")
         .setContentInfo("second content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("a")
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

mBuilder3.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("third ticker")
         .setContentTitle("third content title")
         .setContentText("third content text")
         .setContentInfo("third content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("b")
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

mBuilder4.setContentIntent(pendingIntent)
         .setWhen(System.currentTimeMillis())
         .setTicker("forth ticker")
         .setContentTitle("forth content title")
         .setContentText("forth content text")
         .setContentInfo("forth content info")
         .setSmallIcon(R.mipmap.ic_launcher)
         .setGroup("b")
         .setGroupSummary(true)
         .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon));

  4.發通知

mNotifacationManager.notify(4, mBuilder1.build());
mNotifacationManager.notify(5, mBuilder2.build());
mNotifacationManager.notify(6, mBuilder3.build());
mNotifacationManager.notify(7, mBuilder4.build());

代碼地址:https://github.com/ZhangMiao147/NotificationDemo

參考文章
http://blog.csdn.net/aqi00/article/details/50540837
http://blog.csdn.net/vipzjyno1/article/details/25248021/
http://iluhcm.com/2017/03/12/experience-of-adapting-to-android-notifications/index.html#RemoteViews適配

通知欄Notification的整理