Zend_Mobile_Push_Apns

Zend_Mobile_Push_Mpns

Zend_Mobile_Push_Mpns provides the ability to send push notifications to Windows Phone. MPNS allows the sending of 3 different types of notifications; which common behavior is in the Zend_Mobile_Push_Message_Mpns base. Followed by specific classes for Raw, Toast and Tile notifications.

Pushing Messages

Note: Prior to pushing messages; you must implement the practices outlined on » Receiving Push Notifications for Windows Phone.

When implementing MPNS; you have several components that you will utilize. Zend_Mobile_Push_Mpns which contains the server components and Zend_Mobile_Push_Message_Mpns_Raw which allows you to send » raw notifications, Zend_Mobile_Push_Message_Mpns_Toast which allows you to send » toast notifications, and Zend_Mobile_Push_Message_Mpns_Tile which allows you to send » tile notifications. Each message sent must do an HTTP request; so remember this when sending in large batches.

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

  1. $mpns = new Zend_Mobile_Push_Mpns();
  2. $messages = array();
  3.  
  4. // raw notification
  5. $message = new Zend_Mobile_Push_Message_Mpns_Raw();
  6. $message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN');
  7. $message->setMessage('<notification><foo id="bar" /></notification>');
  8. $messages[] = $message;
  9.  
  10. // toast message
  11. $message = new Zend_Mobile_Push_Message_Mpns_Toast();
  12. $message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN');
  13. $message->setTitle('Foo');
  14. $message->setMessage('Bar');
  15. $messages[] = $message;
  16.  
  17. // tile message
  18. $message = new Zend_Mobile_Push_Mpns_Tile();
  19. $message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN');
  20. $message->setBackgroundImage('foo.bar');
  21. $message->setCount(1);
  22. $message->setTitle('Bar Foo');
  23. $messages[] = $message;
  24.  
  25. foreach ($messages as $m) {
  26.     try {
  27.         $mpns->send($m);
  28.     } catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
  29.         echo 'Remove token: ' . $m->getToken() . PHP_EOL;
  30.     } catch (Zend_Mobile_Push_Exception $e) {
  31.         echo 'Error occurred, token: ' . $m->getToken() . ' - ' . $e->getMessage() . PHP_EOL;
  32.     }
  33. }
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 MPNS or internally on input validation Read the message and determine remediation steps.
Zend_Mobile_Push_Exception_DeviceQuotaExceeded You have sent too many messages to this device; you may retry again later. Try again later or implement » Exponential Backoff .
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 MPNS
Zend_Mobile_Push_Exception_InvalidToken Any form of an invalid token will be if the device is no longer subscribed, inactive or not valid. In some cases you may attempt to resend in an hour; this will be stated in the exception. Otherwise you will want to remove the token from being sent to again.
Zend_Mobile_Push_Exception_QuotaExceeded You have reached the per-day throttling. Per-day throttling is only on unauthenticated web services; you will need to » register your application for notifications.

Advanced Messages

MPNS 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_Mpns_* allows you to do far more advanced messaging outlined below.

Tile Messages

Tile messages have additional optional attributes for Windows Phone 7.1+; you must ensure that you are sending to a device with the proper version otherwise your notification will fail.

  1. $message = new Zend_Mobile_Push_Message_Mpns_Tile();
  2. $message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN'); // REPLACE WITH NOTIFICATION URI FROM MPNS
  3. $message->setBackgroundImage('foo.jpg');
  4. $message->setCount(1);
  5. $message->setTitle('Bar');
  6.  
  7. // other optional attributes for wp7.1+
  8. $message->setTileId('/Foo.xaml');
  9. $message->setBackBackgroundImage('blue.jpg');
  10. $message->setBackTitle('Bar');
  11. $message->setBackContent('Foo Bar');

Toast Messages

Toast messages have additional optional attributes for Windows Phone 7.1+; you must ensure that you are sending to a device with the proper version otherwise your notification will fail.

  1. $message = new Zend_Mobile_Push_Message_Mpns_Toast();
  2. $message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN'); // REPLACE WITH NOTIFICATION URI FROM MPNS
  3. $message->setTitle('Foo');
  4. $message->setMessage('Bar');
  5.  
  6. // optional attributes for wp7.1+
  7. $message->setParams('?bar=foo'); //optional parameters

Zend_Mobile_Push_Apns