Implements C2dm Notifications
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.



I am deprecating this project as I am working on a ZF 1.x style Push
Notification server. This is actually shaping up in a separate repository and
handles everything that this current C2DM implementation does as well.

See Zend_Mobile:“” which I am working
for ZF 1.12 during the proposal process.

Implementing C2DM Push Notifications for Google Android

This project makes usage of libraries from Zend Framework in order to do the authentication for Google. Each response from the Google C2DM Service is encapsulated.
In order to utilize this functionality you must sign up for a c2dm account:

Quick Example of Sending a Notification Through C2DM

Sorry about the formatting, textile in GitHub really does not like whitespace in the pre-code blocks.

    define('C2DM_USERNAME', '');
    define('C2DM_PASSWORD', 'myExamplePassword');
    define('C2DM_APPLICATION', 'ExampleApp');

    function getBackOffTime($fails, Zend_Http_Response $response)
        if ($retry = $response->getHeader('Retry-After')) {
            if (is_string($retry)) {
                $retry = strtotime($retry) - time();
        return (int) $retry;
        return intval(pow(2, $fails) - 1);

    try {
        $client = Zend_GData_ClientLogin::getHttpClient(
    } catch (Zend_Gdata_App_CaptchaRequiredException $cre) {
        // manual login is required
        echo 'URL of CAPTCHA image: ' . $cre->getCaptchaUrl() . PHP_EOL;
        echo 'Token ID: ' . $cre->getCaptchaToken() . PHP_EOL;
    } catch (Zend_Gdata_App_AuthException $ae) {
        echo 'Problem authenticating: ' . $ae->exception() . PHP_EOL;

    $c2dm = new Zend_Service_Google_C2dm();

    $registrationId = 'THE_REGISTRATION_ID_OF_THE_PHONE';
    $collapseKey    = 'aUniqueStackableIdentifier';
    $data = array(
        'key' => 'value-pairs'
    $message = new Zend_Service_Google_C2dm_Message(

    $failCount = 0;
    do {
        $tryAgain = false;
        try {
        } catch (Zend_Service_Google_C2dm_Exception_QuotaExceeded $e) {
            $tryAgain = true;
            usleep(getBackOffTime($failCount, $c2dm->getLastResponse()) * 1000);
        } catch (Zend_Service_Google_C2dm_Exception_ServerUnavailable $e) {
            $tryAgain = true;
            usleep(getBackOffTime($failCount, $c2dm->getLastResponse()) * 1000);
        } catch (Zend_Service_Google_C2dm_Exception_InvalidRegistration $e) {
            // do not attempt to send a message to this id again
        } catch (Zend_Service_Google_C2dm_Exception_NotRegistered $e) {
            // do not attempt to send a message to this id again
        } catch (Zend_Service_Google_C2dm_Exception_DeviceQuotaExceeded $e) {
            // you may attempt to retry, however, it may be best to let it go away
        } catch (Zend_Service_Google_C2dm_Exception_MessageTooBig $e) {
            // you may want to log this one and find the offending code and reduce the message size
        } catch (Zend_Service_Google_C2dm_Exception_MissingCollapseKey $e) {
            // you may want to log this one and find the offending code to ensure that this is pushed in
        } catch (Zend_Service_Google_C2dm_Exception $e) {
            // all of the rest of the exceptions are fatal in nature, log the exception and kill the client
            // sometimes we simply just need to generate a new auth token.
    } while ($tryAgain);

Further Reading

Checkout my blog on Android C2DM With PHP and Zend Framework