Send push notifications to iOS Simulator

As you know, when implement push notifications on iOS Application and run the app on simulator, you’ll receive this classic error in console:

Remote notifications are not supported in the simulator

But is really true?


Answer to the question above:

Create a payload

Just for example use this simple payload or create a new one:

{
  "aps": {
    "alert": {
      "title": "Ciao Ciccio",
      "body": "Let's push on macOS! 🎉",
      "sound": "default"
    },
    "badge": 999
  },
  "Simulator Target Bundle": "com.yourapp.bundle"
}

Remember to use the “Simulator Target Bundle” key with the bundle id of your app.


Send push

Now you have two ways to push locally:

  1. BEGINNER: Run your app (on simulator) and drag this payload.json directly on simulator: magic is done, a push notifications (with all the callbacks called) is shown on screen.
  2. PRO: Open terminal and use the “xcrun simctl push” command with the following parameters:
xcrun simctl push #deviceId# #path/payload.json#

Where #deviceId# is the id of your running simulator. (You can find it in the Xcode’s “Device & Simulators” window, right click on simulator -> Copy Identifier).

And last parameter is the payload file path.

Result:

[~/Desktop]$ xcrun simctl push XXXXXXX-XXXX[...]X payload.apns
 Notification sent to 'com.yourapp.bundle'

Personally I prefer this second options. You can also use this script in a pipeline while running unit tests / UI tests.

Enjoy pushing!

 

Alberto Pasca

Software engineer @ Pirelli & C. S.p.A. with a strong passion for mobile  development, security, and connected things.