Zend_Mobile_Push_Apns

Zend_Mobile_Push_Apns provides the ability to send push notifications to APNS generally in conjunction with Zend_Mobile_Push_Message_Apns; however there is a case when it would not be utilized is when getting feedback from the APNS server.

Pushing Messages

Note: Prior to pushing messages; you must follow the » provisioning and deployment steps outlined by Apple.

When implementing APNS; you have a few components that you will utilize. Zend_Mobile_Push_Apns which contains the server components and Zend_Mobile_Push_Message_Apns which contains the message that you would like to send. Generally when sending push notifications to Apple you should do so in a batch.

The actual implementation of the code is fairly minimal; however, considerations to error handling must be taken.

  1. $message = new Zend_Mobile_Push_Message_Apns();
  2. $message->setAlert('Zend Mobile Push Example');
  3. $message->setBadge(1);
  4. $message->setSound('default');
  5. $message->setId(time());
  6. $message->setToken('ABCDEF0123456789');
  7.  
  8. $apns = new Zend_Mobile_Push_Apns();
  9. $apns->setCertificate('/path/to/provisioning-certificate.pem');
  10. // if you have a passphrase on your certificate:
  11. // $apns->setCertificatePassphrase('foobar');
  12.  
  13. try {
  14.     $apns->connect(Zend_Mobile_Push_Apns::SERVER_SANDBOX_URI);
  15. } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
  16.     // you can either attempt to reconnect here or try again later
  17.     exit(1);
  18. } catch (Zend_Mobile_Push_Exception $e) {
  19.     echo 'APNS Connection Error:' . $e->getMessage();
  20.     exit(1);
  21. }
  22.  
  23. try {
  24.     $apns->send($message);
  25. } catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
  26.     // you would likely want to remove the token from being sent to again
  27.     echo $e->getMessage();
  28. } catch (Zend_Mobile_Push_Exception $e) {
  29.     // all other exceptions only require action to be sent
  30.     echo $e->getMessage();
  31. }
  32. $apns->close();
Exceptions and Remediation Techniques
Exception Meaning Handling
Zend_Mobile_Push_Exception These types of exceptions are more generic in nature and are thrown either from APNS or internally on input validation Read the message and determine remediation steps.
Zend_Mobile_Push_Exception_InvalidPayload Generally the payload will not throw an exception unless the size of the payload is too large or it is missing required content. Check the size of the payload is within the requirements of APNS
Zend_Mobile_Push_Exception_InvalidToken Any form of an invalid token will be if the token is no longer registered; you are missing a token or it is in an invalid format. You should remove the token and not attempt to send to it again.
Zend_Mobile_Push_Exception_InvalidTopic An invalid topic simply means that the message id was too long or not an integer. Ensure that the message ID is an integer.
Warning

When sending in batches and you are sending a large amount of push notifications out; you should ensure to usleep from time to time. This will ensure that your messages will be delivered and APNS will not simply hang up on you.

Getting Feedback

APNS has a feedback service that you must listen to. Apple states that they monitor providers to ensure that they are listening to this service.

The feedback service simply returns an array of device tokens and the time. You can use the time to ensure that the device has not re-registered for push notifications since the last send.

  1. $apns = new Zend_Mobile_Push_Apns();
  2. $apns->setCertificate('/path/to/provisioning-certificate.pem');
  3.  
  4. try {
  5.     $apns->connect(Zend_Mobile_Push_Apns::SERVER_FEEDBACK_SANDBOX_URI);
  6. } catch (Zend_Mobile_Push_Exception_ServerUnavailable $e) {
  7.     // you can either attempt to reconnect here or try again later
  8.     exit(1);
  9. } catch (Zend_Mobile_Push_Exception $e) {
  10.     echo 'APNS Connection Error:' . $e->getMessage();
  11.     exit(1);
  12. }
  13.  
  14. $tokens = $apns->feedback();
  15. while(list($token, $time) = each($tokens)) {
  16.     echo $time . "\t" . $token . PHP_EOL;
  17. }
  18. $apns->close();

Advanced Messages

APNS provides the ability for sending more advanced messages; for instance the examples above show the most basic implementation of a message. Zend_Mobile_Push_Message_Apns allows you to do far more advanced messaging outlined below.

Alerts

Alerts can contain anything from a simple body message to having an action key and a launch image (iOS 4). You may only want to provide an action key when only a confirmation is necessary OR you are looking to localize the button with non-standard text (aka not "View").

The following code example shows alerts from the » APNS payload examples.

  1. $message = new Zend_Mobile_Push_Message_Apns();
  2.  
  3.     // message with different button
  4.     $message->setAlert('Bob wants to play poker', 'PLAY');
  5.     // message using apps localized strings w/ string replacements
  6.     $message->setAlert(null, null, 'GAME_PLAY_REQUEST_FORMAT', array('Jenna', 'Frank'));

Custom Data

You can send your app custom data which allows you to make decisions based on the notifications; such as synchronizing data.

  1. $message = new Zend_Mobile_Push_Message_Apns();
  2.                 $message->addCustomData('foo', 'bar');
  3.                 $message->addCustomData('foo', array('bar' => 1));
  4.                 $message->addCustomData('bar', 'foo');
Warning

You may not use a custom key of 'aps' as it is reserved by Apple and leveraged for the main push data.

blog comments powered by Disqus