Android O 新特性 — Notification

新特性

Android 8.0 中,咱們已從新設計通知,以便爲管理通知行爲和設置提供更輕鬆和更統一的方式。這些變動包括:html

  • 通知渠道:Android 8.0 引入了通知渠道,其容許您爲要顯示的每種通知類型建立用戶可自定義的渠道。用戶界面將通知渠道稱之爲通知類別。要了解如何實現通知渠道的信息,請參閱通知渠道指南。
  • 通知標誌:Android 8.0 引入了對在應用啓動器圖標上顯示通知標誌的支持。通知標誌可反映某個應用是否存在與其關聯、而且用戶還沒有予以清除也未對其採起行動的通知。通知標誌也稱爲通知點。要了解如何調整通知標誌,請參閱通知標誌指南。
  • 休眠:用戶能夠將通知置於休眠狀態,以便稍後從新顯示它。從新顯示時通知的重要程度與首次顯示時相同。應用能夠移除或更新已休眠的通知,但更新休眠的通知並不會使其從新顯示。
  • 通知超時:如今,使用 setTimeoutAfter() 建立通知時您能夠設置超時。您可使用此函數指定一個持續時間,超過該持續時間後,通知應取消。若是須要,您能夠在指定的超時持續時間以前取消通知。
  • 通知設置:當您使用 Notification.INTENT_CATEGORY_NOTIFICATION_PREFERENCESIntent 從通知建立指向應用通知設置的連接時,您能夠調用 setSettingsText() 來設置要顯示的文本。此係統能夠提供如下 Extra 數據和 Intent,用於過濾應用必須向用戶顯示的設置:EXTRA_CHANNEL_IDNOTIFICATION_TAGNOTIFICATION_ID
  • 通知清除:系統如今可區分通知是由用戶清除,仍是由應用移除。要查看清除通知的方式,您應實現 NotificationListenerService 類的新 onNotificationRemoved() 函數。
  • 背景顏色:您如今能夠設置和啓用通知的背景顏色。只能在用戶必須一眼就能看到的持續任務的通知中使用此功能。例如,您能夠爲與駕車路線或正在進行的通話有關的通知設置背景顏色。您還可使用 Notification.Builder.setColor() 設置所需的背景顏色。這樣作將容許您使用 Notification.Builder.setColorized() 啓用通知的背景顏色設置。
  • 消息樣式:如今,使用 MessagingStyle 類的通知可在其摺疊形式中顯示更多內容。對於與消息有關的通知,您應使用 MessagingStyle 類。您還可使用新的 addHistoricMessage() 函數,經過向與消息相關的通知添加歷史消息爲會話提供上下文。

                                  

建立通知

您能夠在 NotificationCompat.Builder 對象中爲通知指定 UI 信息和操做。要建立通知,請調用 NotificationCompat.Builder.build(),它將返回包含您的具體規範的 Notification 對象。要發出通知,請經過調用 NotificationManager.notify()Notification 對象傳遞給系統。java

必需的通知內容

Notification 對象必須包含如下內容:android

建立通知/自定義通知渠道

(1)NotificationManager獲取

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);複製代碼

(2)通知渠道的建立

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

// 經過taskStackBuilder對象獲取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;    // 自定義通知渠道級別

//建立自定義渠道
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);複製代碼

(3)通知的建立、顯示

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());複製代碼

如下爲代碼截圖和通知顯示效果:bash


                                              圖1. 建立自定義渠道的Notification代碼邏輯app


                                                               圖2. Notification顯示截圖函數

通知超時

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)
        // 設置超時時間,5000 = 5秒,Notification將會消失        
        .setTimeoutAfter(5000);複製代碼
相關文章
相關標籤/搜索