Kể từ ngày 27 tháng 3 năm 2025, bạn nên sử dụng android-latest-release
thay vì aosp-main
để xây dựng và đóng góp cho AOSP. Để biết thêm thông tin, hãy xem phần Thay đổi đối với AOSP.
Quyền gửi thông báo cho thông báo chọn sử dụng
Sử dụng bộ sưu tập để sắp xếp ngăn nắp các trang
Lưu và phân loại nội dung dựa trên lựa chọn ưu tiên của bạn.
Thông báo trong Android 13 sử dụng mô hình chọn tham gia, đây là thay đổi so với các phiên bản Android trước đây sử dụng mô hình chọn không tham gia. Trong Android 13, tất cả ứng dụng đều phải yêu cầu người dùng cấp quyền trước khi gửi lời nhắc thông báo. Mô hình này giúp giảm tình trạng gián đoạn thông báo, giảm thiểu tình trạng quá tải thông tin và giúp người dùng kiểm soát thông báo nào xuất hiện dựa trên những thông báo quan trọng với họ. Để hỗ trợ mô hình chọn tham gia, OEM phải triển khai các thay đổi trong hệ thống thông báo và quyền khi bắt đầu chạy.
Trang này mô tả những việc mà nhà sản xuất thiết bị gốc (OEM) phải triển khai để hỗ trợ thay đổi này và cách xác thực việc triển khai.
Triển khai các thay đổi đối với thông báo chọn sử dụng
Kể từ Android 13, ứng dụng phải khai báo ý định gửi thông báo bằng cách yêu cầu quyền khi bắt đầu chạy android.permission.POST_NOTIFICATION
từ hệ thống trước khi có thể gửi thông báo.
Trên Android 13 trở lên, chế độ cài đặt xác định xem một ứng dụng có thể gửi thông báo cho người dùng hay không được lưu trữ trong hệ thống quyền.
Trước Android 13, chế độ cài đặt này được lưu trữ trong hệ thống thông báo. Do đó, nhà sản xuất thiết bị gốc (OEM) phải di chuyển dữ liệu thông báo hiện có về việc một ứng dụng có được phép gửi thông báo hay không, từ hệ thống thông báo sang hệ thống cấp quyền khi bắt đầu chạy. Nhà sản xuất thiết bị gốc (OEM) cũng phải duy trì các API hiện có trong hệ thống thông báo hiển thị dữ liệu đó cho nhà phát triển ứng dụng.
Các thay đổi đối với hệ thống thông báo và quyền dựa trên mô hình chọn tham gia hành vi thông báo cho người dùng và được mô tả trong phần Nguyên tắc triển khai.
Hành vi của thông báo cho người dùng trong mô hình chọn sử dụng
Bảng sau đây minh hoạ hành vi thông báo cho nhiều phiên bản ứng dụng trên một thiết bị chạy Android 13:
Thiết bị chạy Android 13 |
Ứng dụng nhắm đến Android 13 trở lên |
Ứng dụng nhắm đến các phiên bản thấp hơn Android 13 |
Lượt cài đặt mới
|
Thông báo sẽ bị chặn cho đến khi ứng dụng nhắc.
Ứng dụng kiểm soát thời điểm yêu cầu cấp quyền.
|
Thông báo sẽ bị chặn cho đến khi hệ điều hành nhắc.
Ứng dụng sẽ yêu cầu cấp quyền trong lần chạy đầu tiên.
|
Ứng dụng hiện có (nâng cấp)
|
Cho phép thông báo cho đến khi ứng dụng nhắc.
Quyền tạm thời được cấp cho đến khi ứng dụng yêu cầu trong lần chạy đủ điều kiện đầu tiên.
|
Cho phép thông báo cho đến khi hệ điều hành nhắc.
Quyền tạm thời được cấp cho đến lần chạy đầu tiên của ứng dụng.
|
Nguyên tắc triển khai
Để triển khai tham chiếu, hãy tham khảo dịch vụ thông báo, dịch vụ quyền và dịch vụ chính sách. Để triển khai các trường hợp ngoại lệ cho trình xử lý quyền mặc định, hãy xem phần Quyền khi bắt đầu chạy.
Trong quá trình triển khai, hãy sử dụng các nguyên tắc sau đây về hành vi thông báo cho người dùng đối với các ứng dụng nhắm đến SDK Android 13 trở xuống:
- Các ứng dụng mới cài đặt trên thiết bị Android 13 không được gửi thông báo nếu người dùng không phê duyệt lời nhắc cấp quyền.
- Nếu ứng dụng nhắm đến các phiên bản Android 13 trở lên, thì thông báo phải bị chặn cho đến khi ứng dụng nhắc, vì ứng dụng kiểm soát thời điểm và liệu có yêu cầu người dùng cấp quyền hay không.
- Nếu ứng dụng nhắm đến các phiên bản thấp hơn Android 13, thì bạn phải chặn thông báo cho đến khi hệ điều hành nhắc. Hệ điều hành phải hiển thị lời nhắc cấp quyền trong lần chạy đầu tiên của ứng dụng.
Mọi ứng dụng đã tồn tại trên thiết bị trước khi nâng cấp lên Android 13 hoặc mọi ứng dụng đã được khôi phục thông qua tính năng sao lưu và khôi phục đều phải được phép gửi thông báo cho đến lần đầu tiên người dùng khởi chạy một hoạt động từ ứng dụng đó.
Đối với các ứng dụng nhắm đến SDK của phiên bản Android 13 trở lên, nếu trước đó người dùng chưa tuỳ chỉnh chế độ cài đặt thông báo cho ứng dụng này ở cấp ứng dụng hoặc NotificationChannel
, hãy thu hồi quyền cấp tạm thời. Sau đó, ứng dụng phải yêu cầu người dùng cấp quyền trước khi được phép tiếp tục gửi thông báo.
Nếu một ứng dụng đã nâng cấp nhắm đến Android 13 hiện không có quyền thông báo thông qua quyền cấp nâng cấp tạm thời và người dùng đã chạy ứng dụng đó ít nhất một lần, thì ứng dụng đó phải hiển thị lời nhắc cấp quyền thông báo trước khi được phép chạy bất kỳ dịch vụ nào khác trên nền trước.
Đối với các ứng dụng có SDK mục tiêu là các phiên bản thấp hơn Android 13, hãy chặn lần chạy hoạt động đầu tiên sau khi ứng dụng đã tạo ít nhất một NotificationChannel
để hiển thị lời nhắc cấp quyền hỏi xem người dùng có muốn nhận thông báo từ ứng dụng hay không.
Nếu trước đây người dùng đã tuỳ chỉnh chế độ cài đặt thông báo ở cấp ứng dụng hoặc NotificationChannel
cho một ứng dụng trên thiết bị nâng cấp hoặc trong bản sao lưu đang được khôi phục cho thiết bị, thì chế độ cài đặt cấp ứng dụng phải được di chuyển vào hệ thống quyền bằng cờ FLAG_PERMISSION_USER_SET
. Không được hiển thị thêm lời nhắc cấp quyền thông báo cho người dùng, trừ phi ứng dụng yêu cầu cụ thể.
Tính năng sao lưu và khôi phục phải tương thích ngược và tương thích tiến giữa thiết bị Android 13 và thiết bị chạy phiên bản hệ điều hành cũ. Dữ liệu sao lưu được tạo từ thiết bị Android 13 phải khôi phục trên phiên bản hệ điều hành cũ và dữ liệu sao lưu từ phiên bản hệ điều hành cũ phải khôi phục trên thiết bị Android 13.
Thông báo đa phương tiện liên kết với quá trình phát nội dung đa phương tiện đang diễn ra phải được miễn khỏi quyền thông báo.
Xác thực các thay đổi đối với hệ thống thông báo và quyền
Để xác thực quá trình triển khai, hãy chạy các kiểm thử sau:
Nội dung và mã mẫu trên trang này phải tuân thủ các giấy phép như mô tả trong phần Giấy phép nội dung. Java và OpenJDK là nhãn hiệu hoặc nhãn hiệu đã đăng ký của Oracle và/hoặc đơn vị liên kết của Oracle.
Cập nhật lần gần đây nhất: 2025-07-27 UTC.
[[["Dễ hiểu","easyToUnderstand","thumb-up"],["Giúp tôi giải quyết được vấn đề","solvedMyProblem","thumb-up"],["Khác","otherUp","thumb-up"]],[["Thiếu thông tin tôi cần","missingTheInformationINeed","thumb-down"],["Quá phức tạp/quá nhiều bước","tooComplicatedTooManySteps","thumb-down"],["Đã lỗi thời","outOfDate","thumb-down"],["Vấn đề về bản dịch","translationIssue","thumb-down"],["Vấn đề về mẫu/mã","samplesCodeIssue","thumb-down"],["Khác","otherDown","thumb-down"]],["Cập nhật lần gần đây nhất: 2025-07-27 UTC."],[],[],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/)."]]