21

I am creating the Android application where C2DM push notification is used. But i have a problem in creating the php code to use c2dm for sending messages. please guide me how to use the php code to send the messages. Actually there is a problem regarding this that how to get client auth token. I have seen the http://code.google.com/android/c2dm/index.html#server url but according to this i have created the android application and i got the registration id also and i also send to the user but how server uses this to send the application.

is there anything needed for the server from the android device to send the messages?.

Josh Clemm
  • 2,966
  • 20
  • 21
Amit Thaper
  • 2,117
  • 4
  • 26
  • 49

5 Answers5

45

To register your own server system and obtain the Authorise Tokens (this is what Cpt. Ohlund proposed):

function googleAuthenticate($username, $password, $source="Company-AppName-Version", $service="ac2dm") {    


        session_start();
        if( isset($_SESSION['google_auth_id']) && $_SESSION['google_auth_id'] != null)
            return $_SESSION['google_auth_id'];

        // get an authorization token
        $ch = curl_init();
        if(!ch){
            return false;
        }

        curl_setopt($ch, CURLOPT_URL, "https://www.google.com/accounts/ClientLogin");
        $post_fields = "accountType=" . urlencode('HOSTED_OR_GOOGLE')
            . "&Email=" . urlencode($username)
            . "&Passwd=" . urlencode($password)
            . "&source=" . urlencode($source)
            . "&service=" . urlencode($service);
        curl_setopt($ch, CURLOPT_HEADER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);    
        curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        // for debugging the request
        //curl_setopt($ch, CURLINFO_HEADER_OUT, true); // for debugging the request

        $response = curl_exec($ch);

        //var_dump(curl_getinfo($ch)); //for debugging the request
        //var_dump($response);

        curl_close($ch);

        if (strpos($response, '200 OK') === false) {
            return false;
        }

        // find the auth code
        preg_match("/(Auth=)([\w|-]+)/", $response, $matches);

        if (!$matches[2]) {
            return false;
        }

        $_SESSION['google_auth_id'] = $matches[2];
        return $matches[2];
    }

To send a message to a phone:

// $msgType: all messages with same type may be "collapsed": if multiple are sent,
// only the last will be received by phone. 
function sendMessageToPhone($authCode, $deviceRegistrationId, $msgType, $messageText) {

            $headers = array('Authorization: GoogleLogin auth=' . $authCode);
            $data = array(
                'registration_id' => $deviceRegistrationId,
                'collapse_key' => $msgType,
                'data.message' => $messageText //TODO Add more params with just simple data instead           
            );

            $ch = curl_init();

            curl_setopt($ch, CURLOPT_URL, "https://android.apis.google.com/c2dm/send");
            if ($headers)
            curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_POST, true);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);


            $response = curl_exec($ch);

            curl_close($ch);

            return $response;
        }
Nino van Hooff
  • 3,677
  • 1
  • 36
  • 52
Yar
  • 4,543
  • 2
  • 35
  • 42
  • 1
    Session uses cookies, doesn't it? How does that work for a server script? – Thomas Ahle Mar 22 '11 at 11:47
  • Yes, session uses cookies in general. Here session is used ONLY to omit unnecessary authentication request, if auth token is known. That is all. If you call this script from another script this mechanism is not needed as your script knows if it already possesses an auth token, doesn't it? – Yar Mar 22 '11 at 17:45
  • If I run the script from cron, I need a way to store the auth token. I guess that won't be session. – Thomas Ahle Mar 22 '11 at 23:19
  • Do/should you get a new auth token everytime or is this something that can be stored? (DB/filesystem/etc) – Mr Zorn Sep 04 '11 at 16:48
  • @MrZorn : you should get the auth token from time to time , because i think that it expires , so every time you got the new auth token you can update the value on your database – Houcine Nov 28 '11 at 17:24
  • Hey, you said the sendMessageToPhone() function is used for sending a message to a phone. So, how to send a message to many phones? – Kannika Jan 03 '12 at 03:17
  • It works. However, how can we handle CAPTCHA request as Google may request a CAPTCHA: https://developers.google.com/accounts/docs/AuthForInstalledApps? – Bao Le Apr 18 '12 at 03:59
7

I've created an example in my blog in working with Android C2DM. I use Zend Framework and a custom component that I wrote out. It should give you the basic information that you will need in order to handle your Android C2DM implementation in PHP.

Android C2DM PHP w/ Zend Framework: http://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/

Regards,

Mike

3

Check this out: http://www.toppa.com/2010/google-clientlogin-php-example/ Otherwise I will get back to you, as I will try C2DM later this week.

Cpt.Ohlund
  • 2,589
  • 1
  • 20
  • 15
  • i want to ask 1 new thing that from where token is to be generated from android device or from application server. here android device is used to receive only messages sent by the application server. i don't want to sent messages from the android device – Amit Thaper Nov 09 '10 at 12:39
  • Hi Please tell me who will send the request for authtoken its from android application or endserver. – Amit Thaper Nov 15 '10 at 08:17
1

As C2DM has been officially deprecated (google c2dm)

I recommend using the new GCM API as described in the following link: GCM Php implementation

Community
  • 1
  • 1
eliav
  • 11
  • 1
0

I have tried using the php code that was accepted as the correct answer but it is not working. I am getting the response http code as "0".

I found the same code in the following link

Need help from the experts out here.

Inder Kumar Rathore
  • 39,458
  • 17
  • 135
  • 184
user703555
  • 265
  • 1
  • 7
  • 14