A Swift Journey: Episode 2 — Handling Silent Notifications
Hello again, and welcome back to our Swift Journey! In this one, we will talk about silent notifications and how we handle them in Connected2.me.
A silent push notification is defined as a push notification that does not have an alert, badge or sound, and just has key-value data.
When you send a Silent Push Notification and if the app is suspended, then the system wakes up or launches your app and puts it into the background state. They can be used to inform the application of new content without having the user informed.
So, after the brief information about the silent push notifications; let’s continue with how to enable them and handling them.
Enabling the Remote Notifications On Your Project
- In the Project Navigator, select your project.
- In the editor, select your iOS App Target.
- Select Capabilities tab.
- Enable Background Modes capability.
- Enable Remote notifications background mode.
Creating the Push Notification Service Certificate
Since we are testing on local environment, we need a sandbox development certificate.
- If you do not have the certificate, go to https://developer.apple.com and create your Apple Push Notification service SSL (Sandbox) certificate.
- After creation, you need to convert your certificate into a .pem format. You could use the following link for generating pem files:
https://stackoverflow.com/questions/1762555/creating-pem-file-for-apns
Getting Device Push Notification Token
- In order to get push notification token, we should check our AppDelegate class.
- We could get our token from didRegisterForRemoteNotificationsWithDeviceToken method.
Preparing JSON Payload and cURL Command
To support a Silent Notification, make sure that the payload’s aps
dictionary includes the content-available
key with a value of 1
. If there are user-visible updates that go along with the background update, you can set the alert
, sound
, or badge
keys in the aps
dictionary.
Sample JSON payload:
After done with payload, we need to prepare our cURL command in order to test our notification.
- We will be using our newly generated .pem files with passphrase.
- “apns-topic” is your App ID that could be get from https://developer.apple.com/account/ios/identifier/bundle
Sample cURL command:
Creating Waiting Scheme for Xcode Project
- You should create a new scheme with Launch option is Wait for executable to be launched.
- After creating, connect your test device and build and your newly created scheme. Since it is on waiting state, your application does not start until you explicitly start it.
Creating Silent Notification Checker Function
- We could check the notification and payload from didFinishLaunchingWithOptions method.
- We could also add a debug point inside to didFinishLaunchingWithOptions method.
Checking Silent Push Notification
- After everything is done, we should send the cURL command from terminal and wait for Xcode debugger to be attached your project.
- If everything goes according to plan, notification will come to your test device, your application starts on background, your debugger catches the process on your debug point, and you should be fine!
Conclusion
In this part, we learned about the silent push notifications, how to setup your project and how to handle these notification types.
We hope you enjoyed this episode, and stay tuned for more!
Would like to get in touch? Reach me out on Connected2.me:
… or on LinkedIn: