Push notifications are one of the most popular ways for developers to keep users engaged in their apps. In IOS applications, push notifications can be incorporated by using the Apple Push Notification service (APNs).
X code 11.4 beta is out and the best part about this release for me is that we can finally test push notifications in the IOS Simulator!
“Simulator supports simulating remote push notifications, including background content fetch notifications. In Simulator, drag and drop an APNs file onto the target simulator. The file must be a J SON file with a valid Apple Push Notification Service payload, including the “aps” key. It must also contain a top-level “Simulator Target Bundle” with a string value matching the target application‘s bundle identifier.”
simctl
also supports sending simulated push notifications. If the file contains “Simulator Target Bundle” the bundle identifier is not required, otherwise you must provide it as an argument (8164566):
$ xcrun simctl push <device> com.example.my-app ExamplePush.apns
“
How can we do it
let’s create a new project. I’ve named it TestPushNotifications and the user interface I chose is Storyboard.
Just to make the app look good (though it won’t help…), I’ve added a UILabel
that says “Hello, world!” in View Controller
of Main.storyboard
.
This is where the fun begins! Let’s go to the AppDelegate.swift
file and add the following line below import UIKit
to import Apple’s UserNotifications
framework.
import UserNotifications
In the AppDelegate
class, let’s create a function, registerForPushNotifications()
, that requests permission from the user to allow the app to send push notifications.
call the function we just created, from the application(_:didFinishLaunchingWithOptions:)
method of AppDelegate
.
Add registerForPushNotifications()
above the return true
statement. Your AppDelegate.swift
file will look like this:
The release note of X code 11.4 beta says:
“simctl
also supports sending simulated push notifications. If the file contains “Simulator Target Bundle”, the bundle identifier is not required, otherwise, you must provide it as an argument (8164566):
$ xcrun simctl push <device> com.example.my-app ExamplePush.apns
“
Now, let’s try what it says. For that, we require a JSON file with a valid Apple Push Notification Service payload.
Apple’s documentation, entitled “Generating a Remote Notification”, gives a clear idea about how we can create such a file. I’m creating a file named notification.apns
on my desktop and adding the following content in it:
After the file has been created, we can open the terminal and use the command given in the release note:
$ xcrun simctl push <device> com.example.my-app ExamplePush.apns
Here, <device>
is to be replaced by the device identifier, com.example.my-app
is to be replaced by your project’s bundle identifier, and ExamplePush.apns
is to be replaced by the filename of our apns
file.
Note:- That the bundle identifier is not necessary in the command, if you add it with the“Simulator Target Bundle”
key in theapns
file.
And you can retrieve the bundle identifier of your project by following the steps shown in the GIF below:
Before we execute the command, we have to send the app to the background first. That’s how the push notification will be visible.
Note: If you want the push notification to be visible even when the app is on the foreground, you can make AppDelegate
conform to UNUserNotificationCenterDelegate
and implement its userNotificationCenter(_:willPresent:withCompletionHandler:)
method. And make sure that you have included the following line before calling registerForPushNotifications()
in application(_:didFinishLaunchingWithOptions:)
method:
UNUserNotificationCenter.current().delegate = self
Now, let’s execute the command. In my case, the command is:
$ xcrun simctl push 25A9FF1A-000B-4F36-B44C-43AC7BCD0958 np.com.sagunrajlage.TestPushNotifications notification.apns
As soon as it is executed, this is what we can see in the simulator — a push notification:
“In Simulator, drag and drop an APNs file onto the target simulator. The file must be a JSON file with a valid Apple Push Notification Service payload, including the “aps” key. It must also contain a top-level “Simulator Target Bundle” with a string value matching the target application‘s bundle identifier.”
We can simply drag and drop our apns
file into the simulator to get a push notification. But for that, we will have to make some modifications in the apns
file.
In the notification.apns
file that we created previously, let’s add one more key, "Simulator Target Bundle”
, which will hold the value of our bundle identifier, which is “np.com.sagunrajlage.TestPushNotifications”
in my case.
So, the file will look like this: