Push Notification in Simulator iOS

Tram Ho

One feature that iOS developers have been waiting for is a test push notificaiton by simulator. The feature can be said to have existed for a long time in Android. Today I would like to introduce how to set up to test push notification on the simulator.

I. Steps.

  1. Download Xcode 11.4 beta now and install it.
  2. Create a project that allows you to receive push notificaiton.
  3. Run the app and enable push notification permissions
  4. Create an Apple Push Notification Service (APNS) file
  5. Drag and drop the above file into the simulator.

OK now, we’ll go through the steps above.

1. Download the Xcode 11.4 beta now and install it.

Please go to the following link: https://developer.apple.com/download/more/ to download xcode and install.

2. Create a project that allows you to receive push notificaiton.

In the AppDelegate.swift file, you import the UserNotifications framework. Next in the application (_: didFinishLaunchingWithOptions 🙂 add the following code to request permission to receive push notificaiton.

3. Run the app and enable push notification permissions

4. Create the Apple Push Notification Service (APNS) file

The APNS payload file is a json file that contains information about push notifications. You can find out about it in Apple’s documentation.

First to create the payload file, we need to get bundle ID from Xcode to create APNS file.

Now I will create the payload file, the creation is very simple, you add the following code to textEdit or Sublime Text. When saving, remember to rename the file to a file ending in .apns Example: “abcz.apns”

Note: Remember to change the value of Target Target Bundle to bundleID of the project you create.

OK, so you have easily tested the push without the need for a real machine as well as the need for a cetificate push to test it. If you want to test push in foreground, you need to install more code ^ _ ^.

Now, I will show you how to test push using a terminal.

II. Push notification by termial.

First you need the identifier of the simulator. In Xcode you press the key combination: Shift + ⌘ + 2 to open “Devices and Simulators” .

Note : Note that if you install multiple Xcode you need to install the correct Xcode 11.4 beta that you need to test. In the termial you type the command in the following structure:

In my computer, I will use the following command:

III. Push notification when app is in foreground. ****

In the Appdelegate.swift file, add the following code:

AppDelegate class you adopt the UNUserNotificationCenterDelegate class

In the application method (_: didFinishLaunchingWithOptions 🙂 you add the following code:

OK Now, test the push again when the app is in foreground.

IV. References :

https://medium.com/swlh/simulating-push-notifications-in-ios-simulator-9e5198bed4a4

Share the news now

Source : Viblo