Hi Guys ,

Today I am going to share how you can set up GCM in to your android app with code snippet.

How we get notification from google server in to our android app.

Below is the Architecture Overview

gcm_overview

I will give you a brief about Above Components :-

Components Used Above :-

 

GCM Connection Servers

 

 

Google servers involved in sending messages between the app server and the client app.

 

Client App

 

A GCM-enabled client app that communicates with your app server.

 

App Server

 

An app server that you write as part of implementing GCM. The app server sends data to a client app via the GCM connection server. If your app server implements the XMPP protocol, it can also receive messages sent upstream from client apps.

 

Credentials Used Above :-

 

Sender ID

 

A unique numerical value created when you configure your API project (given as “Project Number” in the Google Developers Console). The sender ID is used in the registration process to identify an app server that is permitted to send messages to the client app.

 

API Key

 

An API key saved on the app server that gives the app server authorized access to Google services. In HTTP, the API key is included in the header of POST requests that send messages. In XMPP, the API key is used in the SASL PLAIN authentication request as a password to authenticate the connection. Do not include the API key anywhere in your client code. You obtain the API key when you configure your API project.

 

Token

 

An ID issued by the GCM connection servers to the client app that allows it to receive messages. Note that registration tokens must be kept secret.

So what ever I mentioned above , you can get easily in google search.

Now follow below steps to complete set up GCM in your app.

a) Get a configuration file from Developer Console or search “Get A Configuration file” you will easily get in google. and also create one server key in console that is also called as API Key , which will be used by your server app while sending any message.

Add the configuration google-services.json at below path of android studio (See in below picture)

google-services.PNG

The Google Services plugin for Gradle parses configuration information from the google-services.json file. Add the plugin to your project by updating your top-level build.gradle and your app-level build.gradle files as follows:

  1. Add the dependency to your project-level build.gradle:
    classpath 'com.google.gms:google-services:1.3.0'
  2. Add the plugin to your app-level build.gradle:
    apply plugin: 'com.google.gms.google-services'
    Compile 'com.google.android.gms:play-services-gcm:7.8.0'

    b) Edit you AndroidManifest.xml

  • Your <application-package-name> + ".permission.C2D_MESSAGE" permission to prevent other Android applications from registering and receiving the Android application’s messages. The permission name must exactly match this pattern—otherwise the Android application will not receive the messages.

permission_c2d

  • A declaration of GcmReceiver, which handles messages sent from GCM to your application. Because this service needs permission to receive messages from GCM, addcom.google.android.c2dm.permission.SEND to the receiver.

receive_message

  • A declaration of GcmListenerService, which enables various aspects of handling messages such as detecting different downstream message types, determining upstream send status, and automatically displaying simple notifications on the app’s behalf.

GCM_listener

  • A service that extends InstanceIDListenerService, to handle the creation, rotation, and updating of registration tokens.

instance_id

  • Optionally, the android.permission.WAKE_LOCK permission if the application needs to keep the processor from sleeping when a message is received.
  • If the GCM feature is critical to the Android application’s function, be sure to setandroid:minSdkVersion="8" or higher in the manifest. This ensures that the Android application cannot be installed in an environment in which it could not run properly.

Here is an example manifest that supports GCM:

<manifest package="com.example.gcm" ...>

    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <permission android:name="<your-package-name>.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="<your-package-name>.permission.C2D_MESSAGE" />

    <application ...>
        <receiver
            android:name="com.google.android.gms.gcm.GcmReceiver"
            android:exported="true"
            android:permission="com.google.android.c2dm.permission.SEND" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.example.gcm" />
            </intent-filter>
        </receiver>
        <service
            android:name="com.example.MyGcmListenerService"
            android:exported="false" >
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
            </intent-filter>
        </service>
        <service
            android:name="com.example.MyInstanceIDListenerService"
            android:exported="false">
            <intent-filter>
                <action android:name="com.google.android.gms.iid.InstanceID" />
            </intent-filter>
        </service>
    </application>

</manifest>

 

C) Obtain a Registration Token

An Android application needs to register with GCM connection servers before it can receive messages. When an app registers, it receives a registration token and sends it to the app server. The client app should store a boolean value indicating whether the registration token has been sent to the server.

First check did you have previously Register your app with GCM or not with below code :-

GCM_REGI

If not then open GCMRegiIntentService.class to register your app with below code :-

GCM_get_token

gcm_sender_id you will get from Google Developer console while making Server Key (or API Key)

Once you’ve received your registration token, make sure to send it to your server.

The listener service’s onTokenRefresh method should be invoked if the GCM registration token has been refreshed:

while making AndroidManifest.xml I have mentioned  packagename.CustomInstanceIDServiceListener , This service will be called if token is refreshed , so add below code in this service.

custom_instance_od

And on Token Refresh Call GCMRegiIntentService.class again to get new token and send it to server.

Dont forget to update your sharedpreference value after server sends your app successfull response. see below code :-

successfull_response

Till Now you have send token or gcm id to your server app successfully and  1 , 2 , 3 Steps are done of Above Architecture Diagram.

Now server app will send message along with gcm id and API Key to gcm connection server.

Now you need to receive message , follow below code in your app :-

As in above AndroidManifest.xml we created CustomGCMListenerService to receive message response from GCM connection server.

message_recv

Like above you can parse response what ever has been send by server app and pass it to NotificationEngine to show Notification.

If you want to make custom notification instead of default one then you can use remote views.

If anything wrong I have put then drop a comment I will correct and update the blog it will help others.