自 2025 年 3 月 27 日起,我們建議您使用 android-latest-release
而非 aosp-main
建構及貢獻 AOSP。詳情請參閱「Android 開放原始碼計畫變更」。
啟用通知的通知權限
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Android 13 中的通知採用選擇加入模式,這與先前使用選擇退出模式的 Android 版本有所不同。在 Android 13 中,所有應用程式都必須先向使用者要求權限,才能傳送通知提示。這有助於減少通知中斷情形、減少資訊過載,並協助使用者根據自己重視的事項控制顯示的通知。為了支援選擇加入模式,原始設備製造商必須在通知和執行階段權限系統中實作變更。
本頁說明原始設備製造商 (OEM) 必須實作哪些內容才能支援這項變更,以及如何驗證實作內容。
實作選擇加入通知的變更
自 Android 13 起,應用程式必須先向系統要求 android.permission.POST_NOTIFICATION
執行階段權限,才能傳送通知,並宣告傳送通知的意圖。
在 Android 13 以上版本中,決定應用程式是否可向使用者傳送通知的設定會儲存在權限系統中。在 Android 13 之前,這項設定會儲存在通知系統中。因此,原始設備製造商必須將現有的通知資料 (關於應用程式是否允許傳送通知) 從通知系統遷移至執行階段權限系統。原始設備製造商 (OEM) 也必須在通知系統中維護現有的 API,以便將這些資料提供給應用程式開發人員。
通知和權限系統的變更,是根據使用者通知行為的選擇加入模式而定,並在「導入指南」一節中加以說明。
選擇接受型模式中的使用者通知行為
下表說明在搭載 Android 13 的裝置上,不同應用程式版本的通知行為:
搭載 Android 13 的裝置 |
指定 Android 13 以上版本的應用程式 |
指定 Android 13 以下版本的應用程式 |
新安裝
|
系統會在應用程式提示前封鎖通知。
應用程式可控制要求權限的時機。 |
系統會在作業系統提示前封鎖通知。
系統會在首次執行應用程式時要求權限。 |
現有應用程式 (升級)
|
在應用程式提示前,系統會允許通知。
系統會授予臨時權限,直到應用程式在首次符合資格的執行階段要求權限為止。 |
在作業系統提示前,系統會允許通知。
系統會在應用程式首次執行前授予臨時權限。 |
導入指南
如需參考實作方式,請參閱通知服務、權限服務和政策服務。如要為預設權限處理常式實作例外狀況,請參閱「執行階段權限」。
在導入期間,請針對指定 Android 13 以下 SDK 的應用程式,遵循下列使用者通知行為規範:
- 在 Android 13 裝置上新安裝的應用程式,必須在使用者核准權限提示後,才能傳送通知。
- 如果應用程式指定 Android 13 以上版本,則必須在應用程式提示前,將通知封鎖,因為應用程式會控管要求使用者權限的時機和方式。
- 如果應用程式指定的目標版本低於 Android 13,則必須在作業系統提示前封鎖通知。作業系統必須在第一次執行應用程式時顯示權限提示。
在升級至 Android 13 之前,裝置上已存在的任何應用程式,或透過備份和還原功能還原的任何應用程式,都必須允許傳送通知,直到使用者首次從該應用程式啟動活動為止。
針對指定 Android 13 以上版本 SDK 的應用程式,如果使用者先前未在應用程式或 NotificationChannel
層級為此應用程式自訂通知設定,請撤銷臨時權限授予作業。應用程式必須先向使用者要求權限,才能繼續傳送通知。
如果指定 Android 13 為目標版本的升級應用程式目前沒有透過臨時升級授權取得通知權限,且使用者至少已啟動一次,則應用程式必須先顯示通知權限提示,才能執行任何其他前景服務。
如果應用程式指定的 SDK 版本低於 Android 13,請在應用程式建立至少一個 NotificationChannel
後攔截首次啟動的活動,以便顯示權限提示,詢問使用者是否要接收來自應用程式的通知。
如果使用者先前曾在升級裝置或備份中,為應用程式在應用程式或 NotificationChannel
層級自訂通知設定,則必須使用 FLAG_PERMISSION_USER_SET
標記,將應用程式層級設定遷移至權限系統。除非應用程式特別要求,否則不得向使用者顯示其他通知權限提示。
備份和還原功能必須在 Android 13 裝置和較舊 OS 版本的裝置之間回溯和前向相容。從 Android 13 裝置產生的備份資料必須還原至較舊的 OS 版本,而較舊 OS 版本的備份資料則必須還原至 Android 13 裝置。
與持續進行的媒體播放相關的媒體通知必須免除通知權限。
驗證通知和權限系統的變更
如要驗證導入作業,請執行下列測試:
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-27 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-27 (世界標準時間)。"],[],[],null,["# Notification permission for opt-in notifications\n\nNotifications in Android 13 use an opt-in model, which\nis a change from previous Android versions, which use an opt-out model. In\nAndroid 13, all apps must ask users for permission before\nsending notification prompts. This model helps reduce notification\ninterruptions, minimizes information overload, and helps users control what\nnotifications appear based on what's important to them. To support the\nopt-in model, OEMs must implement changes in the notification and runtime\npermission systems.\n\nThis page describes what OEMs must implement to support this change and how\nto validate the implementation.\n\nImplement changes for opt-in notifications\n------------------------------------------\n\nStarting with Android 13, apps must declare their\nintent to send notifications by requesting the\n[`android.permission.POST_NOTIFICATION`](https://developer.android.com/about/versions/13/changes/notification-permission)\nruntime permission from the system before they can send notifications.\n\nIn Android 13 and higher, the setting that determines\nif an app can send notifications to the user is stored in the permission system.\nPrior to Android 13, this setting was stored in the\nnotification system. Hence, OEMs must migrate the existing notification data\nabout whether an app is allowed to send notifications, from the notification\nsystem into the runtime permission system. OEMs must also maintain existing APIs\nin the notification system that surface that data to app developers.\n\nChanges to the notification and permission systems are based on the\n[opt-in model of user notification behavior](#behavior-optin) and are\ndescribed in the [Guidelines for implementation](#guidelines-impl) section.\n\n### Behavior of user notifications in an opt-in model\n\nThe following table illustrates the notification behavior for various app\nversions on a device running Android 13:\n\n| Device on Android 13 | Apps targeting Android 13 or higher | Apps targeting versions lower than Android 13 |\n|------------------------|--------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------|\n| New install | Notifications are blocked until prompted by the app. Apps control when to ask for permission. | Notifications are blocked until prompted by the OS. Permission is asked on the first run of the app. |\n| Existing app (upgrade) | Notifications are allowed until prompted by the app. Temporary permission is granted until the app asks on the first qualifying run. | Notifications are allowed until prompted by the OS. Temporary permission is granted until the first run of the app. |\n\n### Guidelines for implementation\n\nFor reference implementation, refer to\n[notification service](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/notification/),\n[permission service](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/pm/permission/) and\n[policy service](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/policy). To implement exceptions\nfor default permission handlers see\n[Runtime Permissions](/docs/core/permissions/runtime_perms#integration).\n\nDuring implementation, use the following guidelines on user notification\nbehavior for apps targeting Android 13 or lower SDKs:\n\n- Freshly installed apps on an Android 13 device must not send a notification without the user approving a permission prompt.\n - If the app targets versions Android 13 and higher, notifications must be blocked until prompted by the app as the app controls when and if to ask for user permission.\n - If the app targets versions lower than Android 13, notifications must be blocked until prompted by the OS. The OS must show the permission prompt on the first run of the app.\n- Any app that existed on the device prior to an upgrade to\n Android 13, or any app that was restored through backup\n and restore, must be allowed to send notifications until the first time the user\n launches an activity from that app.\n\n - For apps that target SDK of versions Android 13\n and higher, if the user hasn't previously customized notification settings for\n this app at the app or `NotificationChannel` level, revoke the temporary\n permission grant. Apps must then ask the user for permission before being\n allowed to continue to send notifications.\n\n If an upgraded app targeting Android 13 doesn't\n currently have the notification permission through the temporary upgrade\n grant, and the user has launched it at least once, the app must show a\n notification permission prompt before it's allowed to run any further foreground\n services.\n - For apps that have a target SDK of versions lower than\n Android 13,\n [intercept](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/core/java/com/android/server/wm/ActivityInterceptorCallback.java)\n the first activity launch after the app has created at least one `NotificationChannel`\n to show a permission prompt asking if the user wants to receive notifications\n from the app.\n\n If a user previously customized notification settings at the\n app or `NotificationChannel` level for an app on the upgrading device or in a\n backup being restored to the device, the app level setting must be migrated into\n the permission system with the `FLAG_PERMISSION_USER_SET` flag. No further\n notification permission prompt must be shown to the user unless the app\n specifically asks it to be.\n- Backup and restore must be backward and forward compatible between an\n Android 13 device and a device from an earlier OS\n version. Backup data generated from an Android 13\n device must restore onto an earlier OS version, and backup data from an earlier\n OS version must restore onto an Android 13 device.\n\n- Media notifications associated with ongoing media playback must be exempt\n from the notification permission.\n\nValidate changes to the notification and permission systems\n-----------------------------------------------------------\n\nTo validate the implementation, run the following tests:\n\n- Unit tests as specified in [`PreferencesHelperTest`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/tests/uiservicestests/src/com/android/server/notification/PreferencesHelperTest.java),\n [`NotificationManagerServiceTest`](https://cs.android.com/android/platform/superproject/+/android-latest-release:frameworks/base/services/tests/uiservicestests/src/com/android/server/notification/NotificationManagerServiceTest.java).\n\n- Any manual test that tests upgrades and backup and restore.\n\n- Any CTS Permission and Notification system test that sends notifications.\n Some of these tests are located in [cts/tests/tests/permission/](https://cs.android.com/android/platform/superproject/+/android-latest-release:packages/modules/Permission/tests/cts/permission/src/android/permission/cts/),\n [NotificationManagerTest.java](https://cs.android.com/android/platform/superproject/+/android-latest-release:cts/tests/tests/notification/src/android/app/notification/current/cts/NotificationManagerTest.java?q=NotificationManagerTest.java),\n and [cts/tests/tests/notificationlegacy/](https://cs.android.com/android/platform/superproject/+/android-latest-release:cts/tests/tests/notificationlegacy/)."]]