Send A Notification To The Right In App Destination

A notification opens the app, but the app lands on its home screen. The delegate received the tap before the navigation stack existed, and the route was sent to a view that was not ready to receive it.

Keep the route as data until the application can apply it. Receiving a notification response and navigating a scene are two different steps.

Decode only routes your app understands

A notification payload is not a navigation command to execute blindly. Decode a versioned, allowlisted shape into a small Sendable value:

import Foundation
import UserNotifications

struct NotificationRoute: Equatable, Sendable {
    let accountID: UUID
    let documentID: UUID
    init?(userInfo: [AnyHashable: Any]) {
        guard userInfo["routeVersion"] as? Int == 1,
            userInfo["kind"] as? String == "document",
            let account = userInfo["accountID"] as? String,
            let document = userInfo["documentID"] as? String,
            let accountID = UUID(uuidString: account),
            let documentID = UUID(uuidString: document)
        else { return nil }
        self.accountID = accountID
        self.documentID = documentID
    }
}
@MainActor
final class PendingNotificationRoute {
    private(set) var pending: NotificationRoute?
    func receive(_ route: NotificationRoute) { pending = route }
    func takeIfReady(accountID: UUID?, sceneReady: Bool) -> NotificationRoute? {
        guard sceneReady, let pending, pending.accountID == accountID else { return nil }
        self.pending = nil
        return pending
    }
    func discard() { pending = nil }
}

The parser accepts one document route with valid UUIDs. It does not accept arbitrary URLs, class names, or a serialized navigation stack. Expand it with explicit cases when the product gains another destination.

The pending slot intentionally uses a latest-tap-wins policy. If your application needs a queue, define its ordering, size limit, and duplicate behavior. Do not accidentally build an unbounded history of every response received during launch.

Install the delegate before launch finishes

Apple requires assigning the UNUserNotificationCenter delegate before the app finishes launching. Keep the delegate alive; that property is weak.

In a SwiftUI app, bridge through the app delegate lifecycle as needed. Installing the notification delegate from a destination view’s onAppear is too late for a cold-launch response.

In the response callback, distinguish the default open action from custom actions and dismissal. Apple’s notification action guide explains that custom actions can run without bringing the app to the foreground. A background “mark read” action should not force a navigation presentation.

Decode the payload at that boundary, then transfer the typed route to the main-actor coordinator. With the completion-handler delegate API, call the completion handler exactly once after the response has been handled or queued. Do not hold it while waiting for a person to sign in.

Wait for the correct account and scene

The fixture waits for two conditions: a ready scene and a matching signed-in account. A route for account A must not open account B’s document with a coincidentally similar identifier.

After taking the route, resolve the document through the current account’s authorized store. The payload’s ID is only a lookup request. It does not prove the record exists or that the account still has permission to read it.

If the record was deleted or access was revoked, show an unavailable destination with a route back to the app. Do not silently redirect to unrelated content and make the original notification impossible to explain.

For multiple scenes, designate which eligible scene consumes the pending route or explicitly activate the appropriate scene. Broadcasting the same route to every window creates duplicate navigation. The single consumption in this helper is useful only when one coordinator owns that decision.

Clear stale intent deliberately

If the user declines the account switch, discards the notification flow, or signs out, clear the pending route. Otherwise an old tap can unexpectedly navigate after an unrelated later login.

If routing requires an async fetch, take the route into a separate loading state and define retry behavior there. Removing it from this pending slot means one scene has accepted responsibility, not that the document has already rendered.

Repeated delivery and repeated taps also deserve a policy. Navigating to the document already on screen can focus that existing destination rather than push another identical detail view.

September 2026 review: malformed payload rejection, scene readiness, account matching, one-time consumption, and discard tests passed, with an iOS 17 typecheck. APNs delivery and real cold-launch scene activation remain application integration tests.