3. Push Notifications and Live Tiles
• Push Notifications offer developers a way to send timely
information relating to their applications even when they
are not running
+
• Windows phone has the unique ability to provide the end
user glanceable access to the information they care most
about, via Live Tiles
5. Three Kinds of Notifications
• Raw
• Notification message content is application-specific
• Delivered directly to app only if it is running
• Toast
• Specific XML schema
• Content delivered to app if it is running
• If app is not running, system displays Toast popup using notification message content
• Tile
• Specific XML schema
• Never delivered to app
• If the Tile is currently pinned to the Start screen, system updates it using notification message
content
5
6. Push Notification Data Flow
URI to the service:
"http://notify.live.com/throttledthirdparty/01.00/AAFRQHgiiMWNTYrRD
XAHQtz-AgrNpzcDAwAAAAQOMDAwMDAwMDAwMDAwMDA"
Push enabled
applications
Notifications
service
HTTP POST
the message
Push endpoint is established. URI is
created for the endpoint.
1
2
3
Third-party
service
MPNS:
Microsoft
hosted
server
Send PN
Message
4
7. Push Notification Features
• Tile updates can update any display element of each of the Tile templates
• Be careful to downgrade updates sent to 7.1 clients
• 30 subscription endpoints allowed per phone
• Maximum size of a notification is 1KB for the header and 3KB for the payload.
• Unauthenticated Web Service limited to 500 push notifications per subscription per day
• Authenticated Web Services are recommended
• Communication uses HTTPS channel
• No limit
• Able to register a callback request
8. Toast Message Content
•Used by system to display Toast message if app not running
•Navigates to page specified in <Param> (if supplied)
•If app is already running
• ShellToastNotificationReceived event fires
• <Text1> and <Text2> values in event args as Dictionary<string, string>
8
9. Toast Message Content
• Use the following HTTP headers:
• Where <batching interval> is 3 – Immediate delivery, 13 – delivered within 450 seconds, 23 –
delivered within 900 seconds
9
string toastMessage = "<?xml version="1.0" encoding="utf-8"?>" +
"<wp:Notification xmlns:wp="WPNotification">" +
"<wp:Toast>" +
"<wp:Text1>WEATHER ALERT</wp:Text1>" +
"<wp:Text2>Stormy</wp:Text2>" +
"<wp:Param>/page1.xaml?value1=1234<wp:Param>" +
"</wp:Toast>" +
"</wp:Notification>";
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", "toast");
sendNotificationRequest.Headers.Add("X-NotificationClass", "<batching interval>");
10. Tile Message Content
10
• Tile messages are never delivered to the app
• There are specific schemas for the a Tile update message for each of the three
Tile Templates: Flip, Cycle and Iconic
• Only Tiles using Flip and Iconic templates can be updated with images at remote URLs
• Remote Images limited to 80KB and must download in 30 seconds or less
• V7.x clients cannot consume these new templates. Recommend you:
• In your app, capture the device unique ID, the push URI, the OS version and the app
version. Send this data to your Web service registration method
• Make sure you only send the new templates to compatible new OS versions/app versions!
• Use the following HTTP headers:
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-WindowsPhone-Target", “token");
sendNotificationRequest.Headers.Add("X-NotificationClass", "<batching interval>");
11. FlipTile Template Tile Message Schema
<?xml version="1.0"?>
<wp:Notification xmlns:wp="WPNotification" Version="2.0">
<wp:Tile Id="[TileId]" Template="FlipTile">
<wp:SmallBackgroundImage Action="Clear">[sm tile img URI]</wp:SmallBackgroundImage>
<wp:WideBackgroundImage Action="Clear">[front of wd tile img URI]</wp:WideBackgroundImage>
<wp:WideBackBackgroundImage Action="Clear">[back of wd tile img URI]</wp:WideBackBackgroundImage>
<wp:WideBackContent Action="Clear">[back of wd tile content text]</wp:WideBackContent>
<wp:BackgroundImage Action="Clear">[front of med tile img URI]</wp:BackgroundImage>
<wp:Count Action="Clear">[count]</wp:Count>
<wp:Title Action="Clear">[title text]</wp:Title>
<wp:BackBackgroundImage Action="Clear">[back of med tile img URI]</wp:BackBackgroundImage>
<wp:BackTitle Action="Clear">[back of tile title text]</wp:BackTitle>
<wp:BackContent Action="Clear">[back of med tile content text]</wp:BackContent>
</wp:Tile>
</wp:Notification>
3/18/201411
14. Raw Message Content
• Use the following HTTP headers:
• Where <batching interval> is 3 – Immediate delivery, 13 – delivered within 450 seconds, 23 –
delivered within 900 seconds
• Message Content is application-specific – only gets delivered to running app
• For example use XML to format data
14
string tileMessage = "<?xml version="1.0" encoding="utf-8"?>" +
"<root>" +
"<Value1>SomeUserValue<Value1>" +
"<Value2>SomeOtherUserValue<Value2>" +
"</root>";
sendNotificationRequest.ContentType = "text/xml";
sendNotificationRequest.Headers.Add("X-NotificationClass", "<batching interval>");
16. Response Custom Headers
• Response Code: HTTP status code (200 OK)
• Notification Status
• Notification received by the Push Notification Service
• For example: “X-NotificationStatus:Received”
• DeviceConnectionStatus
• The connection status of the device
• //For example: X-DeviceConnectionStatus:Connected
• SubscriptionStatus
• The subscription status
• //For example: X-SubscriptionStatus:Active
• More information
• http://msdn.microsoft.com/en-us/library/ff402545(v=VS.92).aspx
17. Review
• Push Notifications are an efficient and battery-friendly way of sending notifications from a
server to an app on a phone
• Raw notifications go only to a running app
• Toast notifications go to the app if it is running, otherwise display as a popup on the phone
• Tile notifications are used only to update tiles on the Start Screen
Push Notifications and Live TilesA user can choose to pin a tile to the Start Screen for any application, for contacts, groups of contacts, and for web pages. Apps can create secondary tiles.These tiles consist of text elements, an optional count and images, and these elements are ‘live’ and can be updated. In the Tiles and Lock Screen Notifications module, you learnt how to update the tiles :From apps, using the ShellTiles APIFrom background agents using the ShellTiles APIFrom ShellTileScheduleThe fourth way that tiles can be updated is from logic running on a backend server, which updates tiles by using the services of Microsoft Push Notifications Services (MPNS).
Push NotificationsPush Notifications allows asynchronous messages to be sent to a phone. In the past, because of the nature of mobile phone networks, mobile software would usually poll a backend server to see if there was anything that had changed it was interested in. Unless you implemented costly wireless middleware, it was impossible to open a network connection to a mobile device to send it a message asynchronously – the software on the device had always to initiate the connection, to ‘call home’. Polling like this is very battery inefficient. Push Notifications is implemented to provide a mechanism to allow data to be sent to the device in a battery-efficient manner and in such a way that the user experience on the phone is not negatively affected. It works similar to email: long-lived HTTP requests are posted to the MPNS servers and the server only sends a response when it has some messages to deliver, but otherwise the request just sits there for a long time, until either it times out or a message is delivered. This is a very battery-efficient way of implementing this kind of messaging capability.
Three Kinds of NotificationsThere are three types of push notifications that a web service can use when sending information to an application: toast, Tile, and raw notifications.Raw NotificationsYou can use a raw notification to send information to your application. If your application is not currently running, the raw notification is discarded on the Microsoft Push Notification Service and is not delivered to the device.Toast NotificationsA toast notification is displayed at the top of the screen to notify users of an event, such as a news or weather alert. The toast displays for about 10 seconds unless the user dismisses it with a flick to the right. If the user taps the toast, the application that sent the toast notification is launched.Tile NotificationsA Tile notification can be used to update a Tile on Start.
Push Notification Data FlowThe diagram illustrates how Push works:Your client application running on the phone requests a push notification URI from the Push Notifications client service. The Push client service then negotiates with the Microsoft Push Notification Service (MPNS) and returns a notification URI to your client application Your client application can then send the URI to your cloud service. You must implement a web method on your service to allow client applications to register with your service, and you will have to implement some kind of list of registrations.When your web service has information to send to your client application, it uses the URI in sending a push notification to the Microsoft Push Notification Service.MPNS in turn routes the push notification to the application running on a Windows Phone device. Depending on the format of the push notification and the payload attached to it, the information is delivered as raw data to the application, the application's Tile is visually updated, or a toast notification is displayed. The Microsoft Push Notification Service sends a response code to your web service after a push notification is sent indicating that the notification has been received and will be delivered to the device at the next possible opportunity. However, the Microsoft Push Notification Service does not provide an end-to-end confirmation that your push notification was delivered from your web service to the device.
Push Notification FeaturesEach phone can support up to 30 apps that have been run and requested a Push notifications subscription endpoint.From the server-side, you can have your web service run as unauthenticated, which means that it is limited to 500 push notifications per subscription – that means 500 to your app on each phone – not so much of a limit!However, if your web service is authenticated (which is recommended), there is no limit on the number of notifications, you communicate with MPNS over an HTTPS encrypted channel and you can set up a callback registration request that associates a callback URI and message with a subscription. After a callback is registered, if the device transitions into an inactive state, the Microsoft Push Notification Service will send the message to the callback URI. After the device returns to an active state, the web service should re-register the callback.
Toast Message ContentThe payload for a Toast Notification must conform to a specific XML schema. This is because a toast notification is handled by the Push Notification Client Services layer if the app is not running – in this case it pops up a toast notification on the screen.If the app is running, and your code has subscribed to the appropriate event, the content of the toast message is delivered to the app.
This is an example of a toast notification. You must also specify the headers as shown.
Tile Message ContentThe payload for a Tile Update is never delivered to the app, and must again conform to a specific XML schema. The data in this message is handled by the Push Notifications Client Services layer on the phone and used to update tiles.Specify the headers on this notification as shown.
Each of the new tile templates on Windows Phone 8 has it’s own specific XML schema for the messages.This one shows the FlipTile notification. Notice that you can clear any property on the tile by specifying the Action="Clear“ attribute.
The CycleTile message can specify from one to nine images.Note, unlike with FlipTile or IconicTile, these images cannot be at remote (web) URLs. They must specify an image in the app package (using an ms-appx:/// URI) or an image in the local folder, using an ms-appdata:///local/shared/shellcontent URI).
Finally, the IconicTile template schema
Raw Message ContentThis is an example of the payload you can send for a RAW message. Since a RAW message is delivered only to your app, and only if it is the running application, the format of the payload is entirely application-specific, so can be anything you want.This example formats some data as XML.
For demonstration notes please look in the “Demonstration” folder and open the corresponding Word document.
Response Custom HeadersAfter your web service sends a push notification to the Microsoft Push Notification Service, your web service will receive one of many response codes with possible custom headers. The information you get back is rich and fairly complicated. The values you get may indicate that:The message has been queued OK and will be sent when possibleThe URI you passed is for an expired subscriptionThe target device may be ‘temporarily disconnected’You have exceeded the quota and must not send any more notifications for a certain period of timeYou should study the response codes as described in the documentation at the URI given on the slide so that you can implement the correct logic in your web service.
ReviewPush Notifications are an efficient and battery-friendly way of sending notifications from a server to an app on a phoneRaw notifications go only to a running appToast notifications go to the app if it is running, otherwise display as a popup on the phone Tile notifications are used only to update tiles on the Start Screen