How to Implement Server Side Event Driven Push Notifications in iOS

Apr 12 · 5 min read

There are a lot of implementation guidelines for push notifications(PN) but PN feature requires very good systematic architecture. In this guideline we will try to cover push notification feature both in server and client side. After this guideline you will be able to send push notifications to your iOS apps from your server app automatically. Once the implementation is completed the rest is up to your imagination and requirement to send notifications to your users but the main focus of this guideline will be showing you how to navigate within the app by using notifications. We will use Google Firebase for both client and server side PN implementation. We will try to cover each step of implementation from scratch and you will be able to find sample project attached at the end of this article. Let’s get started!!

1- First we will create our single view app from Xcode

2- Give it any name you want, but keep that in mind the bundle identifier will be used for creating Firebase app and necessary PN certificates from Apple.

3- We will create separate XIB files and controllers to show our different pages

We create an XIB file like shown above and provide a controller name to handle UI. Please keep that in mind that when we create UIViewControllers using XIB files we need to change the class of File’s Owner with our UIViewController class name and attach the view below as source to our controller as shown in the image below;

There is one tricky step here normally Xcode doesn’t allow us to do this unless we change File’s Owner class to a view controller class so make sure that you do this step after creating XiB, view controller and changing file’s owner class to your view controller class.

4- Repeat step 3 until you achieve a file structure like this;

5- Page1Controller code is shown below all other pages also have the similar code;

Now we need to update our project settings so that it will have the capability of receiving push notifications

We need to enable these settings in the capability section

You can also check the detailed guideline for enabling push notifications in iOS from the link below:

https://www.raywenderlich.com/8164-push-notifications-tutorial-getting-started

Now that we have our iOS setup ready we can move to firebase console for creating our app from the link below;

Link: https://firebase.google.com/

6- From here first you need to create a project;

After project creation is done you can click on your project and open the project management console. Now we can integrate Firebase Push Notification( a.k.a Cloud Messaging in firebase)

In this section you need to choose iOS platform and fill the form step by step, the most important thing here is that you define iOS bundle id correctly as shown below otherwise your push notifications will not work

After this point you just need to follow the steps defined in Firebase and you will be able to complete integrating Firebase to you iOS app. Once the integration is complete you can launch the app on a device and confirm that Firebase is working. Keep that in mind that push notifications does not work on simulators so you need to launch the app on an actual device.

We will use default firebase app delegate codes as much as possible not to cause any confusion. Here is the structure of app delegate of demo project;

As you can see in the app delegate we used the suggested implementation of Firebase. But for our project we customize some functions to handle navigation within the app depending on push notification payload.

From this point on we will switch to Restful API implementation so that we can send notifications from our own server.

We will use NodeJS to develop our server code and a package called fcm-node which can be find here.

Our server code will look like this;

As can be seen in the code above we created an http server using ExpressJS and created an end-point called notification which accepts POST requests and send a notification to certain device. Before you make the request make sure that you are starting the server code by typing “node app.js” command in the shell. Here is the example request I used for sending a notification to my own device;

In this request we send the device token which is retrieved when we launch our app with firebase as shown in the image below;

Once we send the request from Postman as defined above we will receive the notification immediately on our device(registered to firebase gcm with token above) and if we click on the notification we will be redirected to page defined in the notification payload.

Redirecting user or navigating to different pages within the app from push notification is very common and used by many popular apps like WhatsApp, Instagram, Twitter and many others. When you finish reading & implementing this article you will be able to answer the technical interview questions listed below;

  • How do you implement push notifications for iOS app?
  • What is the purpose of device token for push notifications?
  • Design a system which will manage iOS app navigation using push notification logic.

If you have any question feel free to leave a comment and we’ll get in touch.
Happy coding!!

You can access complete iOS app from here,
You can also access complete server code from here.

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade