什么是通知渠道
每條通知都要屬于一個對應(yīng)的渠道逆趋。每個App都可以自由地創(chuàng)建當(dāng)前App擁有哪些通知渠道,但是這些通知渠道的控制權(quán)都是掌握在用戶手上的狭瞎。用戶可以自由地選擇這些通知渠道的重要程度今瀑,是否響鈴、是否振動拗引、或者是否要關(guān)閉這個渠道的通知借宵。Google這次對于8.0系統(tǒng)通知渠道的推廣態(tài)度還是比較強(qiáng)硬的,targetSdkVersion在26以上如果不使用通知渠道的話矾削,那么App的通知將完全無法彈出壤玫。
創(chuàng)建通知渠道
上代碼
String channelId = "message";
String channelName = "消息提示";
int importance = NotificationManager.IMPORTANCE_HIGH;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
NotificationChannel channel = new NotificationChannel(channelId, channelName, importance);
NotificationManager notificationManager = (NotificationManager) getSystemService(
NOTIFICATION_SERVICE);
notificationManager.createNotificationChannel(channel);
}
這里創(chuàng)建了一個叫消息提示的的通知渠道,重要程度為NotificationManager.IMPORTANCE_HIGH
緊急哼凯。
創(chuàng)建通知渠道的這部分代碼欲间,你可以寫在Activity中,也可以寫在Application中断部,實(shí)際上可以寫在程序的任何位置猎贴,只需要保證在通知彈出之前調(diào)用就可以了。并且創(chuàng)建通知渠道的代碼只在第一次執(zhí)行的時候才會創(chuàng)建蝴光,以后每次執(zhí)行創(chuàng)建代碼系統(tǒng)會檢測到該通知渠道已經(jīng)存在了她渴,因此不會重復(fù)創(chuàng)建,也并不會影響任何效率虱疏。
顯示通知
NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getContext(), NotificationActivity.class);
PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this, channelId)
//標(biāo)題
.setContentTitle("收到一條聊天消息")
//內(nèi)容
.setContentText("今天中午吃什么")
//設(shè)置發(fā)送的時間
.setWhen(System.currentTimeMillis())
//設(shè)置小圖標(biāo)(通知欄沒有下拉的圖標(biāo))
.setSmallIcon(R.drawable.icon_done)
//設(shè)置右側(cè)大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_2))
//設(shè)置點(diǎn)擊通知后自動刪除通知
.setAutoCancel(true)
.setContentIntent(pi)
.build();
notificationManager.notify(1, notification);
//似乎只有設(shè)置了setContentIntent惹骂,AutoCancel才能生效
多行文字通知
NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getContext(), NotificationActivity.class);
PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, 0);
String title = "多行文字標(biāo)題";
String content =
"多行文字內(nèi)容,多行文字內(nèi)容做瞪,多行文字內(nèi)容对粪,多行文字內(nèi)容,多行文字內(nèi)容装蓬,多行文字內(nèi)容著拭,多行文字內(nèi)容,多行文字內(nèi)容牍帚,多行文字內(nèi)容儡遮,多行文字內(nèi)容,多行文字內(nèi)容";
//創(chuàng)建多文字樣式
NotificationCompat.BigTextStyle bigTextStyle = new NotificationCompat.BigTextStyle()
.setBigContentTitle(title)
.bigText(content);
Notification notification = new NotificationCompat.Builder(this, channelId)
//標(biāo)題
.setContentTitle("你有一條新消息")
//設(shè)置小圖標(biāo)(通知欄沒有下拉的圖標(biāo))
.setSmallIcon(R.drawable.icon_done)
//設(shè)置右側(cè)大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_2))
//設(shè)置發(fā)送的時間
.setWhen(System.currentTimeMillis())
//設(shè)置點(diǎn)擊通知后自動刪除通知
.setAutoCancel(true)
.setContentIntent(pi)
.setStyle(bigTextStyle)
.build();
notificationManager.notify(1, notification);
//這里設(shè)置setContentText 也不會生效暗赶,會直接顯示bigTextStyle里的內(nèi)容鄙币,下拉之后不會顯示setContentTitle肃叶,直接顯示bigText
微信消息樣式
NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getContext(), NotificationActivity.class);
PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, 0);
String title = "冰冰";
ArrayList<String> messageList = new ArrayList<String>();
messageList.add("今晚有空嗎?");
messageList.add("晚上跟我一起去玩吧?");
messageList.add("怎么不回復(fù)我十嘿?因惭?我生氣了!绩衷!");
messageList.add("我真生氣了1哪А!?妊唷N鹁觥!你聽見了嗎!");
messageList.add("別不理我U忻ぁ5退酢!");
String content = "[" + messageList.size() + "條]" + title + ": " + messageList.get(0);
NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();
for (String msg : messageList) {
inboxStyle.addLine(msg);
}
inboxStyle.setSummaryText("[" + messageList.size() + "條]" + title);
Notification notification = new NotificationCompat.Builder(this, channelId)
//標(biāo)題
.setContentTitle(title)
//內(nèi)容
.setContentText(content)
//設(shè)置小圖標(biāo)(通知欄沒有下拉的圖標(biāo))
.setSmallIcon(R.drawable.icon_done)
//設(shè)置右側(cè)大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_2))
//設(shè)置發(fā)送的時間
.setWhen(System.currentTimeMillis())
//設(shè)置點(diǎn)擊通知后自動刪除通知
.setAutoCancel(true)
.setContentIntent(pi)
.setStyle(inboxStyle)
.build();
notificationManager.notify(1, notification);
進(jìn)度條樣式
NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getContext(), NotificationActivity.class);
PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this, channelId)
//標(biāo)題
.setContentTitle("正在下載")
//內(nèi)容
.setContentText("50%")
//設(shè)置發(fā)送的時間
.setWhen(System.currentTimeMillis())
//設(shè)置小圖標(biāo)(通知欄沒有下拉的圖標(biāo))
.setSmallIcon(R.drawable.icon_done)
//設(shè)置右側(cè)大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_2))
//設(shè)置點(diǎn)擊通知后自動刪除通知
.setAutoCancel(true)
.setContentIntent(pi)
//主要是這句
.setProgress(100, 50, false)
.build();
notificationManager.notify(1, notification);
//setProgress(100, 50, false)
大圖樣式
NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getContext(), NotificationActivity.class);
PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, 0);
NotificationCompat.BigPictureStyle bigPictureStyle =
new NotificationCompat.BigPictureStyle()
.bigPicture(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_5))
.setBigContentTitle("圖片標(biāo)題");
Notification notification = new NotificationCompat.Builder(this, channelId)
//標(biāo)題
.setContentTitle("你有一條新消息")
.setContentText("圖片")
//設(shè)置小圖標(biāo)(通知欄沒有下拉的圖標(biāo))
.setSmallIcon(R.drawable.icon_done)
//設(shè)置右側(cè)大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_2))
//設(shè)置發(fā)送的時間
.setWhen(System.currentTimeMillis())
//設(shè)置點(diǎn)擊通知后自動刪除通知
.setAutoCancel(true)
.setContentIntent(pi)
.setStyle(bigPictureStyle)
.build();
notificationManager.notify(1, notification);
自定義通知欄view
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.custom_view_layout);
// 設(shè)置點(diǎn)擊事件
remoteViews.setOnClickPendingIntent(R.id.iv_play_or_pause, getActivityPendingIntent(1));
remoteViews.setOnClickPendingIntent(R.id.iv_next, getActivityPendingIntent(2));
remoteViews.setOnClickPendingIntent(R.id.iv_cancel, getActivityPendingIntent(3));
remoteViews.setTextViewText(R.id.tv_title, "標(biāo)題");
remoteViews.setTextViewText(R.id.tv_summery, "藝術(shù)家");
NotificationManager notificationManager =
(NotificationManager) getContext().getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent = new Intent(getContext(), NotificationActivity.class);
PendingIntent pi = PendingIntent.getService(getContext(), 0, intent, 0);
Notification notification = new NotificationCompat.Builder(this, channelId)
//設(shè)置發(fā)送的時間
.setWhen(System.currentTimeMillis())
//設(shè)置小圖標(biāo)(通知欄沒有下拉的圖標(biāo))
.setSmallIcon(R.drawable.icon_done)
//設(shè)置右側(cè)大圖標(biāo)
.setLargeIcon(BitmapFactory.decodeResource(getResources(),
R.drawable.icon_head_hydra_2))
//設(shè)置點(diǎn)擊通知后自動刪除通知
.setAutoCancel(true)
.setContentIntent(pi)
.setContent(remoteViews)
.build();
notificationManager.notify(1, notification);
關(guān)于通知欄塊是很混亂的宪肖,Android系統(tǒng)大版本更新的時候經(jīng)常有變動表制。國內(nèi)廠商修改很嚴(yán)重,我手上有小米和vivo的測試機(jī)控乾,以上的代碼在不同的手機(jī)上表現(xiàn)也各不相同么介。如果有用到復(fù)雜視圖的通知欄,最好是自定義通知欄view蜕衡。
資料
Android通知欄微技巧壤短,8.0系統(tǒng)中通知欄的適配
NotifyUtil
NotificationDemo