Engineering 

| 22 October 2015

Enable Android Push Notification using Google Cloud Messaging (GCM)


Most likely, your app needs real time updates for dynamic content or for more user engagement. Earlier we used to have polling mechanism to refresh the data. Polling is too expensive to the system as it consume server and client resources even if there are no updates. To solve this we need push notification from server and here GCM (Google cloud messaging) help us.

Google Cloud Messaging is a free service from Google to send push notifications to device on demand. If you want to learn more about Google Cloud technology, then please refer to this article on google cloud certification by Intellipaat.

Enabling GCM to your application:


There are two part of it. One is authentication of server and another is identification of device. We will go through step by step for each part.

1 . Server Authentication.

a ) Enable API : To get the server key go to Google developer console and select your project from top drop down menu. Once you select your project you can see your project details. Please note down your project number.



Click on the APIS & auth and select API . In mobile API, select “Cloud Messaging for Android” and enable the API.



Enabling Cloud Messaging for Android



b ) Create a Server Key : To create a server key go to credential section and in ADD CREDENTIAL Drop Down select 1st option Server key.



once you add server key you can see your server key listed in the API KEY section.



This is what we need to send push notification from server. While sending push notification via gcm server we need to send the device token and Server-Key in the request.

2. Device Identification :

Upon registering device with the GCM server, it will return registrationId. The GCM registrationId is an identifier to identify the mobile device. We need to store this on our server to send the push notification.

a) Register device with GCM server

First of all we need to add permission to your AndroidManifest.xml for GCM. Below two lines we need to add.



b ) Add your notification receiver in AndroidManifest.xml file.



You can see applozic sdk sample code for reference.

Best thing is google play service provide convenient apis for gcm registration. Following is the two lines of code which is required to get the registrationId of device..

GoogleCloudMessaging gcm =GoogleCloudMessaging.getInstance(mActivity);.

String regid = gcm.register (PROJECT_NUMBER);.

Once we get the registrationId, send this registration-Id to your server. Server will use this device registration-Id while sending push notification to device.

You can find full code snippet from applozic android SDK on github..

Applozic is on a mission to bring real time communication to every connected application in a matter of few mins.

Want to bring real time communication capabilities to your apps?

Sign up now: Applozic

Contact: growth@applozic.com AppLozic/Applozic-Android-SDK Applozic-Android-SDK for “Plug & Play Messaging library”github.com

See how a real-time chat SDK and API can boost, enhance, and optimize your user experiences.


ana mayer
Devashish Mamgain
</section>

Applozic Chat is easy for
developers, invaluable for
businesses, and addictive for users

See how in-app chat, voice and video messaging can transform your app from ordinary to unforgettable, for any industry and any platform.

Related articles

blog-3

Development

12 min read

What’s New: Product Updates From November 2020 | Applozic

In the month of November, we have released the latest version of iOS SDK. The fastest way to integrate high-quality 1:1 voice and video calls inside your application [...]

ana mayer
Ana Mayer