Zend_Mobile_Push-Gcm.xml 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.mobile.push.gcm">
  4. <title>Zend_Mobile_Push_Gcm</title>
  5. <para>
  6. <classname>Zend_Mobile_Push_Gcm</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_Gcm</classname>.
  10. </para>
  11. <sect2 id="zend.mobile.push.gcm.server">
  12. <title>Pushing Messages</title>
  13. <note>
  14. <para>Prior to pushing and receiving messages; you will need to create a Google
  15. API Project and setup your Android app to listen to GCM
  16. messages.. If you have not done this, follow the <ulink
  17. url="http://developer.android.com/guide/google/gcm/gs.html">
  18. GCM: Getting Started</ulink> document.
  19. </para>
  20. </note>
  21. <para>
  22. When implementing GCM; you have a few components that
  23. you will utilize. <classname>Zend_Mobile_Push_Gcm</classname>
  24. which contains the server components,
  25. <classname>Zend_Mobile_Push_Message_Gcm</classname> which contains
  26. the message that you would like to send, and
  27. <classname>Zend_Mobile_Push_Response_Gcm</classname> which contains
  28. the response from GCM. Each message sent must do an HTTP request; so
  29. remember this when sending in large batches.
  30. </para>
  31. <para>
  32. The actual implementation of the code is fairly minimal; however,
  33. considerations to error handling must be taken.
  34. </para>
  35. <programlisting language="php"><![CDATA[
  36. $message = new Zend_Mobile_Push_Message_Gcm();
  37. $message->addToken('ABCDEF0123456789');
  38. $message->setData(array(
  39. 'foo' => 'bar',
  40. 'bar' => 'foo',
  41. ));
  42. $gcm = new Zend_Mobile_Push_Gcm();
  43. $gcm->setApiKey('YOUR_API_KEY');
  44. try {
  45. $response = $gcm->send($message);
  46. } catch (Zend_Mobile_Push_Exception $e) {
  47. // exceptions require action or implementation of exponential backoff.
  48. die($e->getMessage());
  49. }
  50. // handle all errors and registration_id's
  51. foreach ($response->getResults() as $k => $v) {
  52. if ($v['registration_id']) {
  53. printf("%s has a new registration id of: %s\r\n", $k, $v['registration_id']);
  54. }
  55. if ($v['error']) {
  56. printf("%s had an error of: %s\r\n", $k, $v['error']);
  57. }
  58. if ($v['message_id']) {
  59. printf("%s was successfully sent the message, message id is: %s", $k, $v['message_id']);
  60. }
  61. }
  62. ]]></programlisting>
  63. <table id="zend.mobile.push.gcm.server.exceptions">
  64. <title>Exceptions and Remediation Techniques</title>
  65. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  66. <thead>
  67. <row>
  68. <entry>Exception</entry>
  69. <entry>Meaning</entry>
  70. <entry>Handling</entry>
  71. </row>
  72. </thead>
  73. <tbody>
  74. <row>
  75. <entry>Zend_Mobile_Push_Exception</entry>
  76. <entry>These types of exceptions are more generic in nature
  77. and are thrown either from gcm when there was an unknown exception
  78. or internally on input validation.</entry>
  79. <entry>Read the message and determine remediation steps.</entry>
  80. </row>
  81. <row>
  82. <entry>Zend_Mobile_Push_Exception_InvalidAuthToken</entry>
  83. <entry>Your API key was likely typed in wrong or does
  84. not have rights to the GCM service.</entry>
  85. <entry>Check your project on the <ulink
  86. url="https://code.google.com/apis/console">Google APIs Console
  87. page</ulink>.</entry>
  88. </row>
  89. <row>
  90. <entry>Zend_Mobile_Push_Exception_ServerUnavailable</entry>
  91. <entry>This exception means there was either an internal
  92. server error OR that the server denied your request and
  93. you should look at the Retry-After header.</entry>
  94. <entry>Read the exception message and utilize
  95. Exponential Backoff</entry>
  96. </row>
  97. <row>
  98. <entry>Zend_Mobile_Push_Exception_InvalidPayload</entry>
  99. <entry>Generally the payload will not throw an exception
  100. unless the size of the payload is too large or the JSON
  101. is too large.</entry>
  102. <entry>Check the size of the payload is within the
  103. requirements of GCM, currently it is 4K.</entry>
  104. </row>
  105. </tbody>
  106. </tgroup>
  107. </table>
  108. </sect2>
  109. <sect2 id="zend.mobile.push.gcm.message">
  110. <title>Advanced Messages</title>
  111. <para>
  112. GCM provides the ability for sending more advanced messages; for
  113. instance the examples above show the most basic implementation of a
  114. message. <classname>Zend_Mobile_Push_Message_Gcm</classname>
  115. allows you to do far more advanced messaging outlined below.
  116. </para>
  117. <sect3 id="zend.mobile.push.gcm.message.delay-while-idle">
  118. <title>Delay While Idle</title>
  119. <para>
  120. If included, indicates that the message should not be sent
  121. immediately if the device is idle. The server will wait for the
  122. device to become active, and then only the last message for each
  123. collapse_key value will be sent.
  124. </para>
  125. <programlisting language="php"><![CDATA[
  126. $message = new Zend_Mobile_Push_Message_Gcm();
  127. $message->setDelayWhileIdle(true);
  128. ]]></programlisting>
  129. </sect3>
  130. <sect3 id="zend.mobile.push.gcm.message.ttl">
  131. <title>Time to Live</title>
  132. <para>
  133. You may set the time to live in seconds, by default GCM will
  134. save it for 4 weeks. Additionally if you specify a Time to
  135. Live, you must also set an ID (the collapse key). Generally it
  136. is best by using the message data so that you know it is a
  137. unique message.
  138. </para>
  139. <programlisting language="php"><![CDATA[
  140. $message = new Zend_Mobile_Push_Message_Gcm();
  141. $message->setTtl(86400);
  142. $message->addData('key', 'value');
  143. $message->setId(md5(json_encode($message->getData())));
  144. ]]></programlisting>
  145. </sect3>
  146. </sect2>
  147. <sect2 id="zend.mobile.push.gcm.response">
  148. <title>Response</title>
  149. <para>
  150. GCM gives a response back that contains detail that needs to be
  151. understood. Most of the time you can just send a message but the
  152. server may come back telling you any the message id, any errors and
  153. potentially new registration ids.
  154. </para>
  155. <sect3 id="zend.mobile.push.gcm.response.result">
  156. <title>Results</title>
  157. <para>
  158. The results are most commonly retrieved by calling the
  159. getResults() method. However, you may prefer to just get
  160. certain results by using the getResult() method. The getResult
  161. method utilizes the constants RESULT_* correlating to message
  162. id, error or registration id.
  163. </para>
  164. <para>
  165. Several utility methods exist to give you a better idea of what
  166. happened during your send. Methods getSuccessCount(),
  167. getFailureCount() and getCanonicalCount() let you know how many
  168. where successful, how many failures and how many updates to
  169. registration ids you have.
  170. </para>
  171. </sect3>
  172. </sect2>
  173. </sect1>