Push Notifications on Android: How to Register with the Server

push notifications on androidAndroid is used by the majority of smartphone devices in the world. It is popular because it is free and has an easy-to-use user interface. It also has the largest library of applications that can do a variety of things, like help you at the office or pass the time by playing a game. The Android operating system has received several updates since it was introduced in 2008 (we’re currently at Version 4.4).  It has been estimated that 71% of all app developers in the world develop apps for the Android market, which keeps expanding daily. If you want to create your own application, it’s surprisingly easy to learn how, especially if you have some experience in Java (this course is a great place to start!)

In this tutorial, we’re going to give you an overview of push notifications and how to get started with push notifications for your Android app. Before we begin, let’s clarify the concept of push notifications and why they are important.

What are Push Notifications Exactly?

Notifications that have been sent out, or “pushed”, to a device by a central server are known as push notifications. Push notifications are received on a device via the internet. An application has to be registered in advance with an external server to receive the service.

Why are they important ? They allow new information to be displayed to a user as soon as it’s available. For example, in a social media app, if a friend sends a message to the user, it could be displayed to him in the next few seconds through a push notification. The information is sent out by an external server. This reduces the load on your application and on the user’s device, as the server does most of the work. With regular notifications, an application has to remain in touch with the server all the time and upload and download data. Also, sometimes it’s possible that a normal notification an application sends out may be missed by the user.  This may happen if the user is on a call, for example, or the device is switched off. Push notifications, because they are on an external server, always deliver the message to the user when the device is ready.

Servers that handle push notifications are also capable of receiving data from the user’s device. The information is passed back and forth between a device and a server, through push and pull protocols. Learn more about notifications for your Android app in this course.

Setting up an External Push Notification Server: Registering for GCM (A Cloud Messaging Service)

You can register with the Google Cloud Messaging for Android (GCM) service to set up a free push notification service. GCM is free and there are no limits on the messages you can send and receive. Google servers are also reliable, so you don’t have to waste money paying for a service.

GCM lets you send both HTTP and XMPP based messages. HTTP is old technology, compared to XMPP, but it can be used side by side with it. XMPP technology is asynchronous, which lets you send more push notifications with fewer resources, unlike HTTP, which is resource intensive. XMPP also allows the device to send messages back to the server, which is invaluable if you are running a messaging application – this allows the user of your app to chat with another. XMPP also operates on a single connection. Separate connections aren’t needed to pass messages back and forth.

You need to install the Google Play Services SDK for your app to support GCM. You can download it here.

Registering for the GCM Service

You need to use the Google API development service to create a Google API project first before you can register for GCM. You can find the developer console here.

Once here, you click on create project (or load an existing project). After you name the project, you need to jot down the project number. This is used to register the GCM service.

After this, the GCM service can be enabled by selecting “APIs and auth” on the sidebar to the left and turning “Google Cloud Messaging for Android” ON. After this, you will need to obtain a unique API key for your applications. You have the option of creating new keys if you believe the app is being threatened by a hacker.

An API key is obtained by first clicking on “APIs and auth” to the left of the developer console, and then selecting “Credentials”. In the submenu, select the “Public API acces” and choose the “Create new key” option. Then choose the “Server Key” option. You need to specify your server’s IP address here, and then click on the “Create” option. You need to jot down your API key- you will need it for authentication later.

Implementing GCM

You need to implement a GCM Client and a GCM server in the coding of your app (Application Manifest). You will need to set up Google Play services before you can do that. This is the most complicated step of the process. It involves setting up permissions so that your application is capable of sending and receiving messages. You also have to ensure that the GCM service cannot run on an unsupported device.

You will also have to decide which server connection you prefer to use – HTTP or XMPP. In either case, the GCM server will interact with your application server (implemented by) and a user’s device and send and receive data back and forth between them.

Of course, you’re not going to use push notifications in isolation. This course can help you develop your own Android application from scratch. Or if you’re more the gaming type, here’s a course to help you develop your own game on Android.