As iOS developers, we have always faced the pain of testing the APNS (Apple Push Notification Services) only on actual iOS devices. Well, not anymore from Xcode 11.4 beta, because now we can test APNS on simulators also.
We’ll directly dive into the action of “how-to”. So follow my lead :
- Have Xcode 11.4 beta(latest available now) downloaded and installed from here.
- Add push notifications to capabilities in Xcode target’s signing & capabilities section.
- Have UserNotifications code setup in your “Appdelegate” file :
4. Now we need to create our own APNS payload file. Here’s how –
- In Xcode, go to File > New > File.
- Select “Notification Simulation file” under Apple watch section.
- Save this file on your preferred location. This file contains a default payload, which we’ll replace with our own payload JSON.
- You can learn more about creating & customizing the payload file here.
5. Payload :
Here’s the important part, you need to have the key “Simulator Target Bundle” in your payload with value as your App’s bundle id.
Now let’s test our implementations, time to catch your notification on the simulator.
There are two ways by which you can push your payload file as notification to your simulator.
i) Using Drag-drop : For this just drag-drop your payload file onto your simulator and you’ll receive notification in your simulator.
ii) Using Terminal: For this, first retrieve your simulator device identifier from Xcode device & simulators.
For that you can navigate like Xcode > Window > Device&Simulators > Simulator tab > rightClick on your selected device simulator > Copy identifier. Then open the terminal and enter below command :
xcrun simctl push <simulatorDeviceIdentifier> <pathTopayloadfile>
That’s it. Now just customize your payload file as you need and test APNS on the simulator without having any iOS device.