A Swift Journey: Episode 2 — Handling Silent Notifications

Arinc Elhan
Connected2me Tech Blog
4 min readMay 29, 2019

--

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.

Silent notifications are not meant as a way to keep your app awake in the background beyond quick refresh operations, nor are they meant for high priority updates. In addition, the delivery of silent notifications may be throttled if the total number becomes excessive. The actual number of silent notifications allowed by the system depends on current conditions, but don’t try to send more than two or three silent notifications per hour.

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.
Enabling Remote Notification

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.

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!

--

--