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.
Nota
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.
$mpns = new Zend_Mobile_Push_Mpns();
$messages = array();
// raw notification
$message = new Zend_Mobile_Push_Message_Mpns_Raw();
$message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN');
$message->setMessage('<notification><foo id="bar" /></notification>');
$messages[] = $message;
// toast message
$message = new Zend_Mobile_Push_Message_Mpns_Toast();
$message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN');
$message->setTitle('Foo');
$message->setMessage('Bar');
$messages[] = $message;
// tile message
$message = new Zend_Mobile_Push_Mpns_Tile();
$message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN');
$message->setBackgroundImage('foo.bar');
$message->setCount(1);
$message->setTitle('Bar Foo');
$messages[] = $message;
foreach ($messages as $m) {
try {
$mpns->send($m);
} catch (Zend_Mobile_Push_Exception_InvalidToken $e) {
echo 'Remove token: ' . $m->getToken() . PHP_EOL;
} catch (Zend_Mobile_Push_Exception $e) {
echo 'Error occurred, token: ' . $m->getToken() . ' - ' . $e->getMessage() . PHP_EOL;
}
}
Tabla 121. 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. |
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 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.
$message = new Zend_Mobile_Push_Message_Mpns_Tile();
$message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN'); // REPLACE WITH NOTIFICATION URI FROM MPNS
$message->setBackgroundImage('foo.jpg');
$message->setCount(1);
$message->setTitle('Bar');
// other optional attributes for wp7.1+
$message->setTileId('/Foo.xaml');
$message->setBackBackgroundImage('blue.jpg');
$message->setBackTitle('Bar');
$message->setBackContent('Foo Bar');
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.
$message = new Zend_Mobile_Push_Message_Mpns_Toast();
$message->setToken('http://sn1.notify.live.net/throttledthirdparty/01.00/THETOKEN'); // REPLACE WITH NOTIFICATION URI FROM MPNS
$message->setTitle('Foo');
$message->setMessage('Bar');
// optional attributes for wp7.1+
$message->setParams('?bar=foo'); //optional parameters