Search code examples
androidnotificationsalarmmanagerandroid-permissionsandroid-pendingintent

How do I schedule a notification in Android 13?


Previous questions on Stack Overflow do not account for Android 13's new permission model, hence I am opening a new question.

My app currently uses an AlarmManager to set a notification for a user at a pre-defined point in the future. Think of this as a 'reminder' before an event happens, at a period defined by the user (e.g 0/1/5/15/30 minutes before the event)

In Android 13, there are 2 new permissions I now need to think about:

  • SCHEDULE_EXACT_ALARM – To schedule a PendingIntent to wake up the device to deliver a notification. The reminder/notification is a secondary feature in my app, which I why I do not use the other permission (USE_EXACT_ALARM)
  • POST_NOTIFICATIONS – To actually show the notification to the user.

Does this mean that for new users to my app, I will now need to send them through two separate permission acceptance flows?

Specifically, for POST_NOTIFICATIONS a runtime dialog to accept or deny notifications AND for SCHEDULE_EXACT_ALARM an Intent to the user's Settings app to allow alarms & reminders (so that I can schedule notifications in the future).

Two separate permission flows feels sub-optimal. Am I reading this correctly, or is there a more user-friendly way to schedule notifications in the future?

Relevant documentation is here:


Solution

  • For this scenario, I ended up building a permission acceptance flow in my app.

    In short, my app checks whether both SCHEDULE_EXACT_ALARM and POST_NOTIFICATIONS have been granted by the user. If one of them hasn't been granted, the user is not able to schedule notifications and I automatically send the user through a permission setup experience.

    In Android 13, this isn't too bad because SCHEDULE_EXACT_ALARM is automatically granted to the user (although they can of course disable this, so the app still needs to check for this permission), so the user just has the POST_NOTIFICATIONS permission to grant.

    I expect in Android 14+ that the SCHEDULE_EXACT_ALARM will not be automatically granted. This is frustrating for this scenario, because unlike POST_NOTIFICATIONS which has a nice runtime dialog, SCHEDULE_EXACT_ALARM involves deep linking the user out of my app into Android Settings. This app switch is a suboptimal experience.

    I have attached some images of how I approach this in my app. If there is a better way to achieve this, I would be open to ideas, but this has solved this issue for the moment.

    1: Permission Setup Screen 2: Grant Permission: POST_NOTIFICATIONS
    Permission Setup Screen POST_NOTIFICATIONS Grant
    3: Grant Permission: SCHEDULE_EXACT_ALARM 4: All Permissions Ready
    SCHEDULE_EXACT_ALARM Grant All Permissions Ready