ตั้งแต่วันที่ 27 มีนาคม 2025 เป็นต้นไป เราขอแนะนำให้ใช้ android-latest-release
แทน aosp-main
เพื่อสร้างและมีส่วนร่วมใน AOSP โปรดดูข้อมูลเพิ่มเติมที่หัวข้อการเปลี่ยนแปลงใน AOSP
สิทธิ์การแจ้งเตือนสําหรับการแจ้งเตือนแบบเลือกใช้
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
การแจ้งเตือนใน Android 13 ใช้รูปแบบการเลือกใช้ ซึ่งแตกต่างจาก Android เวอร์ชันก่อนหน้าที่ใช้รูปแบบการไม่เลือกใช้ ใน Android 13 แอปทั้งหมดต้องขอสิทธิ์จากผู้ใช้ก่อนส่งข้อความแจ้งเตือน รูปแบบนี้ช่วยลดการหยุดชะงักจากการแจ้งเตือน ลดปริมาณข้อมูลที่มากเกินไป และช่วยให้ผู้ใช้ควบคุมการแจ้งเตือนที่จะปรากฏตามสิ่งที่สำคัญกับตน หากต้องการรองรับรูปแบบการเลือกใช้ OEM ต้องทำการเปลี่ยนแปลงในระบบการแจ้งเตือนและสิทธิ์รันไทม์
หน้านี้จะอธิบายสิ่งที่ OEM ต้องติดตั้งใช้งานเพื่อรองรับการเปลี่ยนแปลงนี้ และวิธีตรวจสอบการติดตั้งใช้งาน
ใช้การเปลี่ยนแปลงสำหรับการแจ้งเตือนแบบเลือกใช้
ตั้งแต่ Android 13 เป็นต้นไป แอปต้องประกาศความตั้งใจที่จะส่งการแจ้งเตือนโดยขอสิทธิ์รันไทม์ android.permission.POST_NOTIFICATION
จากระบบก่อนจึงจะส่งการแจ้งเตือนได้
ใน Android 13 ขึ้นไป ระบบจะจัดเก็บการตั้งค่าที่กำหนดว่าแอปจะส่งการแจ้งเตือนไปยังผู้ใช้ได้หรือไม่ไว้ในระบบสิทธิ์
ก่อนที่จะมี Android 13 การตั้งค่านี้จะจัดเก็บไว้ในระบบการแจ้งเตือน ดังนั้น OEM จึงต้องย้ายข้อมูลการแจ้งเตือนที่มีอยู่เกี่ยวกับแอปที่ได้รับอนุญาตให้ส่งการแจ้งเตือนหรือไม่จากระบบการแจ้งเตือนไปยังระบบสิทธิ์รันไทม์ OEM จะต้องดูแลรักษา API ที่มีอยู่ของระบบการแจ้งเตือนที่แสดงข้อมูลดังกล่าวต่อนักพัฒนาแอปด้วย
การเปลี่ยนแปลงระบบการแจ้งเตือนและสิทธิ์จะอิงตามรูปแบบการเลือกใช้ลักษณะการแจ้งเตือนของผู้ใช้ และอธิบายไว้ในส่วนหลักเกณฑ์การใช้งาน
ลักษณะการทํางานของการแจ้งเตือนผู้ใช้ในแบบเลือกใช้
ตารางต่อไปนี้แสดงลักษณะการแจ้งเตือนสำหรับแอปเวอร์ชันต่างๆ ในอุปกรณ์ที่ใช้ Android 13
อุปกรณ์ที่ใช้ Android 13 |
แอปที่กำหนดเป้าหมายเป็น Android 13 ขึ้นไป |
แอปที่กำหนดเป้าหมายเป็นเวอร์ชันที่ต่ำกว่า Android 13 |
การติดตั้งใหม่
|
ระบบจะบล็อกการแจ้งเตือนจนกว่าแอปจะแสดงข้อความแจ้ง
แอปจะควบคุมเวลาที่จะขอสิทธิ์
|
ระบบจะบล็อกการแจ้งเตือนจนกว่าระบบปฏิบัติการจะแจ้ง
ระบบจะขอสิทธิ์เมื่อเรียกใช้แอปครั้งแรก
|
แอปที่มีอยู่ (อัปเกรด)
|
อนุญาตให้แสดงการแจ้งเตือนจนกว่าแอปจะแจ้ง
ระบบจะมอบสิทธิ์ชั่วคราวจนกว่าแอปจะขอสิทธิ์ในการเรียกใช้ครั้งแรก
|
อนุญาตให้แสดงการแจ้งเตือนจนกว่าระบบปฏิบัติการจะแจ้ง
ระบบจะมอบสิทธิ์ชั่วคราวจนกว่าแอปจะทำงานเป็นครั้งแรก
|
หลักเกณฑ์การใช้งาน
สําหรับการใช้งานอ้างอิง โปรดดูบริการการแจ้งเตือน บริการสิทธิ์ และบริการนโยบาย หากต้องการใช้ข้อยกเว้นสำหรับตัวแฮนเดิลสิทธิ์เริ่มต้น โปรดดูสิทธิ์รันไทม์
ในระหว่างการติดตั้งใช้งาน ให้ใช้หลักเกณฑ์ต่อไปนี้เกี่ยวกับลักษณะการแจ้งเตือนผู้ใช้สำหรับแอปที่กำหนดเป้าหมายเป็น SDK ของ Android 13 หรือต่ำกว่า
- แอปที่เพิ่งติดตั้งในอุปกรณ์ Android 13 ต้องไม่ส่งการแจ้งเตือนหากผู้ใช้ไม่อนุมัติข้อความแจ้งสิทธิ์
- หากแอปกำหนดเป้าหมายเป็น Android เวอร์ชัน 13 ขึ้นไป คุณต้องบล็อกการแจ้งเตือนจนกว่าแอปจะแสดงข้อความแจ้ง เนื่องจากแอปจะเป็นผู้ควบคุมว่าจะขอสิทธิ์จากผู้ใช้หรือไม่และเมื่อใด
- หากแอปกำหนดเป้าหมายเป็นเวอร์ชันที่ต่ำกว่า Android 13 คุณต้องบล็อกการแจ้งเตือนจนกว่าระบบปฏิบัติการจะแจ้ง ระบบปฏิบัติการต้องแสดงข้อความแจ้งสิทธิ์เมื่อแอปทำงานเป็นครั้งแรก
แอปที่มีอยู่ในอุปกรณ์ก่อนการอัปเกรดเป็น Android 13 หรือแอปที่กู้คืนผ่านการสำรองข้อมูลและการกู้คืนต้องได้รับอนุญาตให้ส่งการแจ้งเตือนจนกว่าผู้ใช้จะเปิดกิจกรรมจากแอปนั้นเป็นครั้งแรก
สําหรับแอปที่กำหนดเป้าหมาย SDK ของ Android เวอร์ชัน 13 ขึ้นไป หากผู้ใช้ไม่ได้ปรับแต่งการตั้งค่าการแจ้งเตือนสำหรับแอปนี้ในระดับแอปหรือ NotificationChannel
มาก่อน ให้เพิกถอนการให้สิทธิ์ชั่วคราว จากนั้นแอปต้องขอสิทธิ์จากผู้ใช้ก่อนจึงจะส่งการแจ้งเตือนได้
หากแอปที่อัปเกรดซึ่งกำหนดเป้าหมายเป็น Android 13 ยังไม่มีสิทธิ์การแจ้งเตือนผ่านการให้สิทธิ์การอัปเกรดชั่วคราว และผู้ใช้เปิดแอปอย่างน้อย 1 ครั้ง แอปต้องแสดงข้อความแจ้งสิทธิ์การแจ้งเตือนก่อนจึงจะอนุญาตให้เรียกใช้บริการที่ทำงานอยู่เบื้องหน้าได้
สําหรับแอปที่มี SDK เป้าหมายเวอร์ชันต่ำกว่า Android 13 ให้ขัดขวางการเริ่มกิจกรรมครั้งแรกหลังจากที่แอปสร้าง NotificationChannel
อย่างน้อย 1 รายการเพื่อแสดงข้อความแจ้งสิทธิ์ที่ถามว่าผู้ใช้ต้องการรับการแจ้งเตือนจากแอปหรือไม่
หากก่อนหน้านี้ผู้ใช้ปรับแต่งการตั้งค่าการแจ้งเตือนที่ระดับแอปหรือ NotificationChannel
สำหรับแอปในอุปกรณ์ที่อัปเกรดหรือในการสำรองข้อมูลที่จะกู้คืนลงในอุปกรณ์ จะต้องย้ายข้อมูลการตั้งค่าระดับแอปไปยังระบบสิทธิ์ที่มี Flag FLAG_PERMISSION_USER_SET
และต้องไม่แสดงข้อความแจ้งสิทธิ์การแจ้งเตือนเพิ่มเติมต่อผู้ใช้ เว้นแต่แอปจะขออนุญาตดังกล่าวโดยเฉพาะ
การสำรองและกู้คืนข้อมูลต้องมีความเข้ากันได้แบบย้อนหลังและแบบไปข้างหน้าระหว่างอุปกรณ์ Android 13 กับอุปกรณ์จากระบบปฏิบัติการเวอร์ชันก่อนหน้า ข้อมูลสำรองที่สร้างจากอุปกรณ์ Android 13 ต้องกู้คืนลงในระบบปฏิบัติการเวอร์ชันเก่า และข้อมูลสำรองจากระบบปฏิบัติการเวอร์ชันเก่าต้องกู้คืนลงในอุปกรณ์ Android 13
การแจ้งเตือนสื่อที่เชื่อมโยงกับการเล่นสื่ออย่างต่อเนื่องต้องได้รับการยกเว้นจากสิทธิ์การแจ้งเตือน
ตรวจสอบการเปลี่ยนแปลงในระบบการแจ้งเตือนและสิทธิ์
เรียกใช้การทดสอบต่อไปนี้เพื่อตรวจสอบการติดตั้งใช้งาน
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-07-27 UTC
[[["เข้าใจง่าย","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 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/)."]]