Android O 新特性 — Notification
新特性
在 Android 8.0 中,我們已重新設(shè)計(jì)通知,以便為管理通知行為和設(shè)置提供更輕松和更統(tǒng)一的方式。這些變更包括:
- 通知渠道:Android 8.0 引入了通知渠道,其允許您為要顯示的每種通知類(lèi)型創(chuàng)建用戶(hù)可自定義的渠道。用戶(hù)界面將通知渠道稱(chēng)之為通知類(lèi)別。要了解如何實(shí)現(xiàn)通知渠道的信息,請(qǐng)參閱通知渠道指南。
- 通知標(biāo)志:Android 8.0 引入了對(duì)在應(yīng)用啟動(dòng)器圖標(biāo)上顯示通知標(biāo)志的支持。通知標(biāo)志可反映某個(gè)應(yīng)用是否存在與其關(guān)聯(lián)、并且用戶(hù)尚未予以清除也未對(duì)其采取行動(dòng)的通知。通知標(biāo)志也稱(chēng)為通知點(diǎn)。要了解如何調(diào)整通知標(biāo)志,請(qǐng)參閱通知標(biāo)志指南。
- 休眠:用戶(hù)可以將通知置于休眠狀態(tài),以便稍后重新顯示它。重新顯示時(shí)通知的重要程度與首次顯示時(shí)相同。應(yīng)用可以移除或更新已休眠的通知,但更新休眠的通知并不會(huì)使其重新顯示。
- 通知超時(shí):現(xiàn)在,使用 setTimeoutAfter() 創(chuàng)建通知時(shí)您可以設(shè)置超時(shí)。您可以使用此函數(shù)指定一個(gè)持續(xù)時(shí)間,超過(guò)該持續(xù)時(shí)間后,通知應(yīng)取消。如果需要,您可以在指定的超時(shí)持續(xù)時(shí)間之前取消通知。
- 通知設(shè)置:當(dāng)您使用 Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCESIntent 從通知?jiǎng)?chuàng)建指向應(yīng)用通知設(shè)置的鏈接時(shí),您可以調(diào)用 setSettingsText() 來(lái)設(shè)置要顯示的文本。此系統(tǒng)可以提供以下 Extra 數(shù)據(jù)和 Intent,用于過(guò)濾應(yīng)用必須向用戶(hù)顯示的設(shè)置:EXTRA_CHANNEL_ID、NOTIFICATION_TAG 和 NOTIFICATION_ID。
- 通知清除:系統(tǒng)現(xiàn)在可區(qū)分通知是由用戶(hù)清除,還是由應(yīng)用移除。要查看清除通知的方式,您應(yīng)實(shí)現(xiàn) NotificationListenerService 類(lèi)的新 onNotificationRemoved() 函數(shù)。
- 背景顏色:您現(xiàn)在可以設(shè)置和啟用通知的背景顏色。只能在用戶(hù)必須一眼就能看到的持續(xù)任務(wù)的通知中使用此功能。例如,您可以為與駕車(chē)路線或正在進(jìn)行的通話(huà)有關(guān)的通知設(shè)置背景顏色。您還可以使用 Notification.Builder.setColor() 設(shè)置所需的背景顏色。這樣做將允許您使用 Notification.Builder.setColorized() 啟用通知的背景顏色設(shè)置。
- 消息樣式:現(xiàn)在,使用 MessagingStyle 類(lèi)的通知可在其折疊形式中顯示更多內(nèi)容。對(duì)于與消息有關(guān)的通知,您應(yīng)使用 MessagingStyle 類(lèi)。您還可以使用新的 addHistoricMessage() 函數(shù),通過(guò)向與消息相關(guān)的通知添加歷史消息為會(huì)話(huà)提供上下文。
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
創(chuàng)建通知
您可以在 NotificationCompat.Builder 對(duì)象中為通知指定 UI 信息和操作。要?jiǎng)?chuàng)建通知,請(qǐng)調(diào)用 NotificationCompat.Builder.build(),它將返回包含您的具體規(guī)范的 Notification 對(duì)象。要發(fā)出通知,請(qǐng)通過(guò)調(diào)用 NotificationManager.notify() 將 Notification 對(duì)象傳遞給系統(tǒng)。
必需的通知內(nèi)容
Notification 對(duì)象必須包含以下內(nèi)容:
- 小圖標(biāo),由 setSmallIcon() 設(shè)置
- 標(biāo)題,由 setContentTitle() 設(shè)置
- 詳細(xì)文本,由 setContentText() 設(shè)置
創(chuàng)建通知/自定義通知渠道
(1)NotificationManager獲取
NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);復(fù)制代碼(2)通知渠道的創(chuàng)建
// Creates an explicit intent for an Activity in your app Intent intent = new Intent(MainActivity.this, Main2Activity.class);TaskStackBuilder taskStackBuilder = TaskStackBuilder.create(MainActivity.this); taskStackBuilder.addParentStack(Main2Activity.class); taskStackBuilder.addNextIntent(intent);// 通過(guò)taskStackBuilder對(duì)象獲取PendingIntent PendingIntent pi = taskStackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);String custom_id = "marco_notification"; // 自定義通知渠道 idCharSequence name = getString(R.string.channel_name); // 自定義通知渠道 nameString description = getString(R.string.channel_description); // 自定義通知渠道描述 int importance = NotificationManager.IMPORTANCE_HIGH; // 自定義通知渠道級(jí)別//創(chuàng)建自定義渠道 NotificationChannel marco_channel = new NotificationChannel(custom_id, name, importance);// 添加一系列特性 marco_channel.setDescription(description); marco_channel.enableLights(true); marco_channel.setLightColor(Color.RED);notificationManager.createNotificationChannel(marco_channel);復(fù)制代碼(3)通知的創(chuàng)建、顯示
NotificationCompat.Builder notification = new NotificationCompat.Builder(MainActivity.this, custom_id) .setContentTitle("This is a Notification") .setContentText("Notification contentText") .setSmallIcon(R.drawable.ic_launcher_background) .setContentIntent(pi); notificationManager.notify(1,notification.build());復(fù)制代碼以下為代碼截圖和通知顯示效果:
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 圖1. 創(chuàng)建自定義渠道的Notification代碼邏輯
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?圖2. Notification顯示截圖
通知超時(shí)
NotificationCompat.Builder notification = new NotificationCompat.Builder(MainActivity.this, custom_id).setContentTitle("This is a Notification").setContentText("Notification contentText").setSmallIcon(R.drawable.ic_launcher_background).setContentIntent(pi)// 設(shè)置超時(shí)時(shí)間,5000 = 5秒,Notification將會(huì)消失 .setTimeoutAfter(5000);復(fù)制代碼總結(jié)
以上是生活随笔為你收集整理的Android O 新特性 — Notification的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: linux基础知识复习
- 下一篇: Android样式和主题(二):系统有哪