建立自訂通知版面配置

如要確保通知在不同 Android 版本中都能呈現最佳狀態,請使用標準通知範本建構通知。如要在通知中提供更多內容,請考慮使用其中一個可展開的通知範本。

不過,如果系統範本不符合需求,您也可以使用自己的通知版面配置。

為內容區域建立自訂版面配置

如需自訂版面配置,您可以將 NotificationCompat.DecoratedCustomViewStyle 套用至通知。這個 API 可讓您為通常由標題和文字內容佔用的內容區域提供自訂版面配置,同時仍使用系統裝飾項目做為通知圖示、時間戳記、子文字和動作按鈕。

這個 API 的運作方式與可展開的通知範本類似,都是以基本通知版面配置為基礎,做法如下:

  1. 使用 NotificationCompat.Builder 建立基本通知。
  2. 呼叫 setStyle(),並傳遞 NotificationCompat.DecoratedCustomViewStyle 的例項。
  3. 將自訂版面配置加載為 RemoteViews 的例項。
  4. 呼叫 setCustomContentView(),設定收合通知的版面配置。
  5. 您也可以視需要呼叫 setCustomBigContentView(),為展開的通知設定不同的版面配置。

準備版面配置

您需要 small 和 large 版面配置。在本例中,small 版面配置可能如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:text="Small notification, showing only a title" />
</LinearLayout>

large 版面配置可能如下所示:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:orientation="vertical">

    <TextView
        android:id="@+id/notification_title"
        style="@style/TextAppearance.Compat.Notification.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Large notification, showing a title and a body." />

    <TextView
        android:id="@+id/notification_body"
        style="@style/TextAppearance.Compat.Notification.Line2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="This is the body. The height is manually forced to 300dp." />
</LinearLayout>

建構及顯示通知

版面配置準備就緒後,您就可以使用這些版面配置,如以下範例所示:

val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager

// Get the layouts to use in the custom notification.
val notificationLayout = RemoteViews(packageName, R.layout.notification_small)
val notificationLayoutExpanded = RemoteViews(packageName, R.layout.notification_large)

// Apply the layouts to the notification.
val customNotification = NotificationCompat.Builder(context, CHANNEL_ID)
    .setSmallIcon(R.drawable.notification_icon)
    .setStyle(NotificationCompat.DecoratedCustomViewStyle())
    .setCustomContentView(notificationLayout)
    .setCustomBigContentView(notificationLayoutExpanded)
    .build()

notificationManager.notify(666, customNotification)

請注意,通知的背景顏色可能會因裝置和版本而異。在自訂版面配置中套用支援程式庫樣式,例如文字的 TextAppearance_Compat_Notification 和標題的 TextAppearance_Compat_Notification_Title,如下例所示。這些樣式會配合色彩變化調整,因此不會出現黑底黑字或白底白字的情況。

<TextView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:text="@string/notification_title"
    android:id="@+id/notification_title"
    style="@style/TextAppearance.Compat.Notification.Title" />

請勿在 RemoteViews 物件上設定背景圖片,否則文字可能會無法辨識。

當您在使用者使用應用程式時觸發通知,結果會類似於圖 1:

收合的自訂通知版面配置
圖 1:使用其他應用程式時,會顯示小型通知版面配置。

輕觸展開箭頭即可展開通知,如圖 2 所示:

展開的自訂通知版面配置
圖 2 使用其他應用程式時,系統會顯示大型通知版面配置。

通知停留時間結束後,通知只會顯示在系統資訊列中,如圖 3 所示:

系統資訊列中的收合通知
圖 3. 小型通知版面配置在系統資訊列中的顯示方式。

輕觸展開箭頭即可展開通知,如圖 4 所示:

系統資訊列中展開的通知
圖 4. 系統資訊列會顯示大型通知版面配置。

建立完全自訂的通知版面配置

如果不希望通知採用標準通知圖示和標題,請按照上述步驟操作,但不要呼叫 setStyle()。