Zend_Mobile_Push-C2dm.xml 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.mobile.push.c2dm">
  4. <title>Zend_Mobile_Push_C2dm</title>
  5. <para>
  6. <classname>Zend_Mobile_Push_C2dm</classname> provides the ability to
  7. send push notifications to Android devices that contain Google Services.
  8. A message will always be constructed with
  9. <classname>Zend_Mobile_Push_Message_C2dm</classname>.
  10. </para>
  11. <sect2 id="zend.mobile.push.c2dm.server">
  12. <title>Pushing Messages</title>
  13. <note>
  14. <para>Prior to pushing messages; you must
  15. <ulink
  16. url="http://code.google.com/android/c2dm/signup.html"> sign
  17. up for a c2dm account</ulink>. In order to get your application
  18. to recieve push notifications, you should follow: <ulink
  19. url="http://code.google.com/android/c2dm/index.html#writing_apps">Writing
  20. Android Applications that Use C2DM</ulink>.</para>
  21. </note>
  22. <para>
  23. When implementing C2DM; you have a few components that
  24. you will utilize. <classname>Zend_Mobile_Push_C2dm</classname>
  25. which contains the server components and
  26. <classname>Zend_Mobile_Push_Message_C2dm</classname> which contains
  27. the message that you would like to send. Each message sent must do
  28. an HTTP request; so remember this when sending in large batches.
  29. </para>
  30. <para>
  31. The actual implementation of the code is fairly minimal; however,
  32. considerations to error handling must be taken.
  33. </para>
  34. <programlisting language="php"><![CDATA[
  35. try {
  36. $client = Zend_Gdata_ClientLogin::getHttpClient(
  37. 'my@gmail.com', // REPLACE WITH YOUR GOOGLE ACCOUNT
  38. 'myPassword', // REPLACE WITH YOUR PASSWORD
  39. Zend_Mobile_Push_C2dm::AUTH_SERVICE_NAME,
  40. null,
  41. 'myAppName' // REPLACE WITH YOUR APP NAME
  42. );
  43. } catch (Zend_Gdata_App_CaptchaRequiredException $cre) {
  44. // manual login is required
  45. echo 'URL of CAPTCHA image: ' . $cre->getCaptchaUrl() . PHP_EOL;
  46. echo 'Token ID: ' . $cre->getCaptchaToken() . PHP_EOL;
  47. exit(1);
  48. } catch (Zend_Gdata_App_AuthException $ae) {
  49. echo 'Problem authenticating: ' . $ae->exception() . PHP_EOL;
  50. exit(1);
  51. }
  52. $message = new Zend_Mobile_Push_Message_C2dm();
  53. $message->setId(time());
  54. $message->setToken('ABCDEF0123456789');
  55. $message->setData(array(
  56. 'foo' => 'bar',
  57. 'bar' => 'foo',
  58. ));
  59. $c2dm = new Zend_Mobile_Push_C2dm();
  60. $c2dm->setLoginToken($client->getClientLoginToken());
  61. try {
  62. $c2dm->send($message);
  63. } catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
  64. // you would likely want to remove the token from being sent to again
  65. echo $e->getMessage();
  66. } catch (Zend_Mobile_Push_Exception $e) {
  67. // all other exceptions only require action to be sent or implementation of exponential backoff.
  68. echo $e->getMessage();
  69. }
  70. ]]></programlisting>
  71. <table id="zend.mobile.push.c2dm.server.exceptions">
  72. <title>Exceptions and Remediation Techniques</title>
  73. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  74. <thead>
  75. <row>
  76. <entry>Exception</entry>
  77. <entry>Meaning</entry>
  78. <entry>Handling</entry>
  79. </row>
  80. </thead>
  81. <tbody>
  82. <row>
  83. <entry>Zend_Mobile_Push_Exception</entry>
  84. <entry>These types of exceptions are more generic in nature
  85. and are thrown either from C2DM when there was an unknown exception
  86. or internally on input validation.</entry>
  87. <entry>Read the message and determine remediation steps.</entry>
  88. </row>
  89. <row>
  90. <entry>Zend_Mobile_Push_Exception_InvalidPayload</entry>
  91. <entry>Generally the payload will not throw an exception
  92. unless the size of the payload is too large or it is missing
  93. required content.</entry>
  94. <entry>Check the size of the payload is within the
  95. requirements of C2DM</entry>
  96. </row>
  97. <row>
  98. <entry>Zend_Mobile_Push_Exception_InvalidToken</entry>
  99. <entry>Any form of an invalid token will be if the token is
  100. no longer registered; you are missing a token or it is in an
  101. invalid format.</entry>
  102. <entry>You should remove the token and not attempt to send
  103. to it again.</entry>
  104. </row>
  105. <row>
  106. <entry>Zend_Mobile_Push_Exception_InvalidTopic</entry>
  107. <entry>An invalid topic simply means that the message id was
  108. too long or not an integer.</entry>
  109. <entry>Ensure that the message ID is an integer.</entry>
  110. </row>
  111. <row>
  112. <entry>Zend_Mobile_Push_Exception_DeviceQuotaExceeded</entry>
  113. <entry>You have sent too many messages to this device;
  114. you may retry again later.</entry>
  115. <entry>Grab the HTTP client and check to see if there
  116. was a retry-after header; otherwise implement
  117. <ulink
  118. url="http://en.wikipedia.org/wiki/Exponential_backoff">Exponential
  119. Backoff</ulink></entry>
  120. </row>
  121. <row>
  122. <entry>Zend_Mobile_Push_Exception_QuotaExceeded</entry>
  123. <entry>You have sent too many messages and have gone
  124. over the quota for the day; you may try again later.</entry>
  125. <entry>Grab the HTTP client and check to see if there
  126. was a retry-after header; like above implement
  127. Exponential Backoff. Secondly; if you are
  128. continually going over your limit; you may request
  129. <ulink
  130. url="http://code.google.com/android/c2dm/quotas.html">Request
  131. a higher quota</ulink>. See the link on the bottom of
  132. the page.</entry>
  133. </row>
  134. </tbody>
  135. </tgroup>
  136. </table>
  137. </sect2>
  138. <sect2 id="zend.mobile.push.c2dm.message">
  139. <title>Advanced Messages</title>
  140. <para>
  141. C2DM provides the ability for sending more advanced messages; for
  142. instance the examples above show the most basic implementation of a
  143. message. <classname>Zend_Mobile_Push_Message_C2dm</classname>
  144. allows you to do far more advanced messaging outlined below.
  145. </para>
  146. <sect3 id="zend.mobile.push.c2dm.message.delay-while-idle">
  147. <title>Delay While Idle</title>
  148. <para>
  149. If included, indicates that the message should not be sent
  150. immediately if the device is idle. The server will wait for the
  151. device to become active, and then only the last message for each
  152. collapse_key value will be sent.
  153. </para>
  154. <programlisting language="php"><![CDATA[
  155. $message = new Zend_Mobile_Push_Message_C2dm();
  156. $message->setDelayWhileIdle(true);
  157. ]]></programlisting>
  158. </sect3>
  159. </sect2>
  160. </sect1>