Catching APNS on your Xcode Simulator

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 :

  1. Have Xcode 11.4 beta(latest available now) downloaded and installed from here.
  2. Add push notifications to capabilities in Xcode target’s signing & capabilities section.
  3. Have UserNotifications code setup in your “Appdelegate” file :
import UIKit
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) {
[weak self] granted, error in
print("Permission granted: \(granted)")
guard granted else { return }
UNUserNotificationCenter.current().getNotificationSettings { settings in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
return true
}
view raw AppDelegate.swift hosted with ❤ by GitHub

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.

{
"aps": {
"alert": {
"title": "APNS For Simulator",
"body": " Eureka! APNS received in simulator 🎉",
"sound": "default"
},
"badge": 1
},
"Simulator Target Bundle": "com.myAppBundleId.identifier"
}
view raw apnsPayload.json hosted with ❤ by GitHub

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.

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.