This is your very first post. Click the Edit link to modify or delete it, or start a new post. If you like, use this post to tell readers why you started this blog and what you plan to do with it.
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
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.
A GCM-enabled client app that communicates with your 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 :-
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.
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.
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)
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:
- Add the dependency to your project-level
- Add the plugin to your app-level
apply plugin: 'com.google.gms.google-services'
b) Edit you AndroidManifest.xml
<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.
- A declaration of
GcmReceiver, which handles messages sent from GCM to your application. Because this service needs permission to receive messages from GCM, add
com.google.android.c2dm.permission.SENDto the receiver.
- 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.
- A service that extends
InstanceIDListenerService, to handle the creation, rotation, and updating of registration tokens.
- Optionally, the
android.permission.WAKE_LOCKpermission 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 set
android: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 :-
If not then open GCMRegiIntentService.class to register your app with below code :-
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.
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 :-
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.
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.