Wednesday, September 18, 2013

GCM APN for Android

Important: C2DM has been officially deprecated as of June 26, 2012.
It has been replaced by Google Cloud Messaging for Android (GCM)
This Tutorial will guide you how to create a sample simple application using the GCM functionality,
This demo will help you registering and unRegistering android device from GCM server
Getting your Sender ID
  • STEP 1.  Register Here .
  • STEP 2.  Click Create project. Your browser URL will change to something like:
    " https://code.google.com/apis/console/#project:4815162342 "
    Take note of the value after#project: (4815162342 in this example). This is your project ID, and it will be used later on as the GCM sender ID. This Id will be used by the Android Device while Registering for Push Notification.
  • STEP 3. Choose Service tab from the left side menu on the web page. and turn on “ Google Cloud Messaging for Android “
  • STEP 4. Go to API Access tab from the left menu of web page.
press Create new Server key and note down the generated key

CREATING APP FOR GCM
  1. Update ADT plugin 20 .
  2. update SDK > install Extras > Google Cloud Messaging for Android Library.
  3. Add gcm.jar to libs folder.(will be in the  android_sdk/extras/google/gcm after updating ADT and SDK)
  • STEP 5. Create A new Project in Android with the following specifications
Android Version 2.2
Package   =  com.sagar.gcma
Main Activity  =  PushAndroidActivity
  • Source Code for PushAndroidActivity.java:
package com.sagar.gcma; 
import static com.sagar.gcma.CommonUtilities.SENDER_ID;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import com.google.android.gcm.GCMRegistrar;
public class PushAndroidActivity extends Activity {
private String TAG = "** pushAndroidActivity **";
private TextView mDisplay;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
checkNotNull(SENDER_ID, "SENDER_ID");
GCMRegistrar.checkDevice(this);
GCMRegistrar.checkManifest(this);
setContentView(R.layout.main);
mDisplay = (TextView) findViewById(R.id.display);
final String regId = GCMRegistrar.getRegistrationId(this);
Log.i(TAG, "registration id =====  "+regId);
if (regId.equals("")) {
GCMRegistrar.register(this, SENDER_ID);
} else {
Log.v(TAG, "Already registered");
}
mDisplay.setText("ffffff        "+regId);
}
private void checkNotNull(Object reference, String name) {
if (reference == null) {
throw new NullPointerException(
getString(R.string.error_config, name));
}
}
@Override
protected void onPause() {
super.onPause();
GCMRegistrar.unregister(this);
}
}

  • Source code for GCMIntentService.java :
package com.sagar.gcma; 
import static com.sagar.gcma.CommonUtilities.SENDER_ID;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import com.google.android.gcm.GCMBaseIntentService;
public class GCMIntentService extends GCMBaseIntentService{
public GCMIntentService() {
super(SENDER_ID);
}
private static final String TAG = "===GCMIntentService===";
@Override
protected void onRegistered(Context arg0, String registrationId) {
Log.i(TAG, "Device registered: regId = " + registrationId);
}
@Override
protected void onUnregistered(Context arg0, String arg1) {
Log.i(TAG, "unregistered = "+arg1);
}
@Override
protected void onMessage(Context arg0, Intent arg1) {
Log.i(TAG, "new message= ");
}
@Override
protected void onError(Context arg0, String errorId) {
Log.i(TAG, "Received error: " + errorId);
}
@Override
protected boolean onRecoverableError(Context context, String errorId) {
return super.onRecoverableError(context, errorId);
}
}
  • Source Code for CommonUtilities.java
?
1
2
3
4
5
6
7
package com.sagar.gcma;
public final class CommonUtilities {
static final String SENDER_ID = "515850168860";
}
  • Source Code for AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
package="com.sagar.gcma"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="16" />
<permission
android:name="com.sagar.gcma.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.sagar.gcma.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".PushAndroidActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name="com.google.android.gcm.GCMBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.sagar.gcma" />
</intent-filter>
</receiver>
<service android:name=".GCMIntentService" />
</application>
</manifest>
?
  • SourceCode of main.xml
</pre>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/display"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#ffffff" />
</ScrollView>
<pre>
?

No comments:

Post a Comment