Using push notifications with Objective-C

Retain your users or just annoy them. Its up to you!

Bruno Muniz
3 min readApr 22, 2017

With APNS (Apple Push Notification Service) and third party API’s that manage push notifications publishing, we just have to provide the certificates from the developer center, and write a few lines of code to make it happen.

Photo by Serge Le Strat on Unsplash

The first step is to enable the Push Notifications feature at the Capabilities tab. We still don't have the certificate telling Xcode that we are allowed by Apple to handle push notifications on this project, so in order to do this, we must go to the Keychain Access > Keychain Access tab> Certificate Assistant > Request a certificate from a certificate authority.

Mark the option: Saved to disk and press continue.

It will generate a .certSigningRequest file. With this file, we will go to our developer account and navigate to your certificates. In the certificates list there should be a '+' button that will allow you to create new ones. We the select Push Notifications, and after that we might be able to select a file -> the .certSigningRequest. Then a .p12 file will be generated that we can download and drag to our own certificates list from your Keychain Access.

Done with the bureaucracy, let’s move on.

There are two types of certificates: sandbox (development) certificate and a distribution. Each one will be used on a different environment so it's not very important to have the distribution one for now. Beware: simulators don't receive notifications, so make sure to run it on your device.

This snippet will ask the user permission to receive push notifications from this App. It will be asked only once.

EDIT: on iOS 10 this snippet became deprecated because now you can use rich media content in your pushes (images, gifs, audio, video) so an updated alternative would be to use UserNotificationsFramework. (Notification Service Extension) Check this link for more information

Another snippet that will handle the payload once it is received is, at the AppDelegate main file:

Inside this method you can treat the JSON payload as you want, and it's being passed as a NSDictionary instance called userInfo. This is the content of a simple push payload:

{    "aps" : {        "content-available" : 1,        "alert" : "Hello Bruno"    }}

alertThe notification’s message.

badge The value indicated in the top right corner of the app icon. This can be set to a value or"Increment" in order to increment the current value by 1.

sound The name of a sound file in the application bundle. (.caf/.wav)

content-available This is a new feature, and it is this key that makes silent push possible. Set this value to 1 to trigger a background download.

To enable silent push, you also have to add remote-notification as your app UIBackgroundModes as described here.

So now that you know what can you expect inside this NSDictionary, you're free to handle it as you want. You can use NSNotificationCenter (Carefully) to trigger some method inside the app, or change some property value. If you want to read more about custom payloads, check this link from apple docs. Also here you can find some information about custom payload.

A very good free platform i've been using to send push notifications on my personal apps is OneSignal. With this you have unlimited free pushes and amount of devices, localization, A/B testing, realtime analytics and many other really nice features for free.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Bruno Muniz
Bruno Muniz

No responses yet

Write a response