建立自訂通知版面配置

為了讓通知在不同 Android 版本上都能呈現最佳效果,請使用標準通知範本建構通知。如果想在通知中提供更多內容,建議您使用其中一個可展開通知範本

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

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

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

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

  1. 使用 NotificationCompat.Builder 建構基本通知
  2. 呼叫 setStyle(),並向其傳遞 NotificationCompat.DecoratedCustomViewStyle 的例項。
  3. 加載自訂版面配置為 RemoteViews 的執行個體。
  4. 呼叫 setCustomContentView() 可設定收合通知的版面配置。
  5. 您也可以選擇呼叫 setCustomBigContentView(),為展開的通知設定不同的版面配置。

準備版面配置

您需要 smalllarge 版面配置。在這個範例中,small 版面配置可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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 版面配置可能如下所示:

<?xml version="1.0" encoding="utf-8"?>
<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>

建構並顯示通知

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

Kotlin

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)

Java

NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

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

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

notificationManager.notify(666, customNotification);

請注意,通知的背景顏色可能會因裝置和版本而有所不同。為自訂版面配置中的文字套用支援資料庫樣式,例如 TextAppearance_Compat_NotificationTextAppearance_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()