Prakash Maharjan
3 min readAug 31, 2020

--

Testing Push Notification on iOS simulator (Swift)

We can test push notification on simulator from Xcode 11.4 and later

There are different ways to do so. I am going to show one of the easy way to do. Follow the steps below:

A. First setup push for the app

  1. Go to Xcode and create a new project and initialise the push notification configuration in AppDelegate.swift file on didFinishLaunchingWithOptions function as below:

2. After that build and run project on simulator once for allowing push.

3 Allow push permission by tapping Allow.

Thats all for the app part.

B. Creating .apns file

Now, in order to test push notification on simulator, we need a file with .apns extension. So lets create that too.

  1. Open up Terminal and in your desired folder type the following command and hit enter.

nano payloadsample.apns

I am on my desktop folder of my mac.

Here nano is the command and payloadsample.apns is the file which we are going to create which have .apns extension.

2. After hitting enter you will get following screen.

So here new file is created and we will have to write the payload in this file. You can write any standard correct format payload sample here.

3. I am just typing basic one here as below:

Here, it is just a simple payload but it should also contain

“Simulator Target Bundle” key with value as your app bundle identifier at top.

Here I have set my exact same app bundle identifier (com.pm.SimulatorPushNotificationTesting) in my test project target.

Check your project target and make sure it is same.

If you do not wan’t to type on terminal, you can exit without writing payload and just open the file from Xcode or any other editor to edit and then save.

4. Now press Control + X key to exit. It will ask for saving changes. Press Y and hit enter.

5. Now go to the folder you created the file, in my case its desktop. You can see there is a file with .apns extension there.

6. Just drag that file and drop on simulator. You will get push notification.

You can log the push payload response from did receive response delegate function in AppDelegate.swift file

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

let response = response.notification.request.content.userInfo

print(“response is : \(response)”)

}

You can find sample Xcode project file and .apns file here for quickly testing purpose on my GitHub:

https://github.com/PrakashMaharjan/PushTestOnSimulatorSampleSwift.git

Just run the project on simulator, Allow the push notification and drag and drop the any one of .apns file on simulator to see the push.

Happy Coding!!!

--

--

Prakash Maharjan

Result oriented freelance iOS Developer (Swift & Objective C), passionate about app development. Available for freelance ios app development works.