段子
經(jīng)常熬夜有三大害處:第一交掏,記憶力越來越差枫浙;第二刨肃,數(shù)學(xué)水平下降;第四箩帚,記憶力越來越差真友。
前言
Notification在Android中使用的還是挺多的,我們公司的項(xiàng)目基本都用到了紧帕。這篇文章依然保持我的風(fēng)格盔然,用寫Demo的方式來學(xué)習(xí),這樣記憶最深是嗜。
Demo
我們寫一個(gè)小Demo愈案,把各種類型的Notification全部展示一遍,首先看下demo截圖
簡單看下鹅搪,大概會(huì)有這么二十多種寫法的Notification站绪,下面一個(gè)一個(gè)來看。
普通通知
Android3.0是一個(gè)分水嶺丽柿,在其之前構(gòu)建Notification推薦使用NotificationCompate.Builder恢准,它位于android.support.v4.app.NotificationCompat.Builder魂挂,是一個(gè)Android向下版本的兼容包,而在Android3.0之后馁筐,一般推薦使用Notification.Builder構(gòu)建涂召。本博客主要介紹的是Android4.x的開發(fā),所以在這里使用Notification.Builder進(jìn)行講解演示敏沉。
通知一般通過NotificationManager服務(wù)發(fā)送一個(gè)Notification對(duì)象來完成通知果正,NotificationManager是一個(gè)重要的系統(tǒng)級(jí)服務(wù),該對(duì)象位于應(yīng)用程序的框架層中盟迟,應(yīng)用程序可以通過它向系統(tǒng)發(fā)送全局的通知舱卡。使用通知的時(shí)候,需要?jiǎng)?chuàng)建一個(gè)Notification對(duì)象用來承載通知的內(nèi)容队萤,但是一般不會(huì)直接通過Notification的構(gòu)造方法來得到對(duì)象轮锥,而是使用它的內(nèi)部類Notification.Builder來實(shí)例化一個(gè)Builder對(duì)象,并設(shè)置通知的各項(xiàng)屬性要尔,最后通過Notification.Builder.builder()方法得到一個(gè)Notification對(duì)象舍杜,當(dāng)獲得這個(gè)Notification對(duì)象之后,就可以使用NotificationManager.notify()方法發(fā)送通知赵辕。
NotificationManager類是一個(gè)通知管理器類既绩,這個(gè)對(duì)象是由系統(tǒng)維護(hù)的服務(wù),是以單例模式的方式獲得还惠,所以一般并不直接實(shí)例化這個(gè)對(duì)象饲握。在Activity中,可以使用Activity.getSystemService(String)方法獲取NotificationManager對(duì)象蚕键,Activity.getSystemService(String)方法可以通過Android系統(tǒng)級(jí)服務(wù)的句柄救欧,返回對(duì)應(yīng)的對(duì)象。在這里需要返回NotificationManager锣光,所以直接傳遞Context.NOTIFICATION_SERVICE即可笆怠。
雖然通知中提供了各種屬性的設(shè)置,但是一個(gè)通知對(duì)象誊爹,有幾個(gè)屬性是必須要設(shè)置的蹬刷,其他的屬性均是可選的,必須設(shè)置的屬性如下:
- 小圖標(biāo)频丘,使用setSamllIcon()方法設(shè)置办成。
- 標(biāo)題,使用setContentTitle()方法設(shè)置搂漠。
- 文本內(nèi)容迂卢,使用setContentText()方法設(shè)置。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setContentTitle("普通通知")
.setContentText("這是一條普通通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
NOTIFICATION_ID是一個(gè)自己定義值,一個(gè)id表示一個(gè)notification冷守,如果兩次發(fā)出的notification是相同的id,那就會(huì)更新之前的那一個(gè)惊科,這是id的用處之一悲敷。id另外一個(gè)用處就是用于移除notification较沪。
mNotificationManager.cancel(NOTIFICATION_ID);
當(dāng)然除了可以用id移除notification以外,還可以通過Tag移除
mNotificationManager.cancel("tag", NOTIFICATION_ID);
或者一次移除所有notification
mNotificationManager.cancelAll();
設(shè)置屬性
最普通的notification肯定是沒法滿足我們的,因?yàn)榧葲]有點(diǎn)擊效果决采,展示信息也很少,所以我們要看看notification給我們提供了哪些api可以設(shè)置參數(shù)磅废。
大小圖標(biāo)
setSmallIcon(R.drawable.icon_small)
setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
當(dāng) setSmallIcon() 與 setLargeIcon() 同時(shí)存在時(shí), smallIcon 顯示在通知的右下角, largeIcon 顯示在左側(cè)题翰;當(dāng)只設(shè)置 setSmallIcon() 時(shí), smallIcon 顯示在左側(cè)〗颜牛看下圖你就明白了幕随。對(duì)于部分 ROM ,可能修改過源碼宿接,如 MIUI 上通知的大圖標(biāo)和小圖標(biāo)是沒有區(qū)別的赘淮。
Ticker提示語句
setTicker("來了一條設(shè)置屬性通知")
在來一條notification時(shí),默認(rèn)情況通知欄上會(huì)顯示一個(gè)小icon睦霎,但是不是很顯眼梢卸,并且不下拉通知欄就不知道具體是來了什么消息,Ticker就可以顯示一句提示語句副女。
設(shè)置時(shí)間
這個(gè)可設(shè)可不設(shè)蛤高,默認(rèn)也會(huì)取系統(tǒng)時(shí)間
setWhen(System.currentTimeMillis())
點(diǎn)擊自動(dòng)移除
這個(gè)最好還是設(shè)置成true,我自己測(cè)試的手機(jī)默認(rèn)是不會(huì)自動(dòng)移除的碑幅,一條notification被點(diǎn)擊了戴陡,我們就默認(rèn)為查看了,就不應(yīng)該再顯示沟涨。
setAutoCancel(true)
設(shè)置數(shù)量
就是右下角顯示的數(shù)字
setNumber(23)
設(shè)置數(shù)據(jù)
這個(gè)數(shù)據(jù)主要是攜帶給跳轉(zhuǎn)Activity用的猜欺,比如我們推送了一部小說,攜帶了小說id等重要信息拷窜,用戶點(diǎn)擊后可以跳轉(zhuǎn)對(duì)應(yīng)的小說詳情去开皿。
setExtras(new Bundle())
設(shè)置跳轉(zhuǎn)
這個(gè)是最重要的屬性了,沒有這個(gè)屬性篮昧,通知基本就沒什么意思了赋荆。
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
setContentIntent(pendingIntent)
flag參數(shù)有四個(gè)取值
- FLAG_CANCEL_CURRENT:如果構(gòu)建的PendingIntent已經(jīng)存在,則取消前一個(gè)懊昨,重新構(gòu)建一個(gè)窄潭。
- FLAG_NO_CREATE:如果前一個(gè)PendingIntent已經(jīng)不存在了,將不再構(gòu)建它酵颁。
- FLAG_ONE_SHOT:表明這里構(gòu)建的PendingIntent只能使用一次嫉你。
- FLAG_UPDATE_CURRENT:如果構(gòu)建的PendingIntent已經(jīng)存在月帝,則替換它,常用幽污。
最后看下全部的代碼
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setTicker("來了一條設(shè)置屬性通知")
.setContentIntent(pendingIntent)
.setWhen(System.currentTimeMillis())
.setNumber(23)
.setAutoCancel(true)
.setExtras(new Bundle())
.setContentTitle("設(shè)置屬性通知")
.setContentText("這是一條設(shè)置屬性通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
大圖模式
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.head_image);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setStyle(new Notification.BigPictureStyle().bigPicture(bitmap))
.setContentTitle("大圖模式通知")
.setContentText("這是一條大圖模式通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
文本段模式
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setNumber(12)
.setStyle(new Notification.BigTextStyle()
.setBigContentTitle("這是一段長文本")
.setSummaryText("這是總結(jié)")
.bigText("打南邊來了個(gè)啞巴嚷辅,腰里別了個(gè)喇叭;打北邊來了個(gè)喇嘛距误,手里提了個(gè)獺犸簸搞。提著獺犸的喇嘛要拿獺犸換別著喇叭的啞巴的喇叭;別著喇叭的啞巴不愿拿喇叭換提著獺犸的喇嘛的獺犸准潭。不知是別著喇叭的啞巴打了提著獺犸的喇嘛一喇叭趁俊;還是提著獺犸的喇嘛打了別著喇叭的啞巴一獺犸。喇嘛回家燉獺犸刑然;啞巴嘀嘀噠噠吹喇叭"))
.setContentTitle("長文本通知")
.setContentText("這是一條長文本通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
文本塊模式
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setNumber(12)
.setStyle(new Notification.InboxStyle()
.setBigContentTitle("這是一個(gè)文本段")
.setSummaryText("這是總結(jié)")
.addLine("文本行111")
.addLine("文本行222")
.addLine("文本行333")
.addLine("文本行444")
.addLine("文本行555")
.addLine("文本行666"))
.setContentTitle("設(shè)置屬性通知")
.setContentText("這是一條文本段通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
精確進(jìn)度條
這個(gè)用的多一些寺擂,展示精確的進(jìn)度值,setProcess()方法第一個(gè)參數(shù)是最大進(jìn)度值泼掠,第二個(gè)參數(shù)是當(dāng)前進(jìn)度值沽讹,步長是1,第三個(gè)參數(shù)false表示的就是精確進(jìn)度條武鲁,true表示的是模糊進(jìn)度條爽雄。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder mProgressBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setProgress(100, 0, false)
.setContentTitle("進(jìn)度條通知")
.setContentText("這是一條進(jìn)度條通知");
new Thread() {
@Override
public void run() {
for (int i = 0; i < 100; i++) {
mProgressBuilder.setProgress(100, i, false);
mNotificationManager.notify(NOTIFICATION_ID, mProgressBuilder.build());
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
模糊進(jìn)度條
第三個(gè)參數(shù)false表示的就是精確進(jìn)度條,true表示的是模糊進(jìn)度條沐鼠。如果是模糊進(jìn)度條挚瘟,那就不存在最大值和當(dāng)前值,所以前兩個(gè)參數(shù)傳0即可饲梭。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setProgress(0, 0, true)
.setContentTitle("模糊進(jìn)度條通知")
.setContentText("這是一條模糊進(jìn)度條通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
震動(dòng)乘盖、音效、呼吸燈
這個(gè)很簡單憔涉,Android提供了三種效果:震動(dòng)订框、音效、呼吸燈兜叨,以及它們?nèi)叩娜我饨M合穿扳。
// 默認(rèn)震動(dòng)
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setDefaults(Notification.DEFAULT_VIBRATE)
.setContentTitle("震動(dòng)通知")
.setContentText("這是一條震動(dòng)通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
// 默認(rèn)音效
setDefaults(Notification.DEFAULT_SOUND)
// 默認(rèn)呼吸燈
setDefaults(Notification.DEFAULT_LIGHTS)
沒有圖,這三個(gè)效果真沒法配圖国旷。矛物。。
還可以選擇全效果跪但,震動(dòng)+音效+呼吸燈
setDefaults(Notification.DEFAULT_ALL)
自定義音效
我寫demo的這個(gè)音效來自騰訊履羞,我有一次在港式餐廳里喝咖啡,正好旁邊一個(gè)騰訊的工程師,聊到我最近要寫一個(gè)Notification的博客忆首,缺少一個(gè)音效文件爱榔,他就推薦了我這個(gè),在此非常感謝騰訊的這位朋友糙及。
好了详幽,不裝逼了。丁鹉。。是我從QQ apk里解壓出來的悴能。揣钦。。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setSound(Uri.parse("android.resource://com.makeunion.notificationdemo/" + R.raw.qq))
.setContentTitle("自定義音效通知")
.setContentText("這是一條自定義音效通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
從代碼中應(yīng)該也能看出來漠酿,音效文件是放在raw目錄下的冯凹。
自定義震動(dòng)
這里我們需要傳一個(gè)long[]數(shù)組參數(shù),這個(gè)參數(shù)真的是日了夠了炒嘲,我看到的解釋是這樣的:
第一個(gè)0表示手機(jī)靜止的時(shí)長
第二個(gè)300表示手機(jī)震動(dòng)的時(shí)長
第三個(gè)300表示手機(jī)靜止的時(shí)長
第四個(gè)300表示手機(jī)震動(dòng)的時(shí)長
所以這里表示手機(jī)先震動(dòng)300毫秒宇姚,然后靜止300毫秒,然后再震動(dòng)300毫秒
但是夫凸,我實(shí)際的效果總是有一點(diǎn)出入浑劳,我自己猜想應(yīng)該是notification總的提示時(shí)間是有限制的,所以會(huì)對(duì)最終效果有影響夭拌。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
long[] vibrate = new long[]{0, 300, 300, 300};
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setVibrate(vibrate)
.setContentTitle("自定義震動(dòng)通知")
.setContentText("這是一條自定義震動(dòng)通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
自定義呼吸燈
這個(gè)呼吸燈也是日了夠了魔熏,完全看不到效果,我不知道是我用的不對(duì)鸽扁,還是現(xiàn)在Android手機(jī)都閹割了這個(gè)功能蒜绽。。桶现。我覺得第二種可能性大些躲雅。
第一個(gè)參數(shù)表示的是顏色
第二個(gè)參數(shù)表示的是燈亮的時(shí)間
第三個(gè)參數(shù)表示的是燈滅的時(shí)間
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setLights(0xFF0000, 1000, 500)
.setContentTitle("自定義呼吸燈通知")
.setContentText("這是一條自定義呼吸燈通知").build();
notification.flags = Notification.FLAG_SHOW_LIGHTS;
mNotificationManager.notify(NOTIFICATION_ID, notification);
只提示一次
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setSound(Uri.parse("android.resource://com.makeunion.notificationdemo/" + R.raw.qq))
.setContentTitle("無限循環(huán)通知")
.setContentText("這是一條無限循環(huán)通知").build();
notification.flags |= Notification.FLAG_INSISTENT;
mNotificationManager.notify(NOTIFICATION_ID, notification);
慎用!B夂汀相赁!你會(huì)聽到一段瘋狂的提示音。
也可以設(shè)置成只提示一次
notification.flags |= Notification.FLAG_ONLY_ALERT_ONCE;
頂部懸浮
API21之后慰于,Android提供了頂部懸浮顯示Notification噪生,這種顯示方式的好處是更加顯眼,可以直接看到通知的內(nèi)容东囚,而且不影響當(dāng)前app的使用跺嗽。要實(shí)現(xiàn)頂部懸浮效果有兩種方式:
- setFullScreenIntent
- PRIORITY_HIGH高優(yōu)先級(jí)
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setFullScreenIntent(pendingIntent, false)
.setSound(Uri.parse("android.resource://com.makeunion.notificationdemo/" + R.raw.qq))
.setContentTitle("頂部懸浮通知通知")
.setContentText("這是一條頂部懸浮通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
或者
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setPriority(Notification.PRIORITY_HIGH)
.setSound(Uri.parse("android.resource://com.makeunion.notificationdemo/" + R.raw.qq))
.setContentTitle("頂部懸浮通知")
.setContentText("這是一條頂部懸浮通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
鎖屏Notification
同樣是API21之后,Android可以實(shí)現(xiàn)在鎖屏狀態(tài)下顯示通知,對(duì)于大多數(shù)app而言桨嫁,在鎖屏狀態(tài)顯示是用戶友好的植兰,因?yàn)橐驗(yàn)橛脩舨挥么蜷_手機(jī)就能看到通知內(nèi)容,但是對(duì)于一些敏感信息璃吧,比如短信之類的楣导,最好不要在鎖屏顯示具體的信息內(nèi)容。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setContentIntent(pendingIntent)
.setNumber(23)
.setAutoCancel(true)
.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentTitle("鎖屏通知")
.setContentText("這是一條鎖屏通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
Action
API20之后新增的功能畜挨,這是一個(gè)很好的功能筒繁,我們通常一個(gè)通知的作用就是展示,最多給整體設(shè)置一個(gè)pendingIntent巴元,這樣擴(kuò)展性就比較差了毡咏,但是有了Action,我們就能在通知上自定義功能鍵了逮刨。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.icon_big))
.setFullScreenIntent(pendingIntent, false)
.addAction(new Notification.Action(R.drawable.icon_share, "分享", pendingIntent))
.addAction(new Notification.Action(R.drawable.icon_zan, "收藏", pendingIntent))
.addAction(new Notification.Action(R.drawable.icon_message, "消息", pendingIntent))
.setSound(Uri.parse("android.resource://com.makeunion.notificationdemo/" + R.raw.qq))
.setContentTitle("action功能通知")
.setContentText("這是一條action功能通知").build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
這里有幾點(diǎn)需要說明:
- Action的icon最好不要設(shè)計(jì)成有顏色的呕缭,按照MaterialDesign的規(guī)范,最好是白色內(nèi)容修己,透明背景恢总。如果有別的顏色,也會(huì)被著色成白色睬愤。(但是有的定制機(jī)又不會(huì))
- Action不要太多片仿,3個(gè)已經(jīng)足夠了,太多了顯示有可能出問題
- api20才支持尤辱,舊版本使用會(huì)報(bào)錯(cuò)
自定義小視圖
自定義視圖用的還是挺多的滋戳,畢竟原生視圖太丑。
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 100, intent, PendingIntent.FLAG_CANCEL_CURRENT);
RemoteViews remoteViews = new RemoteViews("com.makeunion.notificationdemo", R.layout.layout_remote_view);
remoteViews.setOnClickPendingIntent(R.id.play_pause_img, pendingIntent);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setAutoCancel(true)
.setContentIntent(pendingIntent)
.setContent(remoteViews).build();
mNotificationManager.notify(NOTIFICATION_ID, notification);
layout布局的代碼我就不貼了啥刻,很簡單奸鸯,沒什么說的
是不是婉如一個(gè)真的音樂播放器。(原諒我最近玩抖音玩入迷了~)
自定義大視圖
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
RemoteViews remoteViews = new RemoteViews("com.makeunion.notificationdemo", R.layout.layout_remote_view);
RemoteViews bigRemoteViews = new RemoteViews("com.makeunion.notificationdemo", R.layout.layout_big_remote_view);
Notification notification = new Notification.Builder(this)
.setSmallIcon(R.drawable.icon_small)
.setOngoing(true)
.setTicker("來了一條伸縮布局通知")
.build();
notification.bigContentView = bigRemoteViews;
notification.contentView = remoteViews;
mNotificationManager.notify(NOTIFICATION_ID, notification);
這是一個(gè)大圖模式和小圖模式的切換可帽,效果還不錯(cuò)娄涩,但是兼容性不行,在有些手機(jī)上顯示效果不對(duì)映跟,還需要繼續(xù)研究蓄拣。和定制機(jī)也有一定關(guān)系。
總結(jié)
好啦努隙,這就是本期的Notification球恤,總的來說內(nèi)容比較簡單,但很實(shí)用荸镊。后面還會(huì)有一系列很實(shí)用的Android文章出爐咽斧。
說到這堪置,我想起一個(gè)很久前的故事,某日我寫了一篇博客张惹,一個(gè)心懷夢(mèng)想的年輕人看了我的博客舀锨,并給我打了2塊錢的賞,今后幾年他事業(yè)順利宛逗,婚姻幸福坎匿,身體健康,越來越帥雷激,從此走上人生巔峰替蔬。