Simplified Testing of Push Notifications in iOS Simulator Through Command Line
Overview
To test push notifications more quickly using an iOS simulator, command line with the Xcode Command Line Tool’s ‘simctl’ command is used. `simctl` (Simulator Control) tool enables pushing “fake” notifications onto the device without the need for a physical device, during testing of the application to check the working and distribution of the push notifications.
In order to test push notification using command line we need the following details:
- Simulator Identifier
- Simulator Identifier is the unique identifier of the simulator where you want to test the notification.
- You can get a unique identifier either from XCode or from Command Line Tool.
- To get a unique identifier from Command Line Tool we have to run the command in your terminal `xcrun simctl list devices` to retrieve a unique identifier as shown in below screenshot.
- For example, if you are using an ‘iPhone 15’ simulator then your identifier will be 5D686661-26C8-42C4-ACF7-ABE5F7911458.
- To get the unique identifier from XCode you can follow the steps shown in the below screenshot.
- App Bundle Identifier
- The app’s bundle identifier (e.g., com.example.pushNotification).
- You can find this in your Xcode project settings under the “General” tab as shown in the screenshot below.
- Payload Push Notification (JSON Format)
- Create a Json file containing the code below in the appropriate manner.
- The aps dictionary should have the alert, badge, sound etc. keys as part of its contents.
- Save this as a .json file. For example push_payload.json.
Unset
{
“aps” : {
“alert” : {
“title” : “Test Notification”,
“body” : “This is a push notification test”
},
“badge” : 1,
“sound” : ‘default’
}
}
Location of the Payload File
- The complete file pathway within which your JSON payload is found, for instance, /Users/username/Downloads/push_payload.json.
Note:
- Ensure you have Xcode and its command line tools installed.
- Your app must be properly configured to receive push notifications and the required setup is done for receiving the remote notification with the AppDelegate class.
Now we have collected all the required details for sending push notification using the command line.
Next, employ the subsequent command to emulate the iOS simulator’s push notification:
Unset
xcrun simctl push 5D686661-26C8-42C4-ACF7-ABE5F7911458 com.example.pushNotification Document/push_payload.json
The command is designed to issue the specified payload via command line to the intended iOS simulator. Upon issuing the command, an alert push notification shall be shown at the borders of the television simulator along with sound and will last for a few seconds and then vanish.