Sending Push Notifications to the iOS Simulator

Alex Bartiş
Feb 12 · 4 min read

Xcode 11.4 beta 1 surprised us last week with a long awaited feature: push notifications on the simulator! (standing ovation).

As anyone that ever had to debug push notifications on iOS knows, the process (depending on your backend implementation) is a bit of a hassle. Starting from the fact that a custom back end implementation might not have an easy way of sending push notifications in development/testing/staging environment, to the fact that you need an actual device to receive those notifications on. And as anyone who ever had to work with manual provisioning knows, that might come with it’s own set of headaches.

But problems no more! Both actual issues have been corrected with the latest beta release. You don’t actually need to send them from your backend and you don’t need a physical device anymore.

At this point you’re thinking “Yay! There’s probably a hidden submenu somewhere in the simulator toolbar”, but think again. If we’re used to Simulate Location or Simulate Memory Warning , this is not so straightforward.

For this, we actually need to use xcrun. If that’s something that’s new to you, let’s take a look at the man description:

xcrun provides a means to locate or invoke coexistence- and platform-
aware developer tools from the command-line, without requiring users to
modify makefiles or otherwise take inconvenient measures to support mul-
tiple Xcode tool chains.

The tool xcode-select is used to specify which installation of Xcode is
used, and may be overridden by the DEVELOPER_DIR environment variable.

The SDK defaults to the boot system OS SDK, and can be specified by the
SDKROOT environment variable or the -sdk option (which takes precedences
over SDKROOT).

TL;DR xcrun is a command line tool which allows you to find or run any tool inside Xcode from the command line.

Let’s now take a look on how we can do this, but before you get started, make sure you have Xcode 11.4 beta 1 installed (it would be a good place to mention that Xcode 11.4 will require macOS Catalina — RIP Mojave).

There are 3 ways of doing this, and two of them need us to create a notification file, an apns file, which contains the body of the notification. Apple has a good in-depth tutorial on how to build one, which you should read, but until then, I’m gonna paste the body of one here:

{
"aps" : {
"alert" : {
"title" : "Simulated Notifications!",
"subtitle" : "Hey There",
"body" : "https://github.com/trusk89"
}
}
}

Save it somewhere easy to access, like your desktop, with the apns extension.

First way:

  1. We need to find the current running simulator. For this, open your terminal of choice and run
xcrun simctl list | grep (Booted)

This will output a list of booted simulators. In my case I have

After the simulator name we have the identifier, the long UUID in parenthesis. We need that.

2. Now that we have the apns and the simulator UUID, we just need to run the command

xcrun simctl push <UUID> <AppBundleId> <PathToNotification>

and we should get a successful output

Second way:

  1. Remember the notification we saved on our desktop? Let’s edit it and add the following line containing the app’s bundle identifier

“Simulator Target Bundle”: “<AppBundleId>”

so our file content now is

{
"Simulator Target Bundle": "com.flurry.FlurryTestApp",
"aps" : {
"alert" : {
"title" : "Simulated Notifications!",
"subtitle" : "Hey There",
"body" : "https://github.com/trusk89"
}
}
}

2. Save the changes

3. Drag and drop the file anywhere on the simulator window.

MAGIC!!!

Third way (shameless plug):

I’ve created a simple app available for free here. It’s opensource and you also have a precompiled executable available if you’re not interested in the all the under-the-hood goodies. Just download it, fill in the necesary info and remember to have fun!

Apple blessed us with this feature so we should enjoy it.

Happy coding!

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade