Logo for tanaschita.com

Testing remote iOS push notifications in a simulator with simctl

Learn how to use the command-line tool simctl to simulate remote notifications in iOS.

25 Apr 2022 · 2 min read

When working with remote push notifications in iOS, testing is not always an easy task. We may not have any access to the server that will send the notifications and since remote notifications are not supported in the simulator, a device is needed to successfully receive a device token required to receive push notifications.

Nevertheless, we have the possibility to simulate a push notification in the simulator with the command-line tool called simctl.

Let's look at how to do that.

Creating and storing an .apn file

The first step to simulate a push notification in a simulator is creating and storing an .apn file. The file contains the JSON notification payload that the app would otherwise receive from the server.

test.apn

{
"aps": {
"alert": "Hello world",
"sound": "default",
}
"categoryId": "1234"
}
Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Simulating push notification with simctl command-line tool

simctl is a command-line tool that provides commands to use and configure an iOS simulator. With Xcode installed, simctl is accessible through the xcrun command.

The easiest way to send a simulated notification is by following these steps:

  1. Run the app that supports and handles push notifications in a simulator
  2. Open the terminal
  3. Change to the directory where the test.apn file is stored
  4. Run xcrun simctl push booted com.tanaschita.ExampleApp test.apn. Replace com.tanaschita.ExampleApp with your app bundle identifier.

After the last step, the notification should pop up in the simulator.

Simulated notification in simulator.
Simulated notification in simulator.

With xcrun simctl push booted com.tanaschita.ExampleApp test.apn, we told simctl to send a test notification to the currently running (booted) simulator.

With that setup, we now can test different scenarios by changing the content in the test.apn file, for example to test the handling of actions or deep links from the notification.

For different iOS notification payload configuration possibilities, check out the official payload key reference.

Sponsorship logo
Preparing for a technical iOS job interview
Check out my new book on preparing for a technical iOS job interview with over 200 questions & answers. Test your knowledge on iOS topics such as Swift & Objective-C, SwiftUI & UIKit, Combine, HTTP Networking, Authentication, Core Data, Concurrency with async/await, Security, Automated Testing and more.
LEARN MORE

Newsletter

Image of a reading marmot
Subscribe

Like to support my work?

Say hi

Related tags

Articles with related topics

notifications

swift

ios

Developer guide on push notifications for iOS

Learn how to setup and handle remote push notifications.

21 May 2023 · 5 min read

Latest articles and tips

© 2023 tanaschita.com

Privacy policy

Impressum