Anyone doing C2DM on Android
Asked Answered
T

2

10

I need to implement c2dm in my app. Is there anyone who is also doing this? Please help..some tutorials will be very helpful OR if you have completed your c2dm implementation then a tutorial is more than appreciated.

Please help.

Tunnell answered 25/10, 2010 at 12:12 Comment(5)
i implement C2DM and it working fine for me. you can check this link github.com/commonsguy/cw-advandroid/tree/… i hope this will help you.Overstreet
How do you implement the third party server part. Which language did you use -- php/pyhtong?? Can you help me server side implmentation?Tunnell
server side implementation in done by using php. simple it receive registration_id and device_id from device and strore it a table. server get an authentication token from c2dm server by using curl URL(given in example), server return a auth token then use next URL to send notification to device.Overstreet
Hi mudit can you please help me?Overstreet
i tried the link given by you and i am getting E/AndroidRuntime(1508): java.lang.RuntimeException: Unable to instantiate service com.commonsware.android.c2dm.C2DMReceiver: java.lang.ClassNotFoundException: com.commonsware.android.c2dm.C2DMReceiver in loader dalvik.system.PathClassLoader[/data/app/com.commonsware.android.c2dm-2.apk] .. Can you please help me to solve this and moreover tell me how server will send messagesUnhandled
S
26

I went ahead and downloaded the Chrome2Phone source code for android and understood how it works through that example, I had the most trouble implementing the server side of the App.

Download it from: http://code.google.com/p/chrometophone/source/checkout

or svn it:

svn checkout http://chrometophone.googlecode.com/svn/trunk/ chrometophone-read-only

Basic things you should understand.

In the C2DMBaseReciever class you have:

@Override
    public final void onHandleIntent(Intent intent) {
        try {
            Context context = getApplicationContext();
            if (intent.getAction().equals(REGISTRATION_CALLBACK_INTENT)) {
                handleRegistration(context, intent);
            } else if (intent.getAction().equals(C2DM_INTENT)) {
                onMessage(context, intent);
            } else if (intent.getAction().equals(C2DM_RETRY)) {
                C2DMessaging.register(context, senderId);
            }
        } finally {
            //  Release the power lock, so phone can get back to sleep.
            // The lock is reference counted by default, so multiple 
            // messages are ok.

            // If the onMessage() needs to spawn a thread or do something else,
            // it should use it's own lock.
            mWakeLock.release();
        }
    }

This method recieves the intents from the C2DM service and handles them.

In the handleRegistration method you will see some code that looks like:

} else {
            try {
                onRegistrered(context, registrationId);
                C2DMessaging.setRegistrationId(context, registrationId);
                //Add some code here to send your server the registration ID for this phone.
            } catch (IOException ex) {
                Log.e(TAG, "Registration error " + ex.getMessage());
            }
        }

You then have to use the google oAuth login service to register your server to the service, once that is done you can send a message. When I was testing I was using curl to send http post requests to the server.

To register from the server:

curl https://www.google.com/accounts/ClientLogin -d Email=theEmailYouWhitelisted -d Passwd=pass****word -d accountType=HOSTED_OR_GOOGLE -d source=Google-cURL-Example -d service=ac2dm

You will get a message with an auth id. You then use that to send the messages. To send a message use:

curl --header "Authorization: GoogleLogin auth=**authFromRegistrationAbove**" "https://android.apis.google.com/c2dm/send" -d registration_id=**phoneRegistrationId(reciever)** -d "data.message=StringToPass" -d collapse_key=something -k

Download curl from: CURL

Hope this helps.

Satiated answered 25/10, 2010 at 13:26 Comment(6)
I get the auth token fine, but running curl on /send returns "...<H1>Unauthorized</H1><H2>Error 401</H2>...". Any idea what might be wrong?Heffron
Are you sure you are using the token correctly? you have to take a substring from the response.Satiated
Everything seems fine, try registering with a bogus email and/or password, so that you know its the wrong credentials, check if that returns an Auth Token, if it does, something might be wrong with your registration, if it doesnt then we can rule that out. I just reran my test server that I had described here and its still working just fine, my guess would be you are either not white listed or you are using the wrong email or something like that.Satiated
Using another email-address gives me Error=BadAuthentication. Weird.Heffron
This is the exec I use in the PHP server. Copy it and try it out, It works. exec("curl --header \"Authorization: GoogleLogin auth=".$auth."\" \"https://android.apis.google.com/c2dm/send\" -d registration_id=".$id." -d \"data.message=".$mensaje."\" -d collapse_key=something -k");Satiated
@Satiated Thanks, I changed the account from a hosted one to a gmail; and remembered to use "auth=" rather than "Auth=". That fixed it.Heffron
D
1

a tutorial about c2dm client/server registration and sending/receiving of messages.

http://android.arnodenhond.com/tutorials/cloud-to-device-messaging

  • intent to request a registration id
  • receiver to receive registration id
  • url to call for registering the server
  • url to call for sending a message
  • broadcast receiver to receive the message
Demented answered 23/6, 2011 at 20:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.