Android GCM is a free service provided by Google, supporting developers to send push notifications from their application servers to android client applications through GCM server. GCM Server handles queuing and storing of messages and delivering them to the registered android devices. To enable this cloud messaging service, both application server and client application will have to get credentials which can be obtained by creating a Google API Project.

Getting “Server Key” and “Project Number”

Create a “Google API Project” to obtain the “Server Key” and “Project Number”
1. Goto Google Developer Console (https://console.developers.google.com/project)
2. Create Project -> Give Project Name -> Create —-> You can get the “Project Number” when click on the project

3. Click on Project ->Select API & AUTH ->Select APIs -> Enabled Google Cloud Messaging for Android

4. Select “Credentials” ->”Create New Key” ->Select “Server Key” (Don’t specify IP -Any IP allowed)

Android Client App
If your are the developer of Android client app, then you will only need the “Project Number”, for your implementation. Project number will be used as the SENDER_ID. But for the 3rd party server application to send messages, it should know the registered devices list. Thereby, the registration_id you obtained through gcm,register() call should be given/send to the application server.
Pre-requisites to deploy app on device:
– Android 2.2 or higher device
– with Google Play Service installed
– and logged in with Google Account

You can use the Demo App provided by GCM as a guide. I will here include the important things to consider.
Download Source Code from Git repository: https://code.google.com/p/gcm/source/checkout
AndroidManifest.xml
Add following required permissions, broadcast-receiver, service to AndroidManifest.xml file
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<permission android:name="<package_name>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="<package_name>.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<package_name>" />
</intent-filter>
</receiver>
<service android:name=".GcmIntentService" />
MainActivity.java
The registration has to be done once . Keep a SharedPreference variable to store the registration_id once you received. This id can be changed when upgrades happen, in that case will have to validate the id, therefore keep it store with the app_version.
GoogleCloudMessaging gcm;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
if(regIdIsEmpty()){
registerInBackground();
}
}
private void registerInBackground() {
new AsyncTask() {
@Override
protected String doInBackground(Void... params) {
String regid = "";
try {
if (gcm == null) {
gcm = GoogleCloudMessaging.getInstance(context);
}
regid = gcm.register(SENDER_ID);
Log.i("Device registered, registration ID=", token);
}
catch (IOException e) {
Log.i("Registration Error", e.getMessage());
}
return regid;
}
}.execute(null, null, null);
}
private void storeRegistrationId(Context context, String regId) {
final SharedPreferences prefs = getGcmPreferences(context);
int appVersion = getAppVersion(context);
Log.i(TAG, "Saving regId on app version " + appVersion);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(PROPERTY_REG_ID, regId);
editor.putInt(PROPERTY_APP_VERSION, appVersion);
editor.commit();
}
GcmIntentService.java
Service class should display the new message as a notification.
public class GcmIntentService extends IntentService {
@Override
protected void onHandleIntent(Intent intent) {
Bundle extras = intent.getExtras();
sendNotification(extras.toString());
}
private void sendNotification(String msg) {
Log.d(TAG, "Preparing to send notification...: " + msg);
mNotificationManager = (NotificationManager) this
.getSystemService(Context.NOTIFICATION_SERVICE);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
new Intent(this, MainActivity.class), 0);
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
this).setSmallIcon(R.drawable.gcm_cloud)
.setContentTitle("GCM Notification")
.setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
.setContentText(msg);
mBuilder.setContentIntent(contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
Log.d(TAG, "Notification sent successfully.");
}
}
GcmBroadcastReceiver.java
A BroadcastReciver is needed to trigger the Service class to post the Notification once a message is received from the GCM Server.
public class GcmBroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GcmIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
startWakefulService(context, (intent.setComponent(comp)));
setResultCode(Activity.RESULT_OK);
}
}
Like this:
Like Loading...