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

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);

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);
 

由於只有一條信息,我們可能看不出最高重要程度的區別,但是,我們如果再設置音效或者震動,那么這條通知就會彈窗顯示

實現彈窗顯示的兩種方法:一,當重要程度為最高,且設置了音效或者是震動 ;二,調用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連用實現了彈窗消息,簡單解釋,這個可以設置音效,震動和呼吸燈的默認效果,也可以單獨設置某個的默認,之前我就是單獨設置了音效的默認

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

setStyle

      這里我就簡單地說兩種,長文本顯示和大圖片顯示,更多的請看這一篇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);
 

   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);

 

 

 setContent

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

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

 

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM