Asked  7 Months ago    Answers:  5   Viewed   35 times

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 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?.



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") {    

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

        // get an authorization token
        $ch = curl_init();
            return false;

        curl_setopt($ch, CURLOPT_URL, "");
        $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


        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, "");
            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);


            return $response;
Wednesday, March 31, 2021
answered 7 Months ago

Found a solution on the CI forums.

Exchange Email Class Patch

It is initiating TLS after SMTP server has been connected.

Worked for me. Jeff

Saturday, May 29, 2021
answered 5 Months ago

the problem is that the excel file is saved version 5, then I should Register with the 2007 version (. xlsx) and the Arabic values ??appear correctly in file properities:

$obj_writer = PHPExcel_IOFactory::createWriter($this->excel, 'Excel2007');
Saturday, May 29, 2021
answered 5 Months ago

It turns out the notifications worked fine whether the app was running or not. The problem was when the app was force-closed.

I think the actual problem is something along the lines of the BroadcastReceiver being killed when the app is force closed but I'm not sure.

This is just to close the question off because it's unlikely to be solved now that I realise the problem is different to the question title.

Wednesday, August 25, 2021
answered 2 Months ago



I don't have much knowledge about C2DM. Use GCM, it's very easy to implement and configure.

Friday, September 3, 2021
Aleksejs Mjaliks
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :