建立及管理通知管道

從 Android 8.0 (API 級別 26) 開始,所有通知都必須指派給 頻道。你可以為每個頻道設定視覺和聽覺行為 會套用至該頻道的所有通知使用者可以變更這些設定 並判斷哪些通知管道會造成乾擾 顯示。

請觀看以下影片,概略瞭解頻道概況和其他通知 這些功能。

通知管道的使用者設定選項適用於 如圖 1 所示

圖 1. 「時鐘」應用程式的通知設定 以及其中一個管道

建立通知管道後,就無法變更通知 行為生效之後,使用者就擁有完整的控制權。不過, 變更頻道的名稱和說明。

為需要傳送的每種通知建立管道。你也可以 建立通知管道來反映使用者的選擇。舉例來說, 您可以為建立的每個對話群組,設定不同的通知管道 使用者在訊息應用程式中

如果您指定 Android 8.0 (API 級別 26) 以上版本,則必須實作 以及更多通知管道如果 targetSdkVersion 設為 25 以下, 當您的應用程式在 Android 8.0 (API 級別 26) 以上版本中執行時,運作方式相同 適用於搭載 Android 7.1 (API 級別 25) 以下版本的裝置。

建立通知管道

如要建立通知管道,請按照下列步驟操作:

  1. 建立 NotificationChannel 物件 指派專屬的頻道 ID、可向使用者顯示的名稱和重要性等級

  2. 視需要指定使用者在系統設定中看到的說明 同時 setDescription()

  3. 將通知管道傳送給 createNotificationChannel()

,瞭解如何調查及移除這項存取權。

以下範例說明如何建立及註冊通知管道:

Kotlin

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    // Create the NotificationChannel.
    val name = getString(R.string.channel_name)
    val descriptionText = getString(R.string.channel_description)
    val importance = NotificationManager.IMPORTANCE_DEFAULT
    val mChannel = NotificationChannel(CHANNEL_ID, name, importance)
    mChannel.description = descriptionText
    // Register the channel with the system. You can't change the importance
    // or other notification behaviors after this.
    val notificationManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
    notificationManager.createNotificationChannel(mChannel)
}

Java

private void createNotificationChannel() {
    // Create the NotificationChannel, but only on API 26+ because
    // the NotificationChannel class is not in the Support Library.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        CharSequence name = getString(R.string.channel_name);
        String description = getString(R.string.channel_description);
        int importance = NotificationManager.IMPORTANCE_DEFAULT;
        NotificationChannel channel = new NotificationChannel(CHANNEL_ID, name, importance);
        channel.setDescription(description);
        // Register the channel with the system. You can't change the importance
        // or other notification behaviors after this.
        NotificationManager notificationManager = getSystemService(NotificationManager.class);
        notificationManager.createNotificationChannel(channel);
    }
}

以原始值重新建立現有通知管道無法執行 因此您可以在啟動應用程式時呼叫此程式碼。

根據預設,發布至特定頻道的所有通知都會採用 從 NotificationManagerCompat敬上 類別,例如 IMPORTANCE_DEFAULT。 或 IMPORTANCE_HIGH。 如要進一步瞭解,請參閱下一節的說明。 重要性等級

如要進一步自訂頻道的預設通知行為, 你可以使用 enableLights()setLightColor(), 和 setVibrationPattern() NotificationChannel。請記住,建立頻道後 無法變更這項設定,而使用者擁有控制權 都正常運作

您也可以在單一作業中建立多個通知管道 撥號中 createNotificationChannels()

設定重要性等級

頻道重要性會影響發布所有通知的干擾程度 頻道。請使用以下其中一種參數在 NotificationChannel 建構函式中指定該函式 五項重要性等級 IMPORTANCE_NONE(0)IMPORTANCE_HIGH(4)

如要支援搭載 Android 7.1 (API 級別 25) 以下版本的裝置,您還必須 通話 setPriority()敬上 使用從 NotificationCompat。 類別

重要性 (NotificationManager.IMPORTANCE_*) 和優先順序 (NotificationCompat.PRIORITY_*) 常數會對應到使用者可見的重要性 選項,如下表所示。

使用者可見的重要性等級 重要性 (Android 8.0 以上版本) 優先順序 (Android 7.1 以下版本)
緊急
會發出音效,並顯示為抬頭通知。
IMPORTANCE_HIGH PRIORITY_HIGHPRIORITY_MAX

會發出音效。
IMPORTANCE_DEFAULT PRIORITY_DEFAULT

不會發出聲音。
IMPORTANCE_LOW PRIORITY_LOW

不會發出聲音,也不會顯示在狀態列中。
IMPORTANCE_MIN PRIORITY_MIN

不會發出聲音,且不會顯示在狀態列或狀態列。
IMPORTANCE_NONE N/A

無論重要性為何,所有通知都會顯示在不受干擾的系統中 UI 位置,例如通知導覽匣 啟動器圖示上的標記 但您可以 修改通知標記的外觀

將頻道提交至 NotificationManager,你 無法變更重要性等級。不過,使用者可以 。

如要瞭解如何選擇適當的優先等級,請參閱「優先順序」 等級」的 通知設計指南

讀取通知管道設定

使用者可以修改通知管道的設定,包括行為 例如震動和快訊音效如果您想知道使用者採用的設定 都適用這些通知管道,請按照下列步驟操作:

  1. 呼叫取得 NotificationChannel 物件 getNotificationChannel()getNotificationChannels()

  2. 查詢特定管道設定,例如 getVibrationPattern()getSound()getImportance()

如果發現頻道設定,並認為該設定妨礙預期行為 可以建議使用者變更設定,並加入相關操作 開啟頻道設定,如下一節所示

開啟通知管道設定

建立通知管道後,就無法變更通知 透過程式輔助方式呈現頻道視覺和聽覺行為只有使用者可以 您可以在系統設定中變更頻道行為。為了為使用者提供 輕鬆存取這些通知設定,只要在應用程式的 可開啟這些系統設定的設定 UI

你可以用 Intent,這個虛擬機器會使用 ACTION_CHANNEL_NOTIFICATION_SETTINGS。 動作。

舉例來說,下列程式碼範例顯示如何將使用者重新導向至 通知管道的設定:

Kotlin

val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS).apply {
    putExtra(Settings.EXTRA_APP_PACKAGE, packageName)
    putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId())
}
startActivity(intent)

Java

Intent intent = new Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS);
intent.putExtra(Settings.EXTRA_APP_PACKAGE, getPackageName());
intent.putExtra(Settings.EXTRA_CHANNEL_ID, myNotificationChannel.getId());
startActivity(intent);

請注意,意圖需要兩個額外項目來指定應用程式套件名稱 (亦稱為應用程式 ID) 和要編輯的頻道。

刪除通知管道

如要刪除通知管道,請撥打 deleteNotificationChannel()。 下列程式碼範例說明如何完成這項程序:

Kotlin

// The id of the channel.
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val id: String = "my_channel_01"
notificationManager.deleteNotificationChannel(id)

Java

NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = "my_channel_01";
notificationManager.deleteNotificationChannel(id);

建立通知管道群組

如果想要在設定使用者介面中進一步整理頻道外觀 您可以建立管道群組如果您的應用程式是否支援 多個使用者帳戶 工作資料夾,因為這項服務可讓您 通知管道群組如此一來 並控管名稱相同的多個通知管道

圖 2. 通知管道設定: 群組。

舉例來說,社交網路應用程式可提供您個人和工作方面的支援 帳戶。在這種情況下,每個帳戶可能須收到多次通知。 含有相同函式與名稱的頻道,例如:

  • 擁有兩個頻道的個人帳戶:

    • 新留言

    • 貼文推薦

  • 具有兩個頻道的商家帳戶:

    • 新留言

    • 貼文推薦

將每個帳戶的通知管道整理成群組,可讓使用者 以便區分兩者

每個通知管道群組都需要一個 ID,此 ID 在 套件,以及使用者能看見的名稱。下列程式碼片段示範 來建立通知管道群組

Kotlin

// The id of the group.
val groupId = "my_group_01"
// The user-visible name of the group.
val groupName = getString(R.string.group_name)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannelGroup(NotificationChannelGroup(groupId, groupName))

Java

// The id of the group.
String groupId = "my_group_01";
// The user-visible name of the group.
CharSequence groupName = getString(R.string.group_name);
NotificationManager notificationManager =
        (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.createNotificationChannelGroup(new NotificationChannelGroup(groupId, groupName));

建立新群組後,你可以撥打 setGroup()敬上 ,將新的 NotificationChannel 物件與群組建立關聯。

將頻道提交給通知管理員後,就無法變更 通知管道與群組之間的關聯