• Advertisement

Google Cloud Messaging For Android (GCM) Simple Tutorial

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
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"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
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>

Package explorer ScreenShot

Logcat ScreenShot

Important links:

54 Responses

  1. there’s no doubt that your blog has the best explanation on this matter. thanks for all you’ve done.http://www.viagensdeturismo.org

  2. heya..thanks for the post and great tips…i think that hard work is the most important aspect of getting success.http://www.guiadenegocio.org

  3. newB here, but … line: in main.xml throws error.

  4. okay it deleted my tag … next to last line:

  5. disregard previous 2 posts … but:

    1) had to add: import com.sagar.gcma.R; to PushAndroidActivity.java
    2) had to add “error_config” element for “My config error text” to res\values\strings.xml

    even so still getting other errors …

  6. can you paste the error log here

    • 1. SENDER_ID can not be resolved to a variable
      2. main can not be resolved or is not a field
      3. display can not be resolved or is not a filed

  7. i understood the concept but…

    Can i know how to run this app on a mobile?
    Is it just launching it on the mobile?
    How does this app get connected so that any message pusehd on the server reaches the mobile?

  8. Hello Sharath
    Actually this code is just for getting the registration key.
    Once you get the registration key u can send it to your server and using that key your server will broadcast the message,

    And on your side the you will need a broadcast receiver that will handle that message.

  9. Hi,
    could you please post a sample code for server side (eg: java/jsp). Thanks

  10. thats great stuff but I need help with server side code :/ can I have its code ?

    • Hello Khurram,
      I am very thankful if you like this, Actually I am a little busy these days thats why could not update the server side code for this but I will do it surely but may take some time .

      thank you

  11. Need Help… I tried this but gives me log output as device has no accounts and asyncFetch: no username… I created new project and put my SENDER_ID…..

  12. excuse me sir , GCM example application..?

  13. hey plz update server side code using servlets or some other mechanism…

  14. hi,

    getting an error that “error_config cannot be resolved or is not a field”.
    Could you please resolve it.

    Thanks

  15. hi

    i have geeing an error “error_config cannot be resolved or is not a field”.
    Can you please help me to get over ride of it.

    thanks.

  16. Hi,
    here is my logcat details. Please do something.

    09-29 10:52:31.608: D/AndroidRuntime(277): Shutting down VM
    09-29 10:52:31.608: W/dalvikvm(277): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
    09-29 10:52:31.778: E/AndroidRuntime(277): FATAL EXCEPTION: main
    09-29 10:52:31.778: E/AndroidRuntime(277): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.komal.gcma/com.komal.gcma.PushAndroidActivity}: java.lang.ClassNotFoundException: com.komal.gcma.PushAndroidActivity in loader dalvik.system.PathClassLoader[/data/app/com.komal.gcma-1.apk]
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.os.Handler.dispatchMessage(Handler.java:99)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.os.Looper.loop(Looper.java:123)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.ActivityThread.main(ActivityThread.java:4627)
    09-29 10:52:31.778: E/AndroidRuntime(277): at java.lang.reflect.Method.invokeNative(Native Method)
    09-29 10:52:31.778: E/AndroidRuntime(277): at java.lang.reflect.Method.invoke(Method.java:521)
    09-29 10:52:31.778: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    09-29 10:52:31.778: E/AndroidRuntime(277): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    09-29 10:52:31.778: E/AndroidRuntime(277): at dalvik.system.NativeStart.main(Native Method)
    09-29 10:52:31.778: E/AndroidRuntime(277): Caused by: java.lang.ClassNotFoundException: com.komal.gcma.PushAndroidActivity in loader dalvik.system.PathClassLoader[/data/app/com.komal.gcma-1.apk]
    09-29 10:52:31.778: E/AndroidRuntime(277): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
    09-29 10:52:31.778: E/AndroidRuntime(277): at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
    09-29 10:52:31.778: E/AndroidRuntime(277): at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
    09-29 10:52:31.778: E/AndroidRuntime(277): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
    09-29 10:52:31.778: E/AndroidRuntime(277): … 11 more

  17. Thank you for such great effort…Could you please post another tutorial for the server side?

  18. what is the problem?
    my logcat errors:
    10-06 22:27:21.872: E/AndroidRuntime(278): FATAL EXCEPTION: main
    10-06 22:27:21.872: E/AndroidRuntime(278): java.lang.NoClassDefFoundError: com.google.android.gcm.GCMRegistrar
    10-06 22:27:21.872: E/AndroidRuntime(278): at eyeAppStudios.com.DashboardActivity.onCreate(DashboardActivity.java:59)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
    10-06 22:27:21.872: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
    10-06 22:27:21.872: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
    10-06 22:27:21.872: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    10-06 22:27:21.872: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    10-06 22:27:21.872: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)

  19. 0-06 22:27:21.872: E/AndroidRuntime(278): FATAL EXCEPTION: main
    10-06 22:27:21.872: E/AndroidRuntime(278): java.lang.NoClassDefFoundError: com.google.android.gcm.GCMRegistrar
    10-06 22:27:21.872: E/AndroidRuntime(278): at eyeAppStudios.com.DashboardActivity.onCreate(DashboardActivity.java:59)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
    10-06 22:27:21.872: E/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
    10-06 22:27:21.872: E/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
    10-06 22:27:21.872: E/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
    10-06 22:27:21.872: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
    10-06 22:27:21.872: E/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
    10-06 22:27:21.872: E/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)

    help me please

    • You might directly added the ‘gcm.jar’ file from “Build Path” instead of that copy the jar file and add to ‘libs’ folder available at your project creation folder. Then it works fine

    • install apk in GoogleAPI enabled emulator or device

      • Am Installed Google Api Enabled Emulator What OutPut Am Getting Please help me.AM getting only fffff no RegistrationId is Getting.

  20. 10-10 20:25:07.277: W/PackageManager(72): Code path for pkg : com.sagar.gcma changing from /data/app/com.sagar.gcma-1.apk to /data/app/com.sagar.gcma-2.apk
    10-10 20:25:07.277: W/PackageManager(72): Resource path for pkg : com.sagar.gcma changing from /data/app/com.sagar.gcma-1.apk to /data/app/com.sagar.gcma-2.apk
    10-10 20:25:07.277: D/PackageManager(72): Services: com.sagar.gcma.GCMIntentService
    10-10 20:25:07.277: D/PackageManager(72): Receivers: com.google.android.gcm.GCMBroadcastReceiver
    10-10 20:25:07.277: D/PackageManager(72): Activities: com.sagar.gcma.PushAndroidActivityActivity
    10-10 20:25:07.277: D/PackageManager(72): Permissions: com.sagar.gcma.permission.C2D_MESSAGE
    10-10 20:25:07.287: I/ActivityManager(72): Force stopping package com.sagar.gcma uid=10041
    10-10 20:25:07.527: I/installd(34): move /data/dalvik-cache/data@app@com.sagar.gcma-2.apk@classes.dex -> /data/dalvik-cache/data@app@com.sagar.gcma-2.apk@classes.dex
    10-10 20:25:07.527: D/PackageManager(72): New package installed in /data/app/com.sagar.gcma-2.apk
    10-10 20:25:07.557: W/PackageManager(72): Unknown permission android.permission.ADD_SYSTEM_SERVICE in package com.android.phone
    10-10 20:25:07.567: W/PackageManager(72): Not granting permission android.permission.SEND_DOWNLOAD_COMPLETED_INTENTS to package com.android.browser (protectionLevel=2 flags=0x1be45)
    10-10 20:25:07.567: W/PackageManager(72): Unknown permission com.google.android.gm.permission.WRITE_GMAIL in package com.android.settings
    10-10 20:25:07.567: W/PackageManager(72): Unknown permission com.google.android.gm.permission.READ_GMAIL in package com.android.settings
    10-10 20:25:07.599: W/PackageManager(72): Unknown permission com.android.providers.im.permission.READ_ONLY in package com.google.android.apps.maps
    10-10 20:25:07.607: W/PackageManager(72): Unknown permission com.google.android.pushmessaging.permission.RECEIVE in package com.google.android.apps.maps
    10-10 20:25:07.857: I/ActivityManager(72): Force stopping package com.sagar.gcma uid=10041
    10-10 20:25:08.057: D/dalvikvm(72): GC_EXPLICIT freed 11001 objects / 658000 bytes in 178ms
    10-10 20:25:08.357: D/dalvikvm(189): GC_EXPLICIT freed 2868 objects / 148192 bytes in 244ms
    10-10 20:25:08.457: W/RecognitionManagerService(72): no available voice recognition services found
    10-10 20:25:08.537: D/dalvikvm(207): GC_EXPLICIT freed 612 objects / 35584 bytes in 448ms
    10-10 20:25:08.928: D/dalvikvm(72): GC_EXPLICIT freed 4243 objects / 232256 bytes in 158ms
    10-10 20:25:08.967: I/installd(34): unlink /data/dalvik-cache/data@app@com.sagar.gcma-1.apk@classes.dex
    10-10 20:25:08.979: D/AndroidRuntime(571): Shutting down VM
    10-10 20:25:08.997: D/dalvikvm(571): Debugger has detached; object registry had 1 entries
    10-10 20:25:09.049: I/dalvikvm(571): JNI: AttachCurrentThread (from ???.???)
    10-10 20:25:09.049: I/AndroidRuntime(571): NOTE: attach of thread ‘Binder Thread #3’ failed
    10-10 20:25:09.877: D/AndroidRuntime(583): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
    10-10 20:25:09.877: D/AndroidRuntime(583): CheckJNI is ON
    10-10 20:25:10.148: D/AndroidRuntime(583): — registering native functions —
    10-10 20:25:11.167: I/ActivityManager(72): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.sagar.gcma/.PushAndroidActivityActivity }
    10-10 20:25:11.257: D/AndroidRuntime(583): Shutting down VM
    10-10 20:25:11.287: D/jdwp(583): Got wake-up signal, bailing out of select
    10-10 20:25:11.287: D/dalvikvm(583): Debugger has detached; object registry had 1 entries
    10-10 20:25:11.347: I/ActivityManager(72): Start proc com.sagar.gcma for activity com.sagar.gcma/.PushAndroidActivityActivity: pid=590 uid=10041 gids={3003}
    10-10 20:25:11.347: I/dalvikvm(583): JNI: AttachCurrentThread (from ???.???)
    10-10 20:25:11.357: I/AndroidRuntime(583): NOTE: attach of thread 'Binder Thread #3' failed
    10-10 20:25:12.079: I/** pushAndroidActivity **(590): registration id =====
    10-10 20:25:12.087: D/GCMRegistrar(590): resetting backoff for com.sagar.gcma
    10-10 20:25:12.107: V/GCMRegistrar(590): Registering app com.sagar.gcma of senders 452450168988
    10-10 20:25:12.397: I/ActivityManager(72): Displayed activity com.sagar.gcma/.PushAndroidActivityActivity: 1141 ms (total 1141 ms)
    10-10 20:25:13.147: V/GCMBroadcastReceiver(590): onReceive: com.google.android.c2dm.intent.REGISTRATION
    10-10 20:25:13.147: V/GCMBroadcastReceiver(590): GCM IntentService class: com.sagar.gcma.GCMIntentService
    10-10 20:25:13.167: V/GCMBaseIntentService(590): Acquiring wakelock
    10-10 20:25:13.197: V/GCMBaseIntentService(590): Intent service name: GCMIntentService-452450168988-1
    10-10 20:25:13.207: E/GCMRegistrar(590): internal error: retry receiver class not set yet
    10-10 20:25:13.207: V/GCMRegistrar(590): Registering receiver
    10-10 20:25:13.227: D/GCMBaseIntentService(590): handleRegistration: registrationId = null, error = PHONE_REGISTRATION_ERROR, unregistered = null
    10-10 20:25:13.227: D/GCMBaseIntentService(590): Registration error: PHONE_REGISTRATION_ERROR
    10-10 20:25:13.227: I/===GCMIntentService===(590): Received error: PHONE_REGISTRATION_ERROR
    10-10 20:25:13.239: V/GCMBaseIntentService(590): Releasing wakelock
    10-10 20:25:18.398: D/dalvikvm(424): GC_EXPLICIT freed 108 objects / 6936 bytes in 150ms
    10-10 20:25:23.417: D/dalvikvm(528): GC_EXPLICIT freed 424 objects / 27712 bytes in 168ms
    10-10 20:25:28.447: D/dalvikvm(259): GC_EXPLICIT freed 84 objects / 3800 bytes in 166ms
    10-10 20:25:33.447: D/dalvikvm(368): GC_EXPLICIT freed 60 objects / 2840 bytes in 142ms
    10-10 20:25:38.487: D/dalvikvm(233): GC_EXPLICIT freed 1611 objects / 103896 bytes in 182ms
    10-10 20:26:01.078: D/SntpClient(72): request time failed: java.net.SocketException: Address family not supported by protocol
    10-10 20:26:17.407: I/EventLogService(233): Aggregate from 1349879352940 (log), 1349879165305 (data)

    Please help me………

  21. I have this message on my logcat
    Received error PHONE_REGISTRATION_ERROR
    GCMRegistrar : internal error: retry receiver class not set yet

    GCMBaseIntentService : handleRegistration: registrationId = null, error = PHONE_REGISTRATION_ERROR, unregistered = null

  22. Please help

  23. i develop an apps..video suveilance apps
    i wan to push notification use GCM when motion is detect..
    when motion is detect, server run file.bat and send message to device..
    how possible can i do that??

  24. can we have JAVA/JSP oriented Server Code ??? anybody can do ???

  25. private void checkNotNull(Object reference, String name) {
    if (reference == null) {
    throw new NullPointerException(
    getString(R.string.error_config, name));
    }
    }
    i am getting error in getString method

  26. Your Blog is Very nice.Am also Using this code but am getting ffffff only not getting any Registration Id.Whats the Wrong. And How can i send and receive Push-notifications.

  27. What Actually am Getting When Run this Project

  28. please help me:
    My LogCat Shown like this:

    02-06 14:43:20.724: I/AndroidRuntime(375): NOTE: attach of thread ‘Binder Thread #3’ failed
    02-06 14:43:21.624: I/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.androidhive.pushnotifications/.RegisterActivity }
    02-06 14:43:21.704: I/AndroidRuntime(383): NOTE: attach of thread ‘Binder Thread #3’ failed
    02-06 14:43:26.894: I/ActivityManager(58): Displayed activity com.androidhive.pushnotifications/.RegisterActivity: 5244 ms (total 5244 ms)
    02-06 14:43:28.724: W/InputManagerService(58): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@440cedd0
    02-06 14:43:31.284: W/KeyCharacterMap(316): No keyboard for id 0
    02-06 14:43:31.284: W/KeyCharacterMap(316): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
    02-06 14:43:50.944: I/ActivityManager(58): Starting activity: Intent { cmp=com.androidhive.pushnotifications/.MainActivity (has extras) }
    02-06 14:43:51.053: E/URL(316): > http://localhost/Gcm/
    02-06 14:43:51.333: I/ActivityManager(58): Displayed activity com.androidhive.pushnotifications/.MainActivity: 356 ms (total 356 ms)
    02-06 14:43:52.013: I/GCMIntentService(316): Device registered: regId = APA91bFPkAJO0ymmVok3bPZy9_BOmztLD0vLgBuuSxt9luTO2CeL-yCRJ8y8q66HXOpYZeh2E3GC7Vrk9Ho9eHpi7nZ5xIMOlH83ogW4llEdpFsNGPAgGOS1LRVLnyNOPbZiyxX6f5IfA6hFqr3Ds7FrAXsSu0VQQUx_av6ZRhbP-vddBYPGu8k
    02-06 14:43:52.023: E/URL(316): > http://localhost/Gcm/
    02-06 14:43:53.384: E/URL(316): > http://localhost/Gcm/
    02-06 14:43:54.404: E/URL(316): > http://localhost/Gcm/
    02-06 14:43:58.006: E/URL(316): > http://localhost/Gcm/
    02-06 14:43:59.104: E/URL(316): > http://localhost/Gcm/
    02-06 14:44:07.164: E/URL(316): > http://localhost/Gcm/
    02-06 14:44:08.524: E/URL(316): > http://localhost/Gcm/
    02-06 14:44:25.434: E/URL(316): > http://localhost/Gcm/
    02-06 14:44:27.265: E/URL(316): > http://localhost/Gcm/

  29. I have created GCM Service as like your code, but I did get output in browser like No devices registered! only after all I have registred Two devices.

    Any Suggestion

  30. Thanks its worked for me.You are doing great job for beginners

  31. Thanks man!!Really simple tutorial to understand what does what.It helped me to understand following:
    1.BroadcastReceiver: Receives GCM messages and delivers them to an application-specific GCMBaseIntentService subclass.
    2.GCMRegistrar:Utilities for device registration.
    3.GCMIntentService:IntentService responsible for handling communication from Google Cloud Messaging service.

  32. This tutorial is deprecated. Please update it. Yeah I know Google changes apis which is a headache for lot of people but this is how things are..

  33. getString(R.string.error_config, name)); this line throwing this error

    error_config cannot be resolved or is not a field
    – Line breakpoint:PushAndroidActivity [line: 44] –
    checkNotNull(Object, String).
    Can you give some thing like if you add anything in your server side the notification has to come in android also,and same like while we add in android also it has to come notification.Can you explain how to do.

  34. thanks it is tutorial very useful

  35. i got the registration id now pls tell me how to proceed with this,
    how to use the registration id and send and receive messages

    • Hello Krunal,
      You now need to send this id to your server where it will be saved to the server db and this id will be used to send gcm from server, you just need to use to get the push data and use it accordingly.

  36. I really like your blog. But I am getting some errors. I would really appreciate if you help. Error are:
    1. SENDER_ID can not be resolved to a variable
    2. main can not be resolved or is not a field
    3. display can not be resolved or is not a field

    Thank you

  37. here is the error log. Please help!

    [2013-10-04 14:23:27 – PushAndroidActivity] Dx
    trouble processing “javax/crypto/SealedObject.class”:

    Ill-advised or mistaken usage of a core class (java.* or javax.*)
    when not building a core library.

    This is often due to inadvertently including a core library file
    in your application’s project, when using an IDE (such as
    Eclipse). If you are sure you’re not intentionally defining a
    core class, then this is the most likely explanation of what’s
    going on.

    However, you might actually be trying to define a class in a core
    namespace, the source of which you may have taken, for example,
    from a non-Android virtual machine project. This will most
    assuredly not work. At a minimum, it jeopardizes the
    compatibility of your app with future versions of the platform.
    It is also often of questionable legality.

    If you really intend to build a core library — which is only
    appropriate as part of creating a full virtual machine
    distribution, as opposed to compiling an application — then use
    the “–core-library” option to suppress this error message.

    If you go ahead and use “–core-library” but are in fact
    building an application, then be forewarned that your application
    will still fail to build or run, at some point. Please be
    prepared for angry customers who find, for example, that your
    application ceases to function once they upgrade their operating
    system. You will be to blame for this problem.

    If you are legitimately using some code that happens to be in a
    core package, then the easiest safe alternative you have is to
    repackage that code. That is, move the classes in question into
    your own package namespace. This means that they will never be in
    conflict with core system classes. JarJar is a tool that may help
    you in this endeavor. If you find that you cannot do this, then
    that is an indication that the path you are on will ultimately
    lead to pain, suffering, grief, and lamentation.

    [2013-10-04 14:23:27 – PushAndroidActivity] Dx 1 error; aborting
    [2013-10-04 14:23:27 – PushAndroidActivity] Conversion to Dalvik format failed with error 1

  38. Please devote some time to format the code like other site do, so that the is more readable and easy to understand just by looking it.
    Secondly, make the area where the content is shown bit more wide, so that more content (specially long statements) can be shown on a single line.

    Without these two things the efforts you made in preparing this tutorial is wasted, my attention is diverting while I see the code.

    Thank You

  39. hi my i hv a problem with dis code
    my logcat is here…

    04-10 18:46:49.190: E/AndroidRuntime(1363): FATAL EXCEPTION: main
    04-10 18:46:49.190: E/AndroidRuntime(1363): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gcm_demo/com.example.gcm_demo.PushAndroidActivity}: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.ActivityThread.access$600(ActivityThread.java:141)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.os.Handler.dispatchMessage(Handler.java:99)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.os.Looper.loop(Looper.java:137)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.ActivityThread.main(ActivityThread.java:5039)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at java.lang.reflect.Method.invokeNative(Native Method)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at java.lang.reflect.Method.invoke(Method.java:511)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at dalvik.system.NativeStart.main(Native Method)
    04-10 18:46:49.190: E/AndroidRuntime(1363): Caused by: java.lang.UnsupportedOperationException: Device does not have package com.google.android.gsf
    04-10 18:46:49.190: E/AndroidRuntime(1363): at com.google.android.gcm.GCMRegistrar.checkDevice(GCMRegistrar.java:98)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at com.example.gcm_demo.PushAndroidActivity.onCreate(PushAndroidActivity.java:21)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.Activity.performCreate(Activity.java:5104)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
    04-10 18:46:49.190: E/AndroidRuntime(1363): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
    04-10 18:46:49.190: E/AndroidRuntime(1363): … 11 more

  40. Cannot import GCMBaseIntentService show error please help

  41. Hi I got the blank registration id it shows me only fffff and nothing back reg id with this .How to get register id
    Thanks

Leave a reply to fundroiding Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.