How to test push notification in Simulator

This is another article from push notification series, So far we have learnt through.

  1. How iOS push notification works?

2. How to configure iOS app to receive push notification?

3. How to generate authentication token/ push notification certificate to send push notification?

4. How to send push notification to real device without server configuration(for testing purposes)?

But, Is there any way to test the push notification in Simulator? Yes, Now we can test the push notification in Simulator as well. This article is going to explain to you how this can be done.

Test push notification in Simulator

Intro

From the Xcode 11.4 onwards apple allows the developers to test the push notification in the simulator with two options. The details can be found in the Xcode 11.4 apple’s release notes

  1. By drag and dropping the APNS file
  2. By using the Xcode command line

Before starting, please make sure your app is ready to receive push notifications, if you want you can check out my previous article.

By drag and drop APNS file

Push Notification .apns file

Step 1

Save this file in your project path which you created already or you can store this anywhere on your machine.

Wait what is Simulator Target Bundle 🤔? This object is used by Apple to verify which app(By checking and verifying by using the bundle Id) you are trying to send the push notification.

Step 2

After saving the file, install the app in the simulator and enable the push notification permission.

Step 3

Drag and drop the previously saved .apns file into the simulator, you will be able to see the notification 🥳.

By Using the Xcode Command Line

Step 1

Create the .apns file in your project file or you can save it anywhere on your machine.

Step2

Open the terminal and navigate to the file which you created.

Step3

Execute this command and see you should be able to see the push notification on your simulator

xcrun simctl push <DeviceID> com.achsuthan.iOSPushNotificationSetup pushnotification.apns

But how to get the simulator’s DeviceID 🧐?.

In order to get the simulator device Id, In the Xcode Window -> Device and Simulator -> Select the simulator which you already installed the app -> Can get the device Id from the identifier.

Get the Simulator’s device Id
Send push notification using Xcode command tool

Important points to take note of 📝

  1. The .apns file should have the JSON data format, we can’t have the last object with a comma(“,”)
  2. Simulator Target Bundle must be your app bundle Identifier
  3. If the simulator not receiving the push notification, make sure you have registered and configured push notification setup in Xcode properly and accept the permission request.
  4. And make sure your app is supporting the foreground push to receive.

Conclusion

We reached the end of the article, from this article we learned how we can send push notifications to the simulator for testing purposes in two different ways.

I hope you guys learned new stuff through this article as well. Let me know if you want me to improve or any questions feel free to add in the comments section. Btw don’t forget to give some claps 👏 which helps me to write more articles 😃.

Where to go from here, next article will be an advanced version of a push notification part 1 it’s all about UNAuthorizationOptions, checkout the article to explore about it.

Check out the GitHub project to get the latest code

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store