使用通知的基本步驟:
1、首先我們需要一個(gè)NotificationManager類(lèi)來(lái)管理通知,可以通過(guò)getSystemService()方法獲得該對(duì)象,getSystemService()接受一個(gè)參數(shù),用來(lái)確認(rèn)獲取系統(tǒng)的哪個(gè)服務(wù),在這里我們只需要傳入Context.NOTIFICATION_SERVICE
NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICER);
2、在獲得NotificationManager對(duì)象后,接下來(lái)使用Builder構(gòu)造器來(lái)創(chuàng)建一個(gè)Notification對(duì)象:
Notification notification=new NotificationCompat.Builder(context)
//設(shè)置通知的標(biāo)題
.setContentTitle("xxx")
設(shè)置通知的內(nèi)容
.setContentText("xxx")
//指定通知被創(chuàng)建的時(shí)間,以毫秒為單位
.setContentWhen(System.currentTimeMills)
//設(shè)置通知的小圖標(biāo)
.setSmallIcon(R.drawable.small_icon)
//設(shè)置通知的大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResource(),R.drawable.large_icon))
.build();
3、完成以上2步操作后,直接使用NotificationManager.notify():
notificationManager.notifity(id,notification);
第一個(gè)參數(shù):第一個(gè)參數(shù)位ID,我們要保證每個(gè)通知的的ID都是不同的。
第二個(gè)參數(shù):構(gòu)建好的notification對(duì)象
通過(guò)使用NotificationManager對(duì)象調(diào)用notifity(id,nitification)方法后,我們可以看到看到通知的效果,那么The question coming 哈哈哈,當(dāng)你試著去點(diǎn)擊這個(gè)通知的時(shí)候你會(huì)發(fā)現(xiàn)他是沒(méi)有任何反應(yīng)的。要想讓我們的通知能在用戶點(diǎn)擊的時(shí)候作一些我們想要的事情,這個(gè)時(shí)候要使用什么,大聲的喊出來(lái)~~~意圖君(Intent)!
二、PendingIntent
Intent是我們?cè)贏ndroid中想讓代碼做任何東西第一個(gè)想到的一個(gè)對(duì)象,那么我們這里的通知是不是也使用Intent對(duì)象來(lái)為我們自己搞事情呢,現(xiàn)實(shí)并不是而是通過(guò)Pendingntent,看名字就知道和Intent有點(diǎn)像。先說(shuō)一下兩者的區(qū)別,Intent一般是指立即執(zhí)行的一個(gè)動(dòng)作,而PendingIntent則是指在合適的時(shí)間去執(zhí)行一個(gè)動(dòng)作,我們的通知希望用戶在點(diǎn)擊了通知的情況下才會(huì)去執(zhí)行這個(gè)動(dòng)作,簡(jiǎn)單的說(shuō)就是可以把pendingIntent理解為延遲的Intent。
在說(shuō)明PendingIntent之前,必須交代以下通知應(yīng)用的場(chǎng)景,一般情況下,我們會(huì)在應(yīng)用沒(méi)有處于當(dāng)前顯示的情況下才會(huì)通過(guò)通知提示用戶,因?yàn)闆](méi)有人會(huì)傻乎乎在自己的App內(nèi)通過(guò)一個(gè)通知告訴用戶他登錄成功了。理解了應(yīng)用場(chǎng)景后,我們來(lái)講解一下如何獲取PendingIntent對(duì)象,PendingIntent中封裝了3個(gè)靜態(tài)方法,我們可以通過(guò)以下方式獲得PendingIntent對(duì)象:
//這個(gè)獲取PendingIntent的方法是在Activcity中使用的(如果你真得6到在自己Acticvity使//用通知的話)
a、PendingIntent pendingIntent=PendingIntent.getActivity(context,0,intent,0),
//這個(gè)方式是我們?cè)趶V播接受
b、PendingIntent pendingIntent=PendingIntent.getBroadcast(context,0,intent,0);
//在服務(wù)中想要獲得PendingIntent需要使用該方法
c、PendingIntent pendingIntent=PendingIntent.getService(context,0,intent,0);
三個(gè)靜態(tài)方法的參數(shù)都是一樣的,分別介紹一下4個(gè)參數(shù):
context:上下文對(duì)象
第二個(gè)參數(shù)一般用不上,我也不知道具體有什么用,一般直接傳入0就可以了,有知道大神請(qǐng)告訴我哈
intent:意圖對(duì)象,這個(gè)相信不需要的去講解大家都也知道了
第四個(gè)參數(shù)用于確定PendingIntent的行為,有FLAG_ONE_SHOT,F(xiàn)LAG_NO_CREATE,FLAG_CANCELCURRENT,FLAG_UPDATE_CURRENT.一般情況下填入0即可。
然后在構(gòu)造notification對(duì)象的時(shí)候調(diào)用setContentIntent(pendingIntent)參數(shù)為我們創(chuàng)建的PendingIntent對(duì)象,點(diǎn)擊事件就是我們PendingIntent對(duì)象中參數(shù)Intent對(duì)象。
設(shè)置了點(diǎn)擊觸發(fā)事件后,點(diǎn)擊后發(fā)現(xiàn)通知并沒(méi)有關(guān)閉,我們可以使用
Notification notification=new NotificationCompat.Builder(this)
...
.setAutoCancel(true)
.build();
//設(shè)置通知鈴聲
Notification notification=new NotificationCompat.Builder(this)
...
.setSound(Uri.fromFile(new File("音頻存放位置")))
.build();
//設(shè)置手機(jī)振動(dòng)
Notification notification=new NotificationCompat.Builder(this)
...
.setVibrate(new long[]{0,1000,1000,100})
.build();
//設(shè)置提示LED燈亮起
Notification notification=new NotificationCompat.Builder(this)
...
.setLights(Color.GREEN,1000,1000)
.build();
//如果你什么都不想設(shè)置的話,還有一個(gè)更加簡(jiǎn)單粗暴的方法
Notification notification=new NotificationCompat.Builder(this)
...
.setDefaults(NotificationCompat.DEFAULT_ALL)
.build();
這里根據(jù)當(dāng)前手機(jī)環(huán)境去決定播放什么鈴聲,如何振動(dòng)以及LED燈光閃爍的時(shí)間以及顏色
三、通知的高級(jí)功能
可以在notification中調(diào)用.setPriority(NotificationCompat.PRIORITY_MAX)設(shè)置notification對(duì)象的優(yōu)先級(jí)
Notification notification=new NotificationCompat.Builder(this)
...
.setPriority(NotificationCompat.PRIORITY_MAX)
.build();
通知的優(yōu)先級(jí)一共有五個(gè):
最低優(yōu)先級(jí):PRIORITY_MIN
低優(yōu)先級(jí):PRIORITY_LOW
默認(rèn)優(yōu)先級(jí):PRIORITY_DEFAULT
高優(yōu)先級(jí):PRIORITY_HIGH
最高優(yōu)先級(jí):PRIORITY_MAX
使用setStyle()讓我們的通知變得多樣化,比如通知內(nèi)容長(zhǎng)內(nèi)容,通知附加大圖片等等
//使用setStyle()讓我們通知內(nèi)容為長(zhǎng)文字
Notification notification=new NotificationCompat.Builder(this)
...
.setStyle(new NotificationCompat.BigTextStyle().bigText("xxx"))
.build();
//使用setStyle讓我們的通知內(nèi)容為大圖片
Notification notification=new NotificationCompat.Builder(this)
...
.setStyle(new NotificationCompat.BigPictrueStyle().bigPictrue(BitmapFactory.decodeResource(getResources(),R.drawable.big_image)))
.build();