How to test Push Notifications on a simulator

Let's see how to test push notifications on the simulator, let's understand what apns file

Help in our Twitter Community for iOS developers

Reads in 7 minutes Updated 15 days ago

Before testing push-notifications on the simulator, you need to get permission from the user. How to request permission is described at the end of the tutorial. You can test both regular and Rich-notifications, which are notifications with pictures, sounds and action-buttons.

Apple Push Notification Service-server sends a notification content file to devices. To test push notifications, you can simulate this request

You can do this through a json-file with data, or through a terminal.

Drag and drop json-file

Create a file with the data for the push. Here I will add text, sound and number to the application icon badge:

{
   "aps" : {
      "alert" : {
         "title" : "Game Request",
         "body" : "Bob wants to play poker"
      },
      "badge" : 9,
      "sound" : "bingbong.aiff"
   }
}

You can specify more content, such as a picture or actions. All available keys for push notifications at the link.

Now you need to add Simulator Target Bundle to the file, so that the simulator understands which target is getting a push:

{
   "aps" : {
      "alert" : {
         "title" : "Game Request",
         "body" : "Bob wants to play poker"
      }
   },
   "Simulator Target Bundle": "com.bundle.example"
}

If the bundle is not specified, you will get this error:

 Error because you did not specify a Target Bundle
Error because you did not specify a Target Bundle

If all is well, a push will appear on the simulator:

 Push notification
Push notification

Through Terminal

In this method you also use the APNS-file, but you pass it through the terminal. Check in the Xcode settings that Command Line Tools is set, otherwise simctl will give an error. If you can't see the path at the bottom, select the Xcode version again:

 Turn on Command Line Tools
Turn on Command Line Tools

The command is used to send a push:

xcrun simctl push <id simulator> <bundle id> <path to apns file>

The Bundle id is the bundle of your application. And to find out the id simulator the command is used:

xcrun simctl list

It will show a list of all simulators and their id. Note that a running simulator will have Booted:

 List of all available simulators
List of all available simulators

Collect the command with the id simulator and call it:

xcrun simctl push 4D1C144E-7C68-484D-894D-CF17928D3D3A com.bundle.example payload.apns

If you have a simulator running, you can specify Booted instead of the key, so the push will automatically fly to the running simulator.

If everything is done correctly, you will get this message:

 Message about sending a push-notification
Message about sending a push-notification

Permission

In order for push-notifications to be shown on the simulator and the device, you need to request permission. You can do this manually or via our library.

Permission request

Import UserNotifications and invoke the system query:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(granted, error) in
   print("Permission Granted: \(granted)")
}

Requests need to be made anywhere before notices are sent. This is roughly what our library does PermissionsKit :

import PermissionsKit

Permission.notification.request {}

Permission reset

If you need to reset the permission for push-notifications, all you need to do is uninstall the app

Sometimes the permission may remain even after reinstalling, then after uninstalling wait a minute and install again
We try to translate the tutorial well, but we aren't native. We appreciate if you can submit a Pull Request with fixes