Zend_Mobile_Push-Apns.xml 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.mobile.push.apns">
  4. <title>Zend_Mobile_Push_Apns</title>
  5. <para>
  6. <classname>Zend_Mobile_Push_Apns</classname> provides the ability to
  7. send push notifications to <acronym>APNS</acronym> generally in
  8. conjunction with <classname>Zend_Mobile_Push_Message_Apns</classname>;
  9. however there is a case when it would not be utilized is when getting
  10. feedback from the APNS server.
  11. </para>
  12. <sect2 id="zend.mobile.push.apns.server">
  13. <title>Pushing Messages</title>
  14. <note>
  15. <para>Prior to pushing messages; you must follow the
  16. <ulink
  17. url="http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ProvisioningDevelopment/ProvisioningDevelopment.html">provisioning and deployment steps outlined by Apple</ulink>.</para>
  18. </note>
  19. <para>
  20. When implementing APNS; you have a few components that
  21. you will utilize. <classname>Zend_Mobile_Push_Apns</classname>
  22. which contains the server components and
  23. <classname>Zend_Mobile_Push_Message_Apns</classname> which contains
  24. the message that you would like to send. Generally when sending
  25. push notifications to Apple you should do so in a batch.
  26. </para>
  27. <para>
  28. The actual implementation of the code is fairly minimal; however,
  29. considerations to error handling must be taken.
  30. </para>
  31. <programlisting language="php"><![CDATA[
  32. $message = new Zend_Mobile_Push_Message_Apns();
  33. $message->setAlert('Zend Mobile Push Example');
  34. $message->setBadge(1);
  35. $message->setSound('default');
  36. $message->setId(time());
  37. $message->setToken('ABCDEF0123456789');
  38. $apns = new Zend_Mobile_Push_Apns();
  39. $apns->setCertificate('/path/to/provisioning-certificate.pem');
  40. // if you have a passphrase on your certificate:
  41. // $apns->setCertificatePassphrase('foobar');
  42. try {
  43. $apns->connect(Zend_Mobile_Push_Apns::SERVER_SANDBOX_URI);
  44. } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
  45. // you can either attempt to reconnect here or try again later
  46. exit(1);
  47. } catch (Zend_Mobile_Push_Exception $e) {
  48. echo 'APNS Connection Error:' . $e->getMessage();
  49. exit(1);
  50. }
  51. try {
  52. $apns->send($message);
  53. } catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
  54. // you would likely want to remove the token from being sent to again
  55. echo $e->getMessage();
  56. } catch (Zend_Mobile_Push_Exception $e) {
  57. // all other exceptions only require action to be sent
  58. echo $e->getMessage();
  59. }
  60. $apns->close();
  61. ]]></programlisting>
  62. <table id="zend.mobile.push.apns.server.exceptions">
  63. <title>Exceptions and Remediation Techniques</title>
  64. <tgroup cols="3" align="left" colsep="1" rowsep="1">
  65. <thead>
  66. <row>
  67. <entry>Exception</entry>
  68. <entry>Meaning</entry>
  69. <entry>Handling</entry>
  70. </row>
  71. </thead>
  72. <tbody>
  73. <row>
  74. <entry>Zend_Mobile_Push_Exception</entry>
  75. <entry>These types of exceptions are more generic in nature
  76. and are thrown either from APNS or internally on input
  77. validation</entry>
  78. <entry>Read the message and determine remediation steps.</entry>
  79. </row>
  80. <row>
  81. <entry>Zend_Mobile_Push_Exception_InvalidPayload</entry>
  82. <entry>Generally the payload will not throw an exception
  83. unless the size of the payload is too large or it is missing
  84. required content.</entry>
  85. <entry>Check the size of the payload is within the
  86. requirements of APNS</entry>
  87. </row>
  88. <row>
  89. <entry>Zend_Mobile_Push_Exception_InvalidToken</entry>
  90. <entry>Any form of an invalid token will be if the token is
  91. no longer registered; you are missing a token or it is in an
  92. invalid format.</entry>
  93. <entry>You should remove the token and not attempt to send
  94. to it again.</entry>
  95. </row>
  96. <row>
  97. <entry>Zend_Mobile_Push_Exception_InvalidTopic</entry>
  98. <entry>An invalid topic simply means that the message id was
  99. too long or not an integer.</entry>
  100. <entry>Ensure that the message ID is an integer.</entry>
  101. </row>
  102. </tbody>
  103. </tgroup>
  104. </table>
  105. <warning>
  106. <para>
  107. When sending in batches and you are sending a large amount of push
  108. notifications out; you should ensure to usleep from time to time. This
  109. will ensure that your messages will be delivered and APNS will not simply
  110. hang up on you.
  111. </para>
  112. </warning>
  113. </sect2>
  114. <sect2 id="zend.mobile.push.apns.feedback">
  115. <title>Getting Feedback</title>
  116. <para>
  117. APNS has a feedback service that you <emphasis>must</emphasis>
  118. listen to. Apple states that they monitor providers to ensure that they
  119. are listening to this service.
  120. </para>
  121. <para>
  122. The feedback service simply returns an array of device tokens and
  123. the time. You can use the time to ensure that the device has not
  124. re-registered for push notifications since the last send.
  125. </para>
  126. <programlisting language="php"><![CDATA[
  127. $apns = new Zend_Mobile_Push_Apns();
  128. $apns->setCertificate('/path/to/provisioning-certificate.pem');
  129. try {
  130. $apns->connect(Zend_Mobile_Push_Apns::SERVER_FEEDBACK_SANDBOX_URI);
  131. } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
  132. // you can either attempt to reconnect here or try again later
  133. exit(1);
  134. } catch (Zend_Mobile_Push_Exception $e) {
  135. echo 'APNS Connection Error:' . $e->getMessage();
  136. exit(1);
  137. }
  138. $tokens = $apns->feedback();
  139. foreach ($tokens as $token => $time) {
  140. echo $time . "\t" . $token . PHP_EOL;
  141. }
  142. $apns->close();
  143. ]]></programlisting>
  144. </sect2>
  145. <sect2 id="zend.mobile.push.apns.message">
  146. <title>Advanced Messages</title>
  147. <para>
  148. APNS provides the ability for sending more advanced messages; for
  149. instance the examples above show the most basic implementation of a
  150. message. <classname>Zend_Mobile_Push_Message_Apns</classname>
  151. allows you to do far more advanced messaging outlined below.
  152. </para>
  153. <sect3 id="zend.mobile.push.apns.message.alerts">
  154. <title>Alerts</title>
  155. <para>
  156. Alerts can contain anything from a simple body message to having an
  157. action key and a launch image (iOS 4). You may only want to provide
  158. an action key when only a confirmation is necessary OR you are
  159. looking to localize the button with non-standard text (aka not
  160. "View").
  161. </para>
  162. <para>
  163. The following code example shows alerts from the <ulink
  164. url="http://developer.apple.com/library/mac/#documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/ApplePushService/ApplePushService.html">APNS
  165. payload examples</ulink>.
  166. </para>
  167. <programlisting language="php"><![CDATA[
  168. $message = new Zend_Mobile_Push_Message_Apns();
  169. // message with different button
  170. $message->setAlert('Bob wants to play poker', 'PLAY');
  171. // message using apps localized strings w/ string replacements
  172. $message->setAlert(null, null, 'GAME_PLAY_REQUEST_FORMAT', array('Jenna', 'Frank'));
  173. ]]></programlisting>
  174. </sect3>
  175. <sect3 id="zend.mobile.push.apns.message.custom-data">
  176. <title>Custom Data</title>
  177. <para>
  178. You can send your app custom data which allows you to make decisions
  179. based on the notifications; such as synchronizing data.
  180. </para>
  181. <programlisting language="php"><![CDATA[
  182. $message = new Zend_Mobile_Push_Message_Apns();
  183. $message->addCustomData('foo', 'bar');
  184. $message->addCustomData('foo', array('bar' => 1));
  185. $message->addCustomData('bar', 'foo');
  186. ]]></programlisting>
  187. <warning>
  188. <para>
  189. You may not use a custom key of 'aps' as it is reserved by Apple and
  190. leveraged for the main push data.
  191. </para>
  192. </warning>
  193. </sect3>
  194. </sect2>
  195. </sect1>