How to Test Push Notifications in the iOS Simulator

How to Test Push Notifications in the iOS Simulator

Written by

in

Quick answer: Yes — since Xcode 11.4 you can deliver a push payload to an app running in the Simulator with xcrun simctl push booted <bundle-id> payload.apns, or by dragging the payload file onto the Simulator window. What the Simulator cannot do is talk to APNs, so real device tokens, silent background pushes and force-quit delivery still need a physical device.

Why this happens

For a long time the correct answer was “no”. The Simulator had no APNs connection, so registerForRemoteNotifications() failed and nothing ever arrived. A lot of advice still repeats that, which is why the question keeps coming back.

Since Xcode 11.4 the Simulator can display a push notification, but it does it locally: simctl reads a JSON payload from disk and hands it to the app’s notification system directly. No Apple server is involved. That means the payload format, the permission prompt, the foreground presentation and the tap handling are all real and worth testing — but the network path, the device token and background wake-ups are not.

The second half of the question — registering for push while the app is terminated — is a separate misunderstanding. Once iOS has terminated your process, no code of yours runs until the user (or the system) launches it again. Registration has to happen at launch or when the user changes a setting, never after termination.

How to fix it

1. Register and ask for permission at launch

Set the notification delegate before the app finishes launching, otherwise taps that cold-start the app are delivered before your delegate exists and get dropped.

import UIKit
import UserNotifications

@main
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {

    func application(_ application: UIApplication,
                     didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        let center = UNUserNotificationCenter.current()
        center.delegate = self

        center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, _ in
            guard granted else { return }
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
        }
        return true
    }

    func application(_ application: UIApplication,
                     didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        let token = deviceToken.map { String(format: "%02x", $0) }.joined()
        print("APNs token: \(token)")
    }

    func application(_ application: UIApplication,
                     didFailToRegisterForRemoteNotificationsWithError error: Error) {
        print("Registration failed: \(error.localizedDescription)")
    }
}

registerForRemoteNotifications() must be called on the main thread, which is why it is hopped back with DispatchQueue.main.async.

In a SwiftUI app, adopt the same delegate with @UIApplicationDelegateAdaptor(AppDelegate.self) var appDelegate on your App struct.

Treat the token printed in the Simulator as a placeholder. It is not something you can hand to APNs and use to send a real push — token handling is a device test.

2. Write a payload file

A minimal payload that produces a visible banner:

{
  "Simulator Target Bundle": "com.example.myapp",
  "aps": {
    "alert": {
      "title": "Order shipped",
      "body": "Your package is on the way."
    },
    "sound": "default",
    "badge": 1
  }
}

Save it as payload.apns (the extension is a convention; simctl only cares that the contents are JSON). The Simulator Target Bundle key is a Simulator-only convenience — it is ignored by real APNs, so keep it out of anything you send to a device.

3. Deliver it with simctl

Find the booted device, then push:

xcrun simctl list devices booted
xcrun simctl push booted com.example.myapp payload.apns

If the payload contains Simulator Target Bundle, you can drop the bundle identifier argument:

xcrun simctl push booted payload.apns

The app must already be installed on that specific Simulator and the bundle identifier must match exactly — simctl reports an error otherwise. xcrun simctl help push lists the accepted forms.

The alternative is drag and drop: with the Simulator booted, drag the payload file onto the Simulator window. This is handy when you are iterating on copy and don’t want to keep a terminal open.

4. Handle foreground display and taps

A notification that arrives while your app is frontmost is silent unless you return presentation options:

extension AppDelegate {

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                willPresent notification: UNNotification,
                                withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
        completionHandler([.banner, .sound, .badge])
    }

    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                didReceive response: UNNotificationResponse,
                                withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
        print("Opened from notification: \(userInfo)")
        completionHandler()
    }
}

Test both states: app in the foreground (banner appears because of willPresent), and app in the background (system banner, tap routes through didReceive).

5. Test the terminated-app case properly

Quit the app from the app switcher, push a payload, then tap the notification. The app cold-starts and didReceive fires — provided the delegate was assigned in didFinishLaunchingWithOptions, as in step 1.

What you cannot do is register after termination. If you want registration to follow a user preference, do it when the toggle changes, and check the current state first so you don’t re-prompt:

UNUserNotificationCenter.current().getNotificationSettings { settings in
    guard settings.authorizationStatus == .notDetermined else { return }
    // safe to call requestAuthorization here
}

If you need to see the permission alert again, delete and reinstall the app or erase the Simulator — iOS only asks once.

6. Know what still needs a real device

  • A real APNs round trip, including the sandbox versus production environment and the aps-environment entitlement.
  • Device tokens you actually send to your server.
  • Silent (content-available) pushes and background fetch — the Simulator does not reproduce APNs background delivery or its throttling.
  • Delivery to an app the user has force-quit, which iOS deliberately blocks for background pushes.
  • Notification Service and Notification Content extensions under real payload sizes and timing.

How to prevent it

  • Keep a checked-in payload.apns per notification type so the whole team pushes identical test data.
  • Assign UNUserNotificationCenter.current().delegate in didFinishLaunchingWithOptions, never later.
  • Match bundle identifiers exactly; a stale identifier is the most common reason simctl push fails silently in your head but errors in the terminal.
  • Test the tap path, not just the banner — most push bugs are in didReceive.
  • Run the same payload against a device on the sandbox environment before shipping.

FAQ

Does the Simulator need an APNs certificate or key?

No. simctl push bypasses APNs entirely and injects the payload locally, so certificates, provisioning profiles and the aps-environment entitlement play no part in Simulator testing.

Why does nothing appear when I run simctl push?

Check three things in order: the device is booted (xcrun simctl list devices booted), the app is installed on that device, and the bundle identifier matches. If the app is installed but in the foreground, make sure willPresent returns presentation options.

Can I register for push notifications after the app has been terminated?

No. A terminated process runs no code. Register at launch, or at the moment the user enables notifications in your settings screen, and let iOS persist the result.

Sources & credits

Parts of this guide are adapted from content licensed under CC BY-SA 4.0; this article is shared under the same license.