天天看點

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

想要看全部設定的請看這一篇 【轉】NotificationCopat.Builder全部設定

常用設定:

設定屬性 說明
setAutoCancel(boolean autocancel) 設定點選資訊後自動清除通知
setContent(RemoteView view) 設定自定義通知
setContentTitle(String string) 設定标題
setContentText(String string) 設定内容
SetContentIntent(PendingIntent intent) 設定點選資訊後的跳轉(意圖)
setWhen(long when) 設定時間
setPriority(int pri) 設定通知的重要程度
setStyle(Style style) 設定樣式
setVisibility(int visibility) 設定鎖屏顯示
setDefault(int defaults) 設定預設
setLight(int argb, int onMs, int offMs) 設定呼吸燈閃爍效果
setSound(Uri sound) 設定通知音效
setVibrate(long [] pattern) 設定震動效果
setCategory(String category) 設定通知類别
setColor(int argb) 設定通知欄顔色
setFullScreenIntent(PendingIntent intent,boolean b) 設定彈窗顯示

簡單步驟說明:

建立一個notification,需要NotificationManager(這是一個系統的服務類,由名字很容易知道是用來管理Notification通知類的)和Notification(通知類)

Notification建立類似Dialog的建立,通過Notification類中提供的各種方法來設定屬性,最後build方法生成

NotificationManger的執行個體可以通過getSystemService這個方法獲得

PendingIntent與Intent有關系,這是一個延遲意圖,可以通過調用getActivity,getBroadcastReceiver,getService三個方法獲得執行個體

普通使用:

NotificationCompat.Builder自動設定的預設值:

    priority: PRIORITY_DEFAULT //通知的重要程度

    when: System.currentTimeMillis() //時間

    audio stream: STREAM_DEFAULT //音效 系統預設音效

上面的這三個我們一般情況下可以不用設定,系統會自動幫我們設定

下面就是一個簡單的使用,設定了标題,内容,小圖示,點選通知欄的該資訊會跳轉到main2Activity中,具體可以看注釋,之後由這個普通使用的通知作為基礎講解NotificationCopat.Builder中的其他設定

Intent  intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("this is cotenttitle")//設定标題,必要
        .setContentText("contentText")//設定内容,必要
        .setWhen(System.currentTimeMillis())//設定時間,預設設定,可以忽略
        .setSmallIcon(R.mipmap.ic_launcher)//設定通知欄的小圖示,必須設定
        .setAutoCancel(true)//設定自動删除,點選通知欄資訊之後系統會自動将狀态欄的通知删除,要與setContentIntent連用
        .setContentIntent(pendingIntent)//設定在通知欄中點選該資訊之後的跳轉,參數是一個pendingIntent
     .build();
     manger.notify(1,notification);//id為1      
Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

setLargeIcon

Intent  intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("this is cotenttitle")//設定标題,必要
        .setContentText("contentText")//設定内容,必要
        .setWhen(System.currentTimeMillis())//設定時間,預設設定,可以忽略
        .setSmallIcon(R.mipmap.ic_launcher)//設定通知欄的小圖示,必須設定
        .setAutoCancel(true)//設定自動删除,點選通知欄資訊之後系統會自動将狀态欄的通知删除
        .setContentIntent(pendingIntent)//設定在通知欄中點選該資訊之後的跳轉,參數是一個pendingIntent
     .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
     //設定大圖示,未設定時使用小圖示代替,拉下通知欄顯示的那個圖示
     //設定大圖檔 BitmpFactory.decodeResource(Resource res,int id) 根據給定的資源Id解析成位圖
     .build();
     manger.notify(1,notification);      
Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

setPriority實作彈窗資訊

Intent  intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("this is cotenttitle")//設定标題,必要
        .setContentText("contentText")//設定内容,必要
        .setWhen(System.currentTimeMillis())//設定時間,預設設定,可以忽略
        .setSmallIcon(R.mipmap.ic_launcher)//設定通知欄的小圖示,必須設定
        .setAutoCancel(true)//設定自動删除,點選通知欄資訊之後系統會自動将狀态欄的通知删除
        .setContentIntent(pendingIntent)//設定在通知欄中點選該資訊之後的跳轉,參數是一個pendingIntent
     .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
     //設定大圖示,未設定時使用小圖示代替,拉下通知欄顯示的那個圖示
     //設定大圖檔 BitmpFactory.decodeResource(Resource res,int id) 根據給定的資源Id解析成位圖
     .setPriority(NotificationCompat.PRIORITY_MAX) //設定為最高重要程度
     .setDefaults(NotificationCompat.DEFAULT_SOUND)//設定音效為預設
     .build();
     manger.notify(1,notification);      

由于隻有一條資訊,我們可能看不出最高重要程度的差別,但是,我們如果再設定音效或者震動,那麼這條通知就會彈窗顯示

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

實作彈窗顯示的兩種方法:一,當重要程度為最高,且設定了音效或者是震動 ;二,調用setFullScreenIntent()

setFullScreenIntent

Intent  intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("this is cotenttitle")//設定标題,必要
        .setContentText("contentText")//設定内容,必要
        .setWhen(System.currentTimeMillis())//設定時間,預設設定,可以忽略
        .setSmallIcon(R.mipmap.ic_launcher)//設定通知欄的小圖示,必須設定
        .setAutoCancel(true)//設定自動删除,點選通知欄資訊之後系統會自動将狀态欄的通知删除
        .setContentIntent(pendingIntent)//設定在通知欄中點選該資訊之後的跳轉,參數是一個pendingIntent
     .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
     //設定大圖示,未設定時使用小圖示代替,拉下通知欄顯示的那個圖示
     //設定大圖檔 BitmpFactory.decodeResource(Resource res,int id) 根據給定的資源Id解析成位圖
      
     .setFullScreenIntent(pendingIntent,false)
     .build();
     manger.notify(1,notification);      

 圖檔與之前那張一樣,我就不放出來了,值得說的是這個方法的第二個參數,為什麼是false呢,如果是true的話,當你在玩遊戲的時候,螢幕是全屏,然後這條通知就不需要點選,直接就會跳轉到某個界面去,除非是來電了才會用true,其他情況若是使用的話,使用者估計直接解除安裝APP

setDefault

之前也是使用了這個設定與setPriority連用實作了彈窗消息,簡單解釋,這個可以設定音效,震動和呼吸燈的預設效果,也可以單獨設定某個的預設,之前我就是單獨設定了音效的預設

有四個參數可以選擇,由英文也可以看出是什麼意思了吧,這裡就不多解釋了

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

setStyle

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

      這裡我就簡單地說兩種,長文本顯示和大圖檔顯示,更多的請看這一篇Android開發——Notification通知的各種Style詳解

  一、BigTextStyle   長文本顯示

Intent  intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("this is cotenttitle")//設定标題,必要
        .setContentText("contentText")//設定内容,必要
        .setWhen(System.currentTimeMillis())//設定時間,預設設定,可以忽略
        .setSmallIcon(R.mipmap.ic_launcher)//設定通知欄的小圖示,必須設定
        .setAutoCancel(true)//設定自動删除,點選通知欄資訊之後系統會自動将狀态欄的通知删除
        .setContentIntent(pendingIntent)//設定在通知欄中點選該資訊之後的跳轉,參數是一個pendingIntent
     .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
     //設定大圖示,未設定時使用小圖示代替,拉下通知欄顯示的那個圖示
     //設定大圖檔 BitmpFactory.decodeResource(Resource res,int id) 根據給定的資源Id解析成位
      
     .setStyle(new NotificationCompat.BigTextStyle().bigText("長内容長内容長内容長内容長内容長内容長内容長内容長内容長内容長
内容長内容長内容長内容長内容長内容長内容長内容長内容長内容"))
     .build();
     manger.notify(1,notification);      
Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

   BigPictureStyle 大圖檔顯示

Intent  intent = new Intent(this,Main2Activity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this,0,intent,0);
NotificationManager manger = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new NotificationCompat.Builder(this)
        .setContentTitle("this is cotenttitle")//設定标題,必要
        .setContentText("contentText")//設定内容,必要
        .setWhen(System.currentTimeMillis())//設定時間,預設設定,可以忽略
        .setSmallIcon(R.mipmap.ic_launcher)//設定通知欄的小圖示,必須設定
        .setAutoCancel(true)//設定自動删除,點選通知欄資訊之後系統會自動将狀态欄的通知删除
        .setContentIntent(pendingIntent)//設定在通知欄中點選該資訊之後的跳轉,參數是一個pendingIntent
     .setLargeIcon(BitmapFactory.decodeResource(getResources(),R.mipmap.ic_launcher))
     //設定大圖示,未設定時使用小圖示代替,拉下通知欄顯示的那個圖示
     //設定大圖檔 BitmpFactory.decodeResource(Resource res,int id) 根據給定的資源Id解析成位
      
     .setStyle(new NotificationCompat.BigPictureStyle().bigPicture(BitmapFactory.decodeResource(getResources(),R.drawable.index)))
     .build();
     manger.notify(1,notification);      
Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

 setContent

  參數是一個RemoteView,由名字大概可以知道是一個View,定義一個布局檔案,之後與其綁定,之後,為布局中的按鈕設定onClick事件,之後調用setContent,remoteView作為參數傳入

           RemoteViews remoteView = new RemoteViews(getPackageName(),R.layout.notification);
               remoteView.setOnClickPendingIntent(R.id.last_btn,pendingIntent);      

個人覺得自定義布局可以做成像音樂播放器的通知欄那樣,不過,今天就暫時簡單的學習,之後有學到再補充

提問之前,請先看提問須知

點選右側圖示發起提問

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

或者加入QQ群一起學習

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

TornadoFx學習交流群:1071184701

Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API
Android開發——Notification通知的使用及NotificationCopat.Builder常用設定API

繼續閱讀