<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mike Willbanks</title>
	<atom:link href="http://blog.digitalstruct.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.digitalstruct.com</link>
	<description>Getting inside the mind of a php developer.</description>
	<lastBuildDate>Thu, 22 Dec 2011 04:04:16 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>RPM Packaging &#8211; Building and Deploying your own PHP</title>
		<link>http://blog.digitalstruct.com/2011/12/21/rpm-packaging-building-and-deploying-your-own-php/</link>
		<comments>http://blog.digitalstruct.com/2011/12/21/rpm-packaging-building-and-deploying-your-own-php/#comments</comments>
		<pubDate>Thu, 22 Dec 2011 03:54:46 +0000</pubDate>
		<dc:creator>Mike Willbanks</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.digitalstruct.com/?p=199</guid>
		<description><![CDATA[I&#8217;ve been building all sorts of RPM&#8217;s lately, from vim 7.3 to mirroring the zend server repository and building pecl extensions. In the PHP world, one might ask why not just build it from source? Well, an RPM IS built from source and then distributed to many servers &#8211; we can ensure that we have [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been building all sorts of RPM&#8217;s lately, from vim 7.3 to mirroring the zend server repository and building pecl extensions.  In the PHP world, one might ask why not just build it from source?  Well, an RPM <strong>IS</strong> built from source and then distributed to many servers &#8211; we can ensure that we have the same packages on each, we can maintain the same versions and if you&#8217;ve read my <a href="/2011/02/09/building-and-maintaining-a-pear-server-with-pirum/">previous post on Pirum</a> you will know that I also like mirroring PEAR packages.  It allows us to simply maintain our versions on each server and lower the maintenance in a larger environment. Not to mention, since it is on the inside of our network, it is insanely quick for the downloading of the packages and maintaining our servers.</p>
<p>If you are not doing packaging, I hope this entices you to start doing some packaging.  I will show you an example of building a PHP RPM based on the distribution (as it is actually how I ensure this server is versioned appropriately and I don&#8217;t lose my build from source scripts).  Oh; and I&#8217;ve only been promising this blog post for approximately 6+ months &#8211; so those expecting it; here it is!</p>
<h2>Overview</h2>
<p>To build an RPM is fairly easy, however, there are a few tools that you will need in order to build everything out:</p>
<ul>
<li>rpmbuild</li>
<li>rpm-devel</li>
<li>rpm-libs</li>
</ul>
<p>Once you have these packages, you have everything that you will need to start building out your package.</p>
<h2>The Mechanics of a Package</h2>
<p>There are a few parts of an RPM package that you will need to know in order to build it.</p>
<ul>
<li>A Specification File</li>
<li>The Source</li>
</ul>
<p>The specification file will explain to the rpmbuild utility how we are going to be building out the package and the source is what we are going to be building from.  Pretty plain and simple, right?</p>
<h3>Specification File</h3>
<p>When we are building out an RPM specification file, there are many components that we need to think through as well as a large amount of macros.  We will be avoiding many of the macros as they do sometimes change the path as to where we may be installing from.  Let&#8217;s start with the full spec file and we will explain from there. This file needs to be in &#8220;/usr/src/redhat/SPECS&#8221;.</p>
<pre>
Name:           php
Version:        5.3.8
Release:        1%{?dist}
Summary:        PHP is a widely-used general-purpose scripting language.

Group:          Development/Languages
License:        PHP License v3.01
URL:            http://www.php.net
Source0:        http://www.php.net/distributions/php-%{version}.tar.gz
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-buildroot

Obsoletes:      php

%description
PHP is a widely-used general-purpose scripting language that is especially
suited for Web development and can be embedded into HTML.

%prep
%setup -q -n %{name}-%{version}
%build
EXTENSION_DIR=%{_libdir}/php/modules; export EXTENSION_DIR
%configure --with-layout=GNU --with-libdir=lib64 --with-enchant \
--enable-fpm --with-gd --enable-intl --enable-mbstring --enable-pcntl \
--enable-soap --enable-sockets --enable-sqlite-utf8 --enable-zip --with-zlib \
--with-curl --with-jpeg-dir --with-png-dir --with-zlib-dir --with-gettext \
--with-mcrypt --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-pdo-sqlite --with-tidy --with-pear=%{_datadir}/php/pear --disable-debug

make %{?_smp_mflags}

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{_initrddir}
install -Dp -m0755 sapi/fpm/init.d.php-fpm.in %{buildroot}%{_initrddir}/php-fpm
%{__make} install INSTALL_ROOT="%{buildroot}"

%clean
rm -rf %{buildroot}

%post
%/sbin/chkconfig --add php-fpm
%/sbin/chkconfig --level 2345 php-fpm on

%preun
if [ "$1" = 0 ] ; then
    /sbin/service php-fpm stop > /dev/null 2>&#038;1
    /sbin/chkconfig --del php-fpm
fi
exit 0

%postun
if [ "$1" -ge 1 ]; then
    /sbin/service php-fpm condrestart > /dev/null 2>&#038;1
fi
exit 0

%files
%defattr(-,root,root,-)
%{_bindir}/*
%{_sbindir}/*
%{_includedir}/*
%{_libdir}/*
%{_mandir}/man1/php*
%{_sysconfdir}/*
%{_datadir}/*
%{_initrddir}/*
%exclude /.channels
%exclude /.depdb
%exclude /.depdblock
%exclude /.filemap
%exclude /.lock

%changelog
* Wed Dec 21 2011 Mike Willbanks <mike @_________.com> - 5.3.8-1
- Updated to 5.3.8
* Tue Feb 23 2011 Mike Willbanks </mike><mike @_________.com> - 5.3.5-1
- Initial Package
</mike></pre>
<h4>The Header</h4>
<pre>
Name:           php
Version:        5.3.8
Release:        1%{?dist}
Summary:        PHP is a widely-used general-purpose scripting language.

Group:          Development/Languages
License:        PHP License v3.01
URL:            http://www.php.net
Source0:        http://www.php.net/distributions/php-%{version}.tar.gz
BuildRoot:      %{_tmppath}/%{name}-%{version}-%{release}-buildroot

Obsoletes:      php

%description
PHP is a widely-used general-purpose scripting language that is especially
suited for Web development and can be embedded into HTML.
</pre>
<p>There are several items here that are fairly self explanatory.  However, I would like to point out 3 of the items just for clarity on what they do.  As you can also see we use these as variables inside of themselves.</p>
<ul>
<li><strong>Source0:</strong> the base path of this is what the actual file is at /usr/src/redhat/SOURCES.  It should also be the same as where you are downloading from.</li>
<li><strong>BuildRoot:</strong> the directory where all of the RPM building should happen, this is where we actually do a build and install into that directory.</li>
<li><strong>Obsoletes:</strong> should be used if this package overrides existing packages in the distribution.</li>
</ul>
<h4>Macros</h4>
<p>RPM specifications contain several macros that automate much of the behavior for you in order to get everything packaged and up and running.  These are a bit out of scope of this blog post, however, you can see the ones that are being utilized from the % sign followed by the actual command.  See the <a href="http://www.rpm.org/max-rpm/ch-rpm-inside.html">Inside the Spec file page</a> to see what some of these mean.</p>
<h4>Making the RPM</h4>
<p>Now, making an RPM is much like compiling from source code.  There are macros that will help you with this such as <strong>%prep</strong>, <strong>%setup</strong>, <strong>%build</strong> and <strong>%install</strong>.  It may seem intimidating at first; but actually far easier than you might originally think.</p>
<pre>
%prep
%setup -q -n %{name}-%{version}
%build
EXTENSION_DIR=%{_libdir}/php/modules; export EXTENSION_DIR
%configure --with-layout=GNU --with-libdir=lib64 --with-enchant \
--enable-fpm --with-gd --enable-intl --enable-mbstring --enable-pcntl \
--enable-soap --enable-sockets --enable-sqlite-utf8 --enable-zip --with-zlib \
--with-curl --with-jpeg-dir --with-png-dir --with-zlib-dir --with-gettext \
--with-mcrypt --with-mysql=mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
--with-pdo-sqlite --with-tidy --with-pear=%{_datadir}/php/pear --disable-debug

make %{?_smp_mflags}

%install
rm -rf %{buildroot}
mkdir -p %{buildroot}%{_initrddir}
install -Dp -m0755 sapi/fpm/init.d.php-fpm.in %{buildroot}%{_initrddir}/php-fpm
%{__make} install INSTALL_ROOT="%{buildroot}"

%clean
rm -rf %{buildroot}
</pre>
<p><strong>%prep:</strong> is really just a container prior to %build and %setup; which contains the very important %setup! The %setup macro is used to unpack the sources.  Generally you might not use the options but in our specific case we want to ensure the directory that it is extracted to.  The <strong>-n</strong> creates the name of the build build directory where these sources will be extracted.  Don&#8217;t worry about the other param. </p>
<p><strong>%build: </strong> this parameter is basically anything that that you would like to be executed by sh prior to %configure <img src='http://blog.digitalstruct.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />   this can also be said for the previous sections.  In PHP our <strong>extremely</strong> important variable is our <em>EXTENSION_DIR</em> which is utilized for where our extensions will be placed.  We also have a %configure macro which is a short-hand for essentially running ./configure for our configure from source folks.  If you seriously need an explanation of this; well google is your friend.</p>
<p>Then we run the make command (remember we are really just talking with the sh shell at this point in time.  Don&#8217;t worry again about the macro contained in there&#8230; it is really just some semantics for jobs running simultaneously.  Now you should be ready for all of the installation!</p>
<p><strong>%install: </strong> is simply installing everything to our build root (aka &#8220;make install&#8221;).  Since I am using php-fpm and we have a init.d in file we can simply handle that through the install command.  As an additional note; if you want PHP to actually build to an RPM there is a single parameter that <strong>YOU MUST</strong> utilize: &#8220;INSTALL_ROOT&#8221;.  In many other cases this variable is different or not relevant.  However, we must use it for PHP to put it into the build root.  Miss this parameter and well; forget about it.  And we have %clean which simply cleans up anything in the build root.</p>
<h4>RPM Installation</h4>
<p>Finally; the good stuff.  We need to define how to install this on our systems.  This includes %pre, %post, %preun, %postun, %files and %changelog. Which simply defines %pre = pre-install, %post = post-install, %preun = pre-uninstall, %postun = post-uninstall, %files = the files to install and %changelog = what we&#8217;ve simply changed.  Seriously all of these are fairly self definable if you read the RPM documentation.  However, we should talk about %files&#8230;  Insideo %files, you <strong>MUST</strong> define every single file that will be built; if you do not&#8230; well then your RPM build will fail.</p>
<h3>The Source</h3>
<p>Whenever you define the source in the specification it <strong>MUST</strong> live inside of the &#8220;/usr/src/redhat/SOURCE&#8221; directory.  If it does not then it will simply not work.  Ensure you&#8217;ve downloaded the source prior to executing the build.</p>
<h3>Execution</h3>
<p>In order to execute the build you need to execute &#8220;rpmbuild -ba php.spec&#8221; within the &#8220;/usr/src/redhat/SPECS&#8221; directory.  After all of this you should have a RPM inside of the &#8220;/usr/src/redhat/RPMS&#8221; directory which you can install on the platform architectures that follow the current platform architecture.  Hope this article serves you well and happy building!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.digitalstruct.com/2011/12/21/rpm-packaging-building-and-deploying-your-own-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Building and Maintaining a PEAR Server with Pirum</title>
		<link>http://blog.digitalstruct.com/2011/02/09/building-and-maintaining-a-pear-server-with-pirum/</link>
		<comments>http://blog.digitalstruct.com/2011/02/09/building-and-maintaining-a-pear-server-with-pirum/#comments</comments>
		<pubDate>Thu, 10 Feb 2011 04:04:41 +0000</pubDate>
		<dc:creator>Mike Willbanks</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.digitalstruct.com/?p=193</guid>
		<description><![CDATA[Pirum is a simple PEAR channel server manager that was built by Fabien Potencier. The Pirum project allows you to easily setup a PEAR channel and publish your own packages quickly. This quick blog post / article will get you going with it in no time. Getting Started There are a few things that you [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://www.pirum-project.org/" onclick="window.open(this.href, '_blank');return false;">Pirum</a> is a simple PEAR channel server manager that was built by <a href="http://fabien.potencier.org/" onclick="window.open(this.href, '_blank');return false;">Fabien Potencier</a>.  The Pirum project allows you to easily setup a PEAR channel and publish your own packages quickly.  This quick blog post / article will get you going with it in no time.</p>
<h2>Getting Started</h2>
<p>There are a few things that you must have already to get going on this exercise (besides PHP and a Web Server *Note: you can do it without these):</p>
<ul>
<li>PEAR</li>
<li>SSH access</li>
</ul>
<h3>Installing Pirum</h3>
<p>To install Pirum we are simply going to be running a few commands against PEAR:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">pear channel-discover pear.pirum-project.org
pear <span style="color: #c20cb9; font-weight: bold;">install</span> pirum<span style="color: #000000; font-weight: bold;">/</span>Pirum-beta</pre></td></tr></table></div>

<p>Pirum should now be installed, we now need to setup the pirum.xml file to describe your server.</p>
<h3>Creating the PEAR Structure for Pirum</h3>
<p>Pirum requires a pirum.xml file that is a very simple XML file.  You place this into the directory that you would like your pear server to live.  Let&#8217;s say for the sakes of this post that it is /var/www/pear.  We create a file /var/www/pear/pirum.xml file that contains the following XML (to be explicit you will change the XML values to your liking)</p>
<pre code="xml">
&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; ?&gt;
&lt;server&gt;
  &lt;name&gt;pear.mydomain.com&lt;/name&gt;
  &lt;summary&gt;PEAR Server for MyDomain&lt;/summary&gt;
  &lt;alias&gt;mydomain&lt;/alias&gt;
  &lt;url&gt;http://pear.mydomain.com/&lt;/url&gt;
&lt;/server&gt;
</pre>
<p>The fields in the pirum.xml file are explained as follows:</p>
<ul>
<li>name: The name is typically the domain name to the PEAR channel.  You should keep this the same since this is how just about every PEAR channel references itself.</li>
<li>summary: A very short summary about what the channel is, generally the name of the project / components that sit inside of it</li>
<li>alias: utilized when upgrading or installing, you can reference a package by &#8220;pear install alias/package_name&#8221;</li>
<li>uri: The external URI that people will run &#8220;pear channel-discover uri&#8221;</li>
</ul>
<h3>Making it Work</h3>
<p>We can now build the main repository.  We will not add any packages at this point but to get the main repository running and viewable from a web server. We will need to run the command:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">pirum build <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>pear</pre></td></tr></table></div>

<p>At this point you should see that pirum is building out a few files such like the following:</p>
<pre>
   INFO   Building channel
   INFO   Building maintainers
   INFO   Building categories
   INFO   Building packages
   INFO   Building releases
   INFO   Building index
   INFO   Building feed
   INFO   Updating PEAR server files
   INFO   Command build run successfully
</pre>
<p>We now need to attach a web server to simply be able to access this directory.  In your virtual hosts file or dedicated server add an entry to /var/www/pear that can be accessed from the uri we created in the pirum.xml file.  You should not be able to see the index.html file for the packages that are installed in the repository (which is none at this point</p>
<h2>Creating a Package</h2>
<p>In order to create package, you should have some form of reusable code that is able to be deployed into a PEAR channel.  This might be a private or public repository &#8211; either way, it is the same exact process.  You must first create a <a href="http://pear.php.net/manual/en/guide.developers.package2.intro.php" onclick="window.open(this.href, '_blank');return false">package.xml</a> file for the individual package in a top level before your package.  Don&#8217;t worry, this is not needed on the same environment as we are going to create a .tgz package from it (tar gzip).  Once you have completed the package.xml file and have created your .tgz file, we are moments away from being able to add a package into pirum.</p>
<p>Once the package is up on server with pirum &#8211; utilize the following command:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">pirum add <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>pear package.tgz</pre></td></tr></table></div>

<p>This will add in the package and now you can install it through the pear repository you have just finished creating.  This same exact process is utilized in upgrading packages.</p>
<h2>Advanced Topics</h2>
<p>Sometimes the main PEAR channel is not going to do it for you.  For instance, I have had the need to locally create a PEAR package in our own repository to maintain versions between web servers as well as being able to quickly download and install the package locally.  This helps to maintain our versions and upgrade when we need to, not when a new web server is provisioned.</p>
<h3>Cloning a Package</h3>
<p>Cloning a PEAR package is as simple as downloading the package and modifying the channel in the package.xml file to match the channel of your package.  I have made this more simple and submitted a push request &#8211; however I currently have no response.  The clone command works under linux only at the moment but can be found in my <a href="https://github.com/mwillbanks/Pirum" onclick="window.open(this.href, '_blank');return false;">github fork of Pirum</a>.  To install it you simply need to replace the pirum file with the one provided in my repository.  This adds a new command &#8220;<strong>pirum clone-package</strong>&#8221; which allows you to clone an existing package and add it into your repository.</p>
<p><strong>Note: </strong> this does not mean it will handle dependencies for you, it will simply clone the package that it was asked to and not modify any dependencies.  Seeing as this is how it works, you cannot download two packages that require each other and expect them to be downloaded from the cloned packages without modifying the package.xml file again.</p>
<h4>Cloning Packages</h4>
<p>A simple package that we have utilized in the past and continue to is Zend Framework.  To add it to our repository we simply run:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="bash" style="font-family:monospace;">pirum clone-package <span style="color: #000000; font-weight: bold;">/</span>var<span style="color: #000000; font-weight: bold;">/</span>www<span style="color: #000000; font-weight: bold;">/</span>pear http:<span style="color: #000000; font-weight: bold;">//</span>pear.zfcampus.org<span style="color: #000000; font-weight: bold;">/</span>get<span style="color: #000000; font-weight: bold;">/</span>ZF-1.11.3.tgz</pre></td></tr></table></div>

<p>Under the hoods, this will download the package by utilizing file_get_contents for the download, executing tar command, running simple xml to locate the element in PHP do a string replace on the channel then re-package it and add it into your repository.</p>
<p>Once this is done your package is read to be installed by running:</p>
<pre code="bash">
pear install mydomain/ZF
</pre>
<h2>Conclusion</h2>
<p>Pirum makes it easy to control your own PEAR server.  Now that you understand how to get it going, utilize it.  It is a great tool and makes it far easier to continue adding new packages to your environment or publishing them to the world.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.digitalstruct.com/2011/02/09/building-and-maintaining-a-pear-server-with-pirum/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Android C2DM with PHP and Zend Framework</title>
		<link>http://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/</link>
		<comments>http://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/#comments</comments>
		<pubDate>Sun, 21 Nov 2010 06:17:19 +0000</pubDate>
		<dc:creator>Mike Willbanks</dc:creator>
				<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.digitalstruct.com/?p=165</guid>
		<description><![CDATA[Update: For the latest please see my github implementation, the code for the underlying Zend Framework implementation has been updated and is maintained there. So you&#8217;ve got a new fancy Android application and you want to be able to send push notifications to the phone. Either for synchronization purposes or for notifications. Since C2DM is [...]]]></description>
				<content:encoded><![CDATA[<p><strong>Update:</strong> For the latest please see my <a href="https://github.com/mwillbanks/Zend_Service_Google_C2dm">github implementation</a>, the code for the underlying Zend Framework implementation has been updated and is maintained there.</p>
<p>So you&#8217;ve got a new fancy Android application and you want to be able to send push notifications to the phone.  Either for synchronization purposes or for notifications.  Since <abbr title="Android Cloud to Device Messaging Framework"><a href="http://code.google.com/android/c2dm/index.html">C2DM</a></abbr> is fairly new and is currently in the labs it is rather difficult to find code that already handles sending out the notifications correctly.</p>
<p>This article will go through sending a push notification (or message) through the Android C2DM server utilizing PHP in the fashion of a Zend Framework component.</p>
<h2>Overview</h2>
<p>Sending a message through C2DM can be rather complex as there are fairly strict requirements that Google puts into place.  There are several areas that you need to be aware of as well as changes that may happen during the processing of a response.  I attempt to encapsulate some of this work in the class handling the sending, however, it is up to the code that sends the notification to properly handle the exceptions thrown.  This allows for extremely high flexibility and the ability to hook your own code into this feature.</p>
<h3>Requirements</h3>
<p>Before we begin there are a few requirements that you will need to have in order to utilize this code to be able to send information to the Android device and create your notification server.</p>
<ul>
<li><a href="http://www.php.net">PHP</a>
<ul>
<li><a href="http://framework.zend.com">Zend Framework</a></li>
<li><a href="http://pecl.php.net/package/gearman">PECL: Gearman</a> <em>* optional</em></li>
<li><a href="/2010/10/17/integrating-gearman-into-zend-framework/">Gearman Zend Framework Worker</a> <em>* optional</em></li>
</ul>
</li>
<li>Google
<ul>
<li>Google Account</li>
<li><a href="http://code.google.com/android/c2dm/signup.html">C2DM Account</a></li>
<li>Android 2.2+</li>
</ul>
</li>
</ul>
<h2>Implementing the C2DM Third-Party Application Server</h2>
<p>In order to implement the <a href="http://code.google.com/android/c2dm/index.html#server">C2DM Third-Party Application Server</a> we will be utilizing PHP and the Zend Framework.  Since Zend Framework already has built-in support for getting the authentication token we will make re-use of that component and simply provide a method for setting the token.</p>
<p>Next we need to ensure that we are properly handling the variances that are coming back from the C2DM server.  This can be login based, message based or based on what our user has done on their device.  Lets get to the code so we can dissect what needs to happen.</p>
<h3>The Library</h3>
<p>This library does not have includes in it as we utilize autoloading, if you need includes it is fairly easy to add them all in.  This is going to be a lot of code &#8211; beware.</p>
<p><strong>library/Zend/Service/Google/C2dm.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * Service Google C2dm
 * Implementation for Android Cloud to Device Messaging
 *
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/** 
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">const</span> C2DM_SEND_URI <span style="color: #339933;">=</span> <span style="color: #0000ff;">'https://android.apis.google.com/c2dm/send'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** 
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">const</span> AUTH_SERVICE_NAME <span style="color: #339933;">=</span> <span style="color: #0000ff;">'ac2dm'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** 
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_defaultPostUri</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">self</span><span style="color: #339933;">::</span><span style="color: #004000;">C2DM_SEND_URI</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** 
     * @var Zend_Http_Client
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_client</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** 
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_loginToken</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/** 
     * @var array
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_options</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Service_Google_C2dm_Message
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_lastMessage</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Http_Response
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_lastResponse</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Constructor
     *
     * @param array|Zend_Config $options
     * @return Zend_Service_Google_C2dm
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setOptions</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Options for C2DM
     *
     * @return array
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getOptions<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_options<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set Options for C2DM
     *
     * @param array|Zend_Config $options
     * @return Zend_Service_Google_C2dm
     * @throws Zend_Service_Google_C2dm_Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setOptions<span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> instanceof Zend_Config<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$options</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$options</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">toArray</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setOptions() expects either an array or a Zend_Config object'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$options</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$method</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'set'</span> <span style="color: #339933;">.</span> <span style="color: #990000;">ucfirst</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">method_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #000088;">$method</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$method</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_options<span style="color: #009900;">&#91;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Login Token
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLoginToken<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_loginToken<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #009933; font-style: italic;">/**
     * Set Login Token
     *
     * @param string $token
     * @return Zend_Service_Google_C2dm
     * @throws Zend_Service_Google_C2dm_Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setLoginToken<span style="color: #009900;">&#40;</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setLoginToken() expects a string'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_loginToken <span style="color: #339933;">=</span> <span style="color: #000088;">$token</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Http Client
     *
     * @return Zend_Http_Client
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getHttpClient<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client instanceof Zend_Http_Client<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Http_Client<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setConfig</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                <span style="color: #0000ff;">'strictredirects'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set Http Client
     *
     * @return Zend_Service_Google_C2dm
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setHttpClient<span style="color: #009900;">&#40;</span>Zend_Http_Client <span style="color: #000088;">$client</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Prepare an HTTP Request for C2DM
     *
     * @return void
     * @throws Zend_Service_Google_C2dm_Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _prepareHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$token</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Sending a message requires a Google Authorization Token'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setUri</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_defaultPostUri<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setHeaders</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Authorization'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'GoogleLogin auth='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$token</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_key_exists</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'delay_while_idle'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_options<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_options<span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'delay_while_idle'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterPost</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'delay_while_idle'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_client <span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Send a Message
     *
     * @param Zend_Service_Google_C2dm_Message $message
     * @return boolean
     * @throws Zend_Service_Google_C2dm_Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> sendMessage<span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Message <span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastMessage <span style="color: #339933;">=</span> <span style="color: #000088;">$message</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'sendMessage was unable to validate the message'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_prepareHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterPost</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'registration_id'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRegistrationId</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterPost</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'collapse_key'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCollapseKey</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$k</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setParameterPost</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'data.'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$k</span><span style="color: #339933;">,</span> <span style="color: #000088;">$v</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastResponse <span style="color: #339933;">=</span> <span style="color: #000088;">$response</span><span style="color: #339933;">=</span> <span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">request</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'POST'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// check the response for errors:</span>
        <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getStatus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">503</span><span style="color: #339933;">:</span>
                <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception_ServerUnavailable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The server was unavailable, check Retry-After and try again'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">case</span> <span style="color: #cc66cc;">401</span><span style="color: #339933;">:</span>
                <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception_InvalidAuthToken<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The Auth token: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLoginToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">' was invalid'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
                <span style="color: #666666; font-style: italic;">// check response body for any errors.</span>
                <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$body</span> <span style="color: #339933;">=</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/=/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$body</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">||</span> <span style="color: #339933;">!</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #666666; font-style: italic;">// bad response from google</span>
                    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception_ServerUnavailable<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The server gave us an invalid response, we need to try again.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$body</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000ff;">'error'</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000088;">$exception</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Zend_Service_Google_C2dm_Exception_<span style="color: #006699; font-weight: bold;">{$body[1]}</span>&quot;</span><span style="color: #339933;">;</span>
                    <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000088;">$exception</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Gets the Last Sent Message
     *
     * @return Zend_Service_Google_C2dm_Message
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLastMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastMessage<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Gets the Last Response
     *
     * @return Zend_Http_Response
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getLastResponse<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_lastResponse<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Message.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * C2dm Message
 * Implements an individual message to a client
 *
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Message
<span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_registrationId</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_collapseKey</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Constructor
     *
     * @param string $registrationId
     * @param string $collapseKey
     * @param array  $data
     * @return Zend_Service_Google_C2dm_Message
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$registrationId</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #000088;">$collapseKey</span><span style="color: #339933;">=</span><span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span> <span style="color: #000088;">$data</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRegistrationId</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$registrationId</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setCollapseKey</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$collapseKey</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Registration ID
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getRegistrationId<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_registrationId<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set Registration ID
     *
     * @param string $registrationId
     * @return Zend_Service_Google_C2dm_Message
     * @throws Zend_Service_Google_C2dm_Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setRegistrationId<span style="color: #009900;">&#40;</span><span style="color: #000088;">$registrationId</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$registrationId</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setRegistrationId() requires a string for registrationId'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_registrationId <span style="color: #339933;">=</span> <span style="color: #000088;">$registrationId</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Collapse Key
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCollapseKey<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_collapseKey<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Collapse Key
     *
     * @return string
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getCollapseKey<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_collapseKey<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set Collapse Key
     *
     * @param string $collapseKey
     * @return Zend_Service_Google_C2dm_Message
     * @throws Zend_Service_Google_C2dm_Exception
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setCollapseKey<span style="color: #009900;">&#40;</span><span style="color: #000088;">$collapseKey</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$collapseKey</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Exception<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'setCollapseKey() requires a string for collapseKey'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_collapseKey <span style="color: #339933;">=</span> <span style="color: #000088;">$collapseKey</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Get Key Value Data Pairs
     *
     * @return array
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> getData<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Set a Data Array
     *
     * @param array $data
     * @return Zend_Service_Google_C2dm_Message
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> setData<span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span> <span style="color: #000088;">$data</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data <span style="color: #339933;">=</span> <span style="color: #000088;">$data</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Validate the Message
     *
     * @return bool
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> validate<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_registrationId<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_collapseKey<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">elseif</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_data<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/DeviceQuotaExceeded.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_DeviceQuotaExceeded <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/InvalidRegistration.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_InvalidRegistration <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/QuotaExceeded.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_QuotaExceeded <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/InvalidAuthToken.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_InvalidAuthToken <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/MessageTooBig.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_MessageTooBig <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/NotRegistered.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_NotRegistered <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>library/Zend/Service/Google/C2dm/Exception/ServerUnavailable.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * @category   Zend
 * @package    Zend_Service
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> Zend_Service_Google_C2dm_Exception_ServerUnavailable <span style="color: #000000; font-weight: bold;">extends</span> Zend_Service_Exception
<span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<h3>The Worker</h3>
<p>Since I am a bit proponent of Gearman &#8211; the worker is written in my custom <a href="/2010/10/17/integrating-gearman-into-zend-framework/">Gearman workers for Zend Framework</a>.  You will be able to see how all of these classes are utilized within this worker.</p>
<p><strong>application/workers/C2dmWorker.php</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * C2DM Worker
 * Sends messages to Android devices through Cloud To Device Messaging
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> C2dmWorker <span style="color: #000000; font-weight: bold;">extends</span> Gearman_Worker
<span style="color: #009900;">&#123;</span>
    <span style="color: #009933; font-style: italic;">/**
     * @var string
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_registerFunction</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'c2dm'</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * @var Zend_Service_Google_C2dm
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000088;">$_c2dm</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Exponential Backoff / Retry-After
     *
     * @param int $fails
     * @param Zend_Http_Response
     * @return int
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> getBackOffTime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$fails</span><span style="color: #339933;">,</span> Zend_Http_Response <span style="color: #000088;">$response</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$retry</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$response</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getHeader</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Retry-After'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">is_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$retry</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$retry</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$retry</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span> <span style="color: #000088;">$retry</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">pow</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$fails</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
      * Initialize the Worker
      *
      * @return void
      */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// attempt to login to google (should store this somewhere like sqlite for multiple workers</span>
        try <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$client</span> <span style="color: #339933;">=</span> Zend_GData_ClientLogin<span style="color: #339933;">::</span><span style="color: #004000;">getHttpClient</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'YOUR_GOOGLE_ACCT_EMAIL'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'YOUR_GOOGLE_ACCT_PSWD'</span><span style="color: #339933;">,</span> Zend_Service_Google_C2dm<span style="color: #339933;">::</span><span style="color: #004000;">AUTH_SERVICE_NAME</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'YOUR_GOOGLE_APP'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Gdata_App_CaptchaRequiredException <span style="color: #000088;">$cre</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// manual login is required</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'URL of CAPTCHA image: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCaptchaUrl</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Token ID: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$cre</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getCaptchaToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
            <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Gdata_App_AuthException <span style="color: #000088;">$ae</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Problem authenticating: '</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$ae</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">exception</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> PHP_EOL<span style="color: #339933;">;</span>
            <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_c2dm <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_c2dm<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLoginToken</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$client</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getClientLoginToken</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #009933; font-style: italic;">/**
     * Work
     *
     * @return void
     */</span>
    <span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000000; font-weight: bold;">function</span> _work<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$workload</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getWorkload</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">// assume workload is json with registrationId, collapseKey and a data array</span>
        <span style="color: #000088;">$data</span> <span style="color: #339933;">=</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$workload</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$message</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Service_Google_C2dm_Message<span style="color: #009900;">&#40;</span>
            <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">registrationId</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">collapseKey</span><span style="color: #339933;">,</span>
            <span style="color: #000088;">$data</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">data</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$failCount</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">do</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$tryAgain</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>  
            try <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_c2dm<span style="color: #339933;">-&gt;</span><span style="color: #004000;">sendMessage</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$message</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_QuotaExceeded <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$failCount</span><span style="color: #339933;">++;</span>
                <span style="color: #000088;">$tryAgain</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">usleep</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getBackOffTime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$failCount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c2dm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLastResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_ServerUnavailable <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$failCount</span><span style="color: #339933;">++;</span>
                <span style="color: #000088;">$tryAgain</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">usleep</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>_getBackOffTime<span style="color: #009900;">&#40;</span><span style="color: #000088;">$failCount</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c2dm</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getLastResponse</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">1000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_InvalidRegistration <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// do not attempt to send a message to this id again</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_NotRegistered <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// do not attempt to send a message to this id again</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_DeviceQuotaExceeded <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// you may attempt to retry, however, it may be best to let it go away</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_MessageTooBig <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">// you may want to log this one and find the offending code and reduce the message size</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception_MissingCollapseKey <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
               <span style="color: #666666; font-style: italic;">// you may want to log this one and find the offending code to ensure that this is pushed in</span>
            <span style="color: #009900;">&#125;</span> catch <span style="color: #009900;">&#40;</span>Zend_Service_Google_C2dm_Exception <span style="color: #000088;">$e</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
              <span style="color: #666666; font-style: italic;">// all of the rest of the exceptions are fatal in nature, log the exception and kill the client</span>
              <span style="color: #666666; font-style: italic;">// sometimes we simply just need to generate a new id.</span>
              <span style="color: #990000;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$tryAgain</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<h2>Receiving C2DM Messages on Android and Displaying a Notification</h2>
<p>The following is going to be the code that we need to implement in order for Receiving C2DM messages as well as displaying them as a notification in the notification manager.</p>
<p><strong>AndroidManifest.xml</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;manifest</span> <span style="color: #000066;">xmlns:android</span>=<span style="color: #ff0000;">&quot;http://schemas.android.com/apk/res/android&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">package</span>=<span style="color: #ff0000;">&quot;com.dummy.app&quot;</span> <span style="color: #000066;">android:versionCode</span>=<span style="color: #ff0000;">&quot;1&quot;</span></span>
<span style="color: #009900;">    <span style="color: #000066;">android:versionName</span>=<span style="color: #ff0000;">&quot;1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;application</span> <span style="color: #000066;">android:icon</span>=<span style="color: #ff0000;">&quot;@drawable/icon&quot;</span> <span style="color: #000066;">android:label</span>=<span style="color: #ff0000;">&quot;@string/app_name&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;service</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;.C2DMReceiver&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;receiver</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dummy.app.C2DMReceiver&quot;</span></span>
<span style="color: #009900;">            <span style="color: #000066;">android:permission</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.permission.SEND&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent</span> -filter<span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dummy.app&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;intent</span> -filter<span style="color: #000000; font-weight: bold;">&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;action</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
                <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;category</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dummy.app&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
            <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/intent<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/receiver<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/application<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;permission</span> <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dummy.app.permission.C2D_MESSAGE&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">android:protectionLevel</span>=<span style="color: #ff0000;">&quot;signature&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses</span> -permission <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.dummy.app.permission.C2D_MESSAGE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;uses</span> -permission <span style="color: #000066;">android:name</span>=<span style="color: #ff0000;">&quot;com.google.android.c2dm.permission.RECEIVE&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/manifest<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

</p>
<p><strong>src/com/dummy/app/C2DMReceiver.java</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.dummy.app</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.Notification</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.NotificationManager</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.app.PendingIntent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.BroadcastReceiver</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Context</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.content.Intent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.os.Bundle</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">android.util.Log</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dummy.app.myIntent</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">com.dummy.app.R</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> C2DMReceiver <span style="color: #000000; font-weight: bold;">extends</span> BroadcastReceiver <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onReceive<span style="color: #009900;">&#40;</span><span style="color: #003399;">Context</span> context, Intent intent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTRATION&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #003399;">String</span> registration <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;registration_id&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;error&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// handle an error for registration</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getStringExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;unregistered&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// call service to handle unregistration</span>
            <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>registration <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// call service to handle registration</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">else</span> <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>intent.<span style="color: #006633;">getAction</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">equals</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.RECEIVE&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #666666; font-style: italic;">// we are just going to say a few fields already exist here, but this is based</span>
            <span style="color: #666666; font-style: italic;">// on what you may be sending from above in your data fields</span>
            <span style="color: #003399;">String</span> title <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #003399;">String</span> message <span style="color: #339933;">=</span> intent.<span style="color: #006633;">getExtras</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;msg&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            Intent myIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span>context, myIntent.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// replace myIntent.class with your intent to launch</span>
            PrendingIntent contentIntent <span style="color: #339933;">=</span> PendingIntent.<span style="color: #006633;">getActivity</span><span style="color: #009900;">&#40;</span>context, <span style="color: #cc66cc;">0</span>, myIntent, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// create the notification!</span>
            <span style="color: #000066; font-weight: bold;">int</span> icon <span style="color: #339933;">=</span> R.<span style="color: #006633;">drawable</span>.<span style="color: #006633;">icon</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">long</span> when <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">currentTimeMillis</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            CharSequence contentTitle, contentMessage <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #339933;">;</span>
            contentTitle <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>CharSequence<span style="color: #009900;">&#41;</span> title<span style="color: #339933;">;</span>
&nbsp;
            contentMessage <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>CharSequence<span style="color: #009900;">&#41;</span> message<span style="color: #339933;">;</span>
&nbsp;
            NotificationManager nm <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>NotificationManager<span style="color: #009900;">&#41;</span> context.<span style="color: #006633;">getSystemService</span><span style="color: #009900;">&#40;</span>context.<span style="color: #006633;">NOTIFICATION_SERVICE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            Notification n <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Notification<span style="color: #009900;">&#40;</span>icon, contentTitle, when<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            n.<span style="color: #006633;">setLatestEventInfo</span><span style="color: #009900;">&#40;</span>context, contentTitle, contentMessage, contentIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            nm.<span style="color: #006633;">notify</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 0 can be replaced with a number for separate style of messages</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

</p>
<p><strong>Registration with C2DM</strong></p>
<p>
<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">Intent registrationIntent <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;com.google.android.c2dm.intent.REGISTER&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
registrationIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;app&quot;</span>, PendingIntent.<span style="color: #006633;">getBroadcast</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">this</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #000000; font-weight: bold;">new</span> Intent<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// boilerplate</span>
registrationIntent.<span style="color: #006633;">putExtra</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;sender&quot;</span>, emailOfSender<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
startService<span style="color: #009900;">&#40;</span>registrationIntent<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

</p>
<h2>Conclusion</h2>
<p>This is an easy way to send notifications to an android device.  I realize that this is a lot of code and not a lot of detail in text, please ask any questions in the comments and I will get back to them.  There is also a wide range of items that you can do on the device through c2dm, this is not just limited to showing a notification or synchronizing data.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.digitalstruct.com/2010/11/21/android-c2dm-with-php-and-zend-framework/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc (Session started)
Database Caching using apc
Object Caching 232/240 objects using apc

 Served from: blog.digitalstruct.com @ 2013-05-18 20:25:01 by W3 Total Cache -->