What, Why, When & How of Push Notifications

Abhay Jain
Lets Code
Published in
5 min readOct 11, 2015

What : We all have heard about push notifications, it has now become the most required feature on any smartphone. It tells you that there is some activity on your app, maybe a friend commented on the photo you posted last night on Facebook or your cousin sent a Good Morning message on WhatsApp or your manager sent you an email or Candy Crush Saga is running a one day sale. You get notified about each of these activities immediately so that you can take actions on them. The server of these apps pushes the activities to the mobile app.

Why : Okay, so we know what it is but why is this technology required? If such technology does not exist, how would we know about the activities in our app? Well, the mobile app would ask the server continuously after some interval (lets say 5 minutes) “Hey! do you have some new activity for me?”. The server would say, “No dude! Nothing new for you.

The mobile app would sit quiet and after another 5 minutes, it would ask server again “Hey! do you have some new activity for me?”. The server might say “Yeah! your cousin said Hello 4 minutes ago”. You would go to app and reply to cousin. This is the pull mechanism, where client (mobile app) is pulling data from server after every 5 minutes. It has delays if intervals of polling are large like few minutes. If polling interval is reduced to make it faster, then each of your app keeps polling their servers so frequently, your battery would die out soon and servers would be overloaded with polling requests. So a new mechanism was needed which solves these issues. It was called push because of its opposite nature than that of pulling.

When : Lets go back to year 2003, when BlackBerry was the smartest phone brand. The most prominent smart feature back then was email. That year, BlackBerry introduced a new technology in their phone, it was push email. Your phone was notified whenever you received a new email, that was it. This notification was nothing like what it looks like today. Same year, Microsoft launched push email in their WindowsPhones. The user was sent an SMS whenever they received a new email.

The modern push notification ecosystem for smartphones was introduced by Apple in 2009 in their iPhone as APNS. They invested extensively on UI/UX of push notifications & notification centre to make it feasible and beautiful as it is today ( Warning! Apple fanboy ). Google joined the party and released their push service in 2010 as C2DM. Google relaunched their push service as GCM in 2012.

How : Last three topics were history of push, we should now dive into more technical aspect of it. Although push notification is now part of all mobile operating systems, iOS, Android, Windows, BlackBerry10 etc but I will only discuss first two. They combined cover more than 95% of the market.

Common Step : Every mobile device (iPhone or Android Phone) has a unique identity provided by manufacturer, lets call it Device ID. Every mobile device is also assigned a unique PushToken ID by their push service provider i.e. by Apple for iPhones and Google for Android Phones. You should save the PushToken ID of device against its Device ID on your server so that you can send push to these devices. You can not send push to phone if you do not have its PushToken ID. Push service providers have their respective APIs which can be called from the mobile app to get PushToken. Push Tokens have expiry time, so they must be refreshed after sometime.

iOS : Apple wants you to first have an SSL certificate to be able to talk to its APNS. The steps to create one are described here. A socket to APNS is opened using this SSL certificate, when you want to push a message, add it to the queue. The payload for APNS looks like this


“aps” : {
“alert” : “This is the alert text”,
“badge” : 1,
“sound” : “default”
}

You must have seen a number in small circle on the top right corner of app icons. This number is called badge.

Apple has a Feedback service, which your server should poll to after some interval (once a day is good enough). It tells out of all the push tokens, which all have expired. You should remove those from your database and ask mobile app to regenerate those.

Android : There is no point in discussing how C2DM works, since it has been completely shutdown by Google. We will see how GCM works, but before that let’s see what advantages it has over C2DM.

  • The biggest change is that GCM does not require ClientLogin for generating AuthToken (It is an encrypted long password given by Google to talk to its Push servers in each request). New AuthToken for your app is available in Google APIs Console.
  • The payload size in C2DM was less than 1k, which is now 4k. It has allowed server to send larger message in push.
  • Another major improvement is batch request. You can send same push to 1000 devices in just single HTTP request.

This is the sample HTTP request in its simplest form to push Android devices


https://gcm-http.googleapis.com/gcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u0GBYzPu7Udno5aA
{
"to": “push_token_id”,
“data”: {
“message”: “My first push!”,
}
}

Final Note : You would require to dig down more in order to run write your push code. I have written a standalone push notification server for my previous company. The product managers can input a MySQL query and a push message, all the users who are in the result set of that query will receive the push message. We did not test its limit, but it was able to send ~30k pushes in one second. After the push, it shows the report of users including operating system wise users distribution, users who successfully received push, users who uninstalled the app etc.

Update : In continuation to this, I have written a post on how to send push more effectively.

--

--

Abhay Jain
Lets Code

Building games @PaytmFirstGames | Startups | Technology | Guitar | Game Developer | Previously @Nimbuzz @Hiverhq @OctroInc | Blogging since 2009