Zend_Mobile_Push Introduction

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. span style="color: #ff0000;">'Zend Mobile Push Example');
  2. $message->setBadge(1);
  3. $message->setSound('default''ABCDEF0123456789''/path/to/provisioning-certificate.pem');
  4. // if you have a passphrase on your certificate:
  5. // $apns->setCertificatePassphrase('foobar');
  6. // you can either attempt to reconnect here or try again later
  7. 'APNS Connection Error:'// you would likely want to remove the token from being sent to again
  8. // all other exceptions only require action to be sent
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. span style="color: #ff0000;">'/path/to/provisioning-certificate.pem'// you can either attempt to reconnect here or try again later
  2. 'APNS Connection Error:'"\t"

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. span style="color: #808080; font-style: italic;">// message with different button
  2.     $message->setAlert('Bob wants to play poker', 'PLAY');
  3.     // message using apps localized strings w/ string replacements
  4. 'GAME_PLAY_REQUEST_FORMAT''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. span style="color: #ff0000;">'foo', 'bar');
  2.                 $message->addCustomData('foo''bar' => 1));
  3.                 $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.


Zend_Mobile_Push Introduction