Testing Push Notification on the iOS Simulator
I was so excited that when I saw the news about the iOS simulator’s push notification support. We definitely need this. Before all this, we have one way for the testing push notification.
How did we work? First all, we need a physical device and valid a push certificate. And then post an HTTP request to APNS service, after all, drop a push notification to physical devices.
I did not want to talk about the tools that were pushed shortly. 3rd party applications like NWPusher, Pusher, etc.
Imagine that these processes and works are gone.
However, Apple has some good news for all the iOS developers. Xcode v11.4 beta has been released and the best part about this version is that we can now finally test push notifications in the iOS Simulator!
Sending Push Notification From Command-line Tools
Xcode has several command-line tools that allow you to run your tests, builds and other tasks directly from the Terminal.
if you are running iOS 13.4 or newer simulator, all you need to send a push notification is an APNS notification payload. That’s requiring JSON format like below. The JSON format is the same as it would be when you send it from a server to a device
{
“Simulator Target Bundle”: “com.batikan.testpushapp”,
“aps”: {
“alert”: {
“title”: “Push on the simulator”,
“subtitle”: “Test push notification”,
“body”: “This notification will show up in the simulator!”
}
}
}
The only difference is which is the top-level key. This key is needed when you are sending the push notification with the APNS file by drag and drop to Simulator.
Before all, make sure your app has the notification permission. Call the register method below in the didFinishLaunchingWithOptions in the AppDelegate file.
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {// Override point for customization after application launch.registerPushNotification()return true}func registerPushNotification() {UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (isGranted, err) inprint(“registerPushNotification \(isGranted)”)}}
Run the following command from the Terminal and then you catch up the push notification payload :)
test_push_notification.apns require the JSON payload located in the where you run the command folder.
xcrun simctl push booted test_push_notification.apns
Ohh God Absolutely, it’s looking awesome :)
If you don’t want to include your app’s bundle identifier in the payload, you need to specify the bundle identifier of the receiving app in the command.
xcrun simctl push booted <app-bundle-identifier> test_push_notification.apns
Sending Push Notification Without Command-line Tools
If you don’t want to send push notification in the Terminal you have one more way.
This scenario is working with the APNS file by the drag and drop. Make sure your file has APNS extensin and required the “Simulator Target Bundle” top-level key.
Summary
I believe this will now be very easy for developers to develop and test notifications. We don’t need a physical device, APNS service, and Push Certificate or 3rd party push notification tools anymore.
And one more thing last week I released a framework named ENV Gadget.
ENV Gadget is a framework that helps you to easily manage the service end-points, service keys and other constants. For more details please follow the link below.
Thanks for your time :)