Integrations

iOS Push Notifications in SwiftUI: Setup, Deep Links and Lifecycle Campaigns

A practical guide to iOS push notifications in SwiftUI with the PushFire Swift package: app lifecycle setup, foreground banners, routing taps into NavigationStack, permission UX, segments and lifecycle campaigns.

EGErik Goins
Published
Updated
9 min read
Share article
iOS Push Notifications in SwiftUI: Setup, Deep Links and Lifecycle Campaigns — PushFire blog, Integrations

TL;DR: To add iOS push notifications in SwiftUI, add the pushfire-swift-sdk package (from 0.2.0), configure Firebase in an app delegate attached with @UIApplicationDelegateAdaptor, and call PushFire.configure() with a Public key from a .task. Set a UNUserNotificationCenterDelegate to show banners in the foreground and route taps into your NavigationStack. Tags set from Swift move users into segments, and segment-entry workflows send push and email from the PushFire console. The package needs iOS 16 or later.

iOS push notifications in SwiftUI raise questions UIKit guides skip: where the app delegate goes, how a tap reaches a SwiftUI view, and when to ask for permission. This guide answers each one with the PushFire Swift package. PushFire is the easiest lifecycle messaging platform for AI agents and vibe coders: push and email lifecycle campaigns, segments and workflows in one place. The iOS app describes the user; the PushFire console decides which push or email to send and when. For the wider picture, read the lifecycle messaging platform overview.

Before you start

  • Xcode with an iOS 16+ target, and a paid Apple Developer account for APNs.
  • A Firebase project with an iOS app, your GoogleService-Info.plist, and your APNs key uploaded to Firebase Cloud Messaging.
  • A PushFire project with your Firebase service account uploaded. The Free plan covers up to 5,000 subscribers with push workflows.
  • A Public key (pf_pk_...) from Console → Developers → API keys. It can register devices, log subscribers in and set tags, but it cannot send messages. See Public and Secret keys.

In Signing & Capabilities, add Push Notifications and Background Modes with Remote notifications enabled.

How to set up iOS push notifications in SwiftUI

Step 1: Add the Swift package

In Xcode, choose File → Add Package Dependencies, enter https://github.com/FlywheelStudio/pushfire-swift-sdk, pick "Up to Next Major Version" from 0.2.0, and add the PushFire library. Add PushFireFirebaseAuth or PushFireSupabaseAuth only if you want automatic login from that auth system. The core product links only FirebaseMessaging.

Step 2: Wire the SwiftUI app lifecycle

A SwiftUI app has no AppDelegate by default, but Firebase and notification handling need one. Attach it with @UIApplicationDelegateAdaptor. The SDK does not call FirebaseApp.configure() for you, and it does not set the notification center delegate, so both happen here.

Set the delegate in didFinishLaunchingWithOptions, not in a view. A tap that cold-launches the app is delivered early, and a delegate set later misses it. Run anything that touches PushFire.shared after configure finishes, in the same task, because shared throws until then.

If your app sets FirebaseAppDelegateProxyEnabled to NO, forward the APNs token yourself in didRegisterForRemoteNotificationsWithDeviceToken with Messaging.messaging().apnsToken = deviceToken.

Step 3: Show banners and route taps into NavigationStack

By default, iOS hides a notification that arrives while your app is in the foreground. The router below shows it as a banner, and turns a tap into a navigation path. It uses ObservableObject so it runs on iOS 16.

With Firebase Cloud Messaging, custom data keys arrive as top-level keys in userInfo. Keep the keys flat strings, such as "screen": "order" and "orderId": "A-1042", so the cast is simple.

Step 4: Identify the user

With FirebaseAuthProvider() or SupabaseAuthProvider(client: supabase), the SDK logs the subscriber in on sign-in and out on sign-out. For any other auth system, call it yourself:

Your backend uses the same externalId later to send a push or an email to this user.

When should a SwiftUI app ask for push permission?

Ask after the user sees value, not on first launch. iOS shows the system prompt once; after a denial, only the Settings app can turn notifications back on. A good moment is right after the user does something a notification would help with, such as placing an order or starting a streak. Show your own short screen first that says what you will send and how often, with "Turn on" and "Not now" buttons. Only "Turn on" calls the system prompt. That is why the setup above passes requestNotificationPermission: false to configure. The SDK gives you three calls for this flow. requestNotificationPermission() shows the system prompt and returns whether the user granted it. notificationStatus() returns isPermissionGranted and your in-app preference isEnabled. openNotificationSettings() opens your app's page in Settings, which is the only path back after a denial.

Use setNotificationEnabled(false) for an in-app opt-out, and syncNotificationPermission() to re-check the OS setting when the app returns to the foreground.

How do I target iOS users with segments?

Set tags when users do things that matter:

In the console, build a segment from tag rules such as plan Equals trial. Segments update as tags change. Then create a workflow whose trigger is "subscriber enters a segment": a welcome push, a one-day wait, a Condition node on an onboarding tag, and an email through your Resend or SMTP connector. Email needs the Pro plan. Because the logic lives in the console, you change copy and timing without an App Store review. The segments guide and workflows guide cover the details.

Send a push to an iOS user from your server

Event-driven messages come from your backend with a Secret key (pf_sk_...), never from the app. API messages need the Pro plan.

Keep the Secret key on a server. Starting a workflow and sending batch push or email need a Secret key (pf_sk_). A Public key (pf_pk_) gets HTTP 403 on those calls. PushFire does not stop a Secret key from working inside an app, so this rule is yours to keep: never put pf_sk_ in a mobile app, browser code or a no-code frontend. Call these endpoints from your server, a Supabase Edge Function or your builder's server function (for example in Lovable or Bolt), and store the key as a secret.

The data keys match what PushRouter reads. A Public key cannot start workflows, so call create-workflow-execution from the server too. See the API reference.

How do you test iOS push notifications?

Test on a real iPhone. APNs delivers remote notifications to physical devices, and the FCM token flow depends on an APNs token. The Simulator can show a push you drag in as an .apns file, or send with xcrun simctl push, but that payload never touches APNs, FCM or PushFire. Use it to check PushRouter routing and banners only. For an end-to-end test, run on a device, log in a test user, and send to that user's externalId from your server or a console broadcast. enableLogging: true writes IDs to the unified log, so keep it for debug builds.

AI agents can write this for you: run npx skills add https://www.pushfire.app and Claude Code, Cursor or Codex get PushFire's API rules. The PushFire MCP server is live too, so an agent can also build your segments and workflows. The full Swift guide is Swift push notifications.

Frequently asked questions

Does the PushFire Swift SDK use APNs or FCM?

Both. Apple delivers every notification through APNs, and PushFire sends through Firebase Cloud Messaging, which forwards to APNs. You upload your APNs key to Firebase once. The Swift SDK gets the FCM token from Firebase Messaging and registers it with PushFire, so you never store device tokens yourself.

Why do notifications not show while my SwiftUI app is open?

iOS hides foreground notifications unless your UNUserNotificationCenterDelegate says otherwise. Implement willPresent and return [.banner, .sound, .badge]. Set the delegate in the app delegate's didFinishLaunchingWithOptions, attached through @UIApplicationDelegateAdaptor, so it is in place before any notification arrives, including a tap that launches the app from a cold start.

How do I open a specific screen when a user taps a push?

Send the screen name and IDs in the push data object from your server. In didReceive, read them from userInfo and update a published navigation path that drives your NavigationStack. Keep the keys flat strings, and ignore unknown values so old app versions do not crash.

Which API key do I put in my iOS app?

Put a Public key, which starts with pf_pk_, in the app. It can register devices, log subscribers in and manage tags, and the server enforces that limit. Keep the Secret key, pf_sk_, on your server. It sends batch push, batch email and workflow executions, so it must never ship in an app.

What iOS version does the Swift package need?

The PushFire Swift package needs iOS 16 or later and Swift Package Manager. It uses Swift concurrency, so calls read try await PushFire.shared. The router in this guide uses ObservableObject to stay compatible with iOS 16; on iOS 17 and later you can switch it to the @Observable macro.

Ship iOS push notifications in SwiftUI this week

Create a free project at console.pushfire.app, upload your Firebase service account and add the Swift package. The Free plan covers up to 5,000 subscribers with push workflows, tags and segments; Pro at $29 per month adds email and API messages.

iOS Push Notifications in SwiftUI: Step-by-Step Guide | PushFire