Search code examples
androidandroid-c2dm

C2DMRegistrar.25 register: http error 401


i'm trying to use C2DM client with this tutorial:http://www.vogella.de/articles/AndroidCloudToDeviceMessaging/article.html

I'm using this code for the client (a little different from tutorial code): http://www.vogella.de/code/de.vogella.android.c2dm/codestartpage.html

I only changued the package name, i merged the two packages in one, com.DemoC2DM and i changued some lines from the manifest to get the code working with the new and only package, but probably i'm doing something wrong in the manifest changued lines because i'm getting this error when i try to get a registration ID with the client:

01-16 12:45:52.133: ERROR/Super(1517): Starting registration
01-16 12:45:52.375: DEBUG/GoogleLoginService(1226): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
01-16 12:45:53.640: DEBUG/dalvikvm(1226): GC_FOR_MALLOC freed 7811 objects / 420368 bytes in 53ms
01-16 12:45:53.640: DEBUG/NativeCrypto(1226): Freeing OpenSSL session
01-16 12:45:53.945: WARN/DefaultRequestDirector(1226): Authentication error: Unable to respond to any of these challenges: {}
01-16 12:45:53.945: DEBUG/C2DMRegistrar(1226): [C2DMRegistrar.25] register: http error 401
01-16 12:45:53.945: ERROR/C2DMRegistrar(1226): [C2DMReg] handleRequest caught org.apache.http.auth.AuthenticationException

This is the code of my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.DemoC2DM" android:versionCode="1"
    android:versionName="1.0">
    <permission android:name="com.DemoC2DM.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="com.DemoC2DM.permission.C2D_MESSAGE" />
    <!-- Permissions -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.USE_CREDENTIALS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="RegisterActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".C2DMReceiver" />


        <!-- Only C2DM servers can send messages for the app. If permission is 
            not set - any other app can generate it -->
        <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <!-- Receive the actual message -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="de.vogella.android.c2dm" />
            </intent-filter>
            <!-- Receive the registration id -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="de.vogella.android.c2dm" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

and this is the code of the original manifest, without my package changes:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.vogella.android.c2dm" android:versionCode="1"
    android:versionName="1.0">
    <permission android:name="de.vogella.android.c2dm.permission.C2D_MESSAGE"
        android:protectionLevel="signature" />
    <uses-permission android:name="de.vogella.android.c2dm.permission.C2D_MESSAGE" />
    <!-- Permissions -->
    <uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />


    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name="RegisterActivity" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".C2DMReceiver" />


        <!-- Only C2DM servers can send messages for the app. If permission is 
            not set - any other app can generate it -->
        <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <!-- Receive the actual message -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="de.vogella.android.c2dm" />
            </intent-filter>
            <!-- Receive the registration id -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="de.vogella.android.c2dm" />
            </intent-filter>
        </receiver>
    </application>
</manifest>

EDIT: adding exception after Sumant changes:

01-16 13:16:39.187: DEBUG/C2DMRegistrar(1226): [C2DMRegistrar.25] register: http error 401
01-16 13:16:39.187: ERROR/C2DMRegistrar(1226): [C2DMReg] handleRequest caught org.apache.http.auth.AuthenticationException
01-16 13:16:39.218: DEBUG/GoogleLoginService(1226): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
01-16 13:16:39.289: WARN/ActivityManager(1086): Unable to start service Intent { act=com.motorola.motosync.service.intent.action.CHECK_PING flg=0x4 cmp=com.motorola.motosync/.service.EasService (has extras) }: not found
01-16 13:16:39.304: DEBUG/GoogleLoginService(1226): onBind: Intent { act=android.accounts.AccountAuthenticator cmp=com.google.android.gsf/.loginservice.GoogleLoginService }
01-16 13:16:40.218: DEBUG/dalvikvm(1226): GC_FOR_MALLOC freed 8422 objects / 453352 bytes in 71ms
01-16 13:16:40.523: DEBUG/AndroidRuntime(1535): Shutting down VM
01-16 13:16:40.523: WARN/dalvikvm(1535): threadid=1: thread exiting with uncaught exception (group=0x4001d7e0)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): FATAL EXCEPTION: main
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): java.lang.RuntimeException: Unable to instantiate receiver com.google.android.c2dm.C2DMBroadcastReceiver: java.lang.ClassNotFoundException: com.google.android.c2dm.C2DMBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.DemoC2DM-2.apk]
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2789)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.app.ActivityThread.access$3200(ActivityThread.java:125)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.os.Looper.loop(Looper.java:123)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.app.ActivityThread.main(ActivityThread.java:4627)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at java.lang.reflect.Method.invokeNative(Native Method)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at java.lang.reflect.Method.invoke(Method.java:521)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at dalvik.system.NativeStart.main(Native Method)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535): Caused by: java.lang.ClassNotFoundException: com.google.android.c2dm.C2DMBroadcastReceiver in loader dalvik.system.PathClassLoader[/data/app/com.DemoC2DM-2.apk]
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2780)
01-16 13:16:40.539: ERROR/AndroidRuntime(1535):     ... 10 more
01-16 13:16:40.562: WARN/ActivityManager(1086):   Force finishing activity com.DemoC2DM/.RegisterActivity

Solution

  • Hi just make the following changes in your manifest file & then try to use.

     <!-- Only C2DM servers can send messages for the app. If permission is 
            not set - any other app can generate it -->
        <receiver android:name="com.google.android.c2dm.C2DMBroadcastReceiver"
            android:permission="com.google.android.c2dm.permission.SEND">
            <!-- Receive the actual message -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.RECEIVE" />
                <category android:name="com.DemoC2DM" />
            </intent-filter>
            <!-- Receive the registration id -->
            <intent-filter>
                <action android:name="com.google.android.c2dm.intent.REGISTRATION" />
                <category android:name="com.DemoC2DM" />
            </intent-filter>
        </receiver>