Skip to content
Menu

Push notifications

Permission prompts

Markdown version

When to ask for notification permission, how to ask on iOS and on Android 13 and later, provisional authorization, and how the SDK records the answer.

Both iOS and Android 13 and later require the user's permission before an app can show notifications. You usually get one chance at the system prompt, so the moment you ask matters more than the code.

When to ask

  • Ask after the user sees the value. For Lumen, that is after the first workout_completed, with a line like "Want a reminder before tomorrow's session?". A prompt at first launch, before the user knows the app, is easy to refuse.
  • Explain first, in your own UI. Show a short screen or sheet that says what you will send and how often, with buttons for yes and not now. Show the system prompt only after yes. Saying not now to your screen costs nothing; refusing the system prompt is hard to undo.
  • Use an in-app message as the explanation. An in-app message with a button that deep links to your own route (for example lumen://enable-reminders) can trigger on an event and lead into the system prompt. See In-app messages.

Ask on iOS

requestPushAuthorization() asks for alerts, sounds and badges. When the user allows it, the SDK registers with APNs and records the push subscription as opted in once the token arrives. When the user declines, it records the subscription as unsubscribed. It returns whether permission was granted.

swift
struct ReminderPrompt: View {
    var body: some View {
        Button("Remind me before workouts") {
            Task {
                let granted = await Herald.shared.requestPushAuthorization()
                if !granted { /* show how to turn notifications on in Settings */ }
            }
        }
    }
}

iOS shows the system prompt once. Later calls return the user's earlier answer without a prompt. To send a user who declined to your app's notification settings:

swift
if let url = URL(string: UIApplication.openNotificationSettingsURLString) {
    UIApplication.shared.open(url)
}

Provisional authorization

Provisional authorization delivers notifications quietly to Notification Center without a prompt, and lets the user keep or turn them off from there. requestPushAuthorization() does not request it. To use it, ask the system yourself and record the result:

swift
@MainActor
func requestQuietNotifications() async {
    let center = UNUserNotificationCenter.current()
    let granted = (try? await center.requestAuthorization(options: [.alert, .sound, .badge, .provisional])) ?? false
    if granted { UIApplication.shared.registerForRemoteNotifications() }
    Herald.shared.setSubscription(.push, granted ? .optedIn : .unsubscribed)
}

The device data then reports provisional as the notification permission.

Ask on Android 13 and later

Android 13 (API level 33) added the POST_NOTIFICATIONS runtime permission. herald-core declares it in its manifest, so you do not add it yourself. On Android 12 and earlier, notifications are allowed by default and there is no prompt.

Herald.requestPushAuthorization(activity) shows the prompt on Android 13 and later when the permission is not granted yet, and records the answer as the push subscription: opted in when the user allows it, unsubscribed when they decline. It reads the answer when your activity resumes after the prompt closes, so you do not forward onRequestPermissionsResult. On Android 12 and earlier, or when the permission is already granted, it records whether notifications are enabled for the app, without a prompt. Call it from the main thread.

kotlin
class SettingsActivity : ComponentActivity() {
    fun onEnableRemindersTapped() {
        Herald.requestPushAuthorization(this)
    }
}

An opt-in is held on the device until the FCM token exists, so Herald receives the token before the opt-in. An opt-out is sent at once.

If you prefer the Activity Result API, request the permission yourself and record the answer with setSubscription:

kotlin
class SettingsActivity : ComponentActivity() {
    private val askNotifications = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted ->
        Herald.setSubscription(
            SubscriptionChannel.PUSH,
            if (granted) SubscriptionState.OPTED_IN else SubscriptionState.UNSUBSCRIBED,
        )
    }

    fun onEnableRemindersTapped() {
        if (Build.VERSION.SDK_INT >= 33) askNotifications.launch(Manifest.permission.POST_NOTIFICATIONS)
    }
}

The SDK only knows the prompt was shown when requestPushAuthorization showed it. If you ask yourself and the user declines, the notification permission in device data stays not_determined rather than denied; the push subscription you record is what Herald acts on.

Subscription state

Herald tracks push consent in two places:

SignalSourceWhat it means
Push subscriptionsetSubscription(.push, …), or requestPushAuthorizationUnsubscribed suppresses push to this device's tokens. Subscribed or opted in lifts that suppression.
Notification permissionDevice data, context.push.authorization, sent with every batchWhat the operating system currently allows: authorized, denied or not_determined on both platforms, plus provisional and ephemeral on iOS. Android 13 and later reports not_determined until the app has asked through requestPushAuthorization; Android 12 and earlier report authorized unless notifications are turned off.

A device with a registered token can receive push without any subscription event. Users can also turn notifications off in system settings at any time, which no SDK call reports as it happens; the notification permission in the next batch reflects it. Notification permission is second-tier device data, so it is not sent in minimal mode.

Verify it works

  1. Install the app fresh, trigger your prompt and allow notifications.
  2. With debug logging on, confirm the subscription and push token batch is accepted (no Herald rejected a batch line).
  3. Decline on a second fresh install and confirm the app shows your path to Settings.

Troubleshooting

  • The iOS prompt never appears. The user already answered. Delete the app to reset the answer on a test device.
  • The Android prompt never appears. The device runs Android 12 or earlier, or the app targets an SDK level below 33, in which case Android shows the prompt on its own when you create the first channel.
  • Push still arrives after the user opted out in your app. Call setSubscription(.push, .unsubscribed) (iOS) or setSubscription(SubscriptionChannel.PUSH, SubscriptionState.UNSUBSCRIBED) (Android) when the user turns off notifications in your own settings screen.

Next

See Herald decide live →