In this section we cover how
Microsoft push notifications work, when to use notifications, and how to
add push notifications to your application. First we start with an
overview of how Microsoft push Notifications work.
Microsoft Push Notification Services (MPNS) provides a
facility to connect your Windows Phone 7 application to the server
without requiring continuous polling by the application. Polling works
and is functional, but if an application is continuously polling and the
server response is "nothing to download," the application is
potentially consuming battery without any user benefit.
NOTE
Push notifications are not a new concept in the industry. Other platforms include push notification services as well.
Another challenge with polling is that Windows Phone 7
does not support multitasking for third-party applications running on
the device. Therefore, for an application to poll for data it must
remain in the foreground as the active application. While not letting
any third-party application to drain the battery polling for data in the
background helps to enhance the end-user experience, applications still
need to be able to react to application data and status changes even
when not running.
1. Raw Push Notification Description
Microsoft Push Notification Services raw push
notifications sends the actual data to the application. You can use raw
notifications in addition to tile and toast notifications to send
information directly to your application. It can be up to 1kb in size.
The idea is to send a toast that something has happened and then send a
raw notification to the application so that it can display the
corresponding data when the application is launched.
2. Toast Push Notification Description
Microsoft Push Notification Services allows an
application to receive notifications that data has been updated or
application status has changed even when the application is not running.
When a toast notification is received, the user receives a "toast,"
which is a little pop-up across the top of the screen that the user can
click on, resulting in launching your application. Figure 1 shows a sample toast notification from the MSDN documentation.
The toast notification is system generated. It
displays over any application running, whether the Start screen or
running application. If the application that expects to receive the
notification is running, the toast notification is not displayed, but
because the application is already running, the application simply
processes and displays the data directly.
An important user experience consideration is to use
toast notifications sparingly and with flexible configuration settings
to help the user tune which notifications they are interested in
receiving. The reason why is because toast notifications are disruptive,
appearing over top any running application, even an action game.
NOTE
First party applications such as the phone dialer
and other built-in applications can multitask with a running
third-party application, as well as with other first-party applications.
3. Tile Notification
Another type of notification is a tile notification,
which updates the application tile if, and only if, the user decided to
pin your application's tile to the Windows Phone 7 Start screen. Tile
notifications can consist of a value for the tile Title value, a Count
value (between 1 and 99), as well as a 173 × 173 background image for
the tile. There are not any additional customizations available but with
a little bit of server-side work your application can dynamically
generate custom images for the tile background.
4. How Microsoft Push Notifications Work
For MPNS to work by sending important data to the
user's phone, it implies some sort of server-side component to the
Windows Phone 7 application receiving the notification, which is
correct. Essentially MPNS is a proxy between the server-side component
of your solution and the Windows Phone 7 application component. Figure 2 provides a graphical representation of how MPNS works.
The user via their Windows Phone 7 phone initiates
the request to MPNS. MPNS receives the request and passes a URI back to
the application. The application then passes the URI to the server-side
application component along with information on which notifications the
user would like to receive such as scores for their favorite sports
team.
The server-side component now has all the information
it needs, the "address" to send the notification to via MPNS as well as
the criteria the user has chosen for what notifications the user would
like to receive.
4.1. Getting Started with Push Notifications
Note from Figure 2
that there is a server-side component that you must create in order to
support push notifications. The server-side component performs the
following two primary tasks:
Receive registration requests and
subscription criteria (i.e., send me a notification for breaking news
regarding my favorite sports team) from the Windows Phone 7 application.
Perform subscription matching and then send push notification requests to MPNS.
For all of the gory details on push notifications, I recommend reviewing the MSDN documentation to start:
http://msdn.microsoft.com/en-us/library/ff402537(v=VS.92).aspx
Also, there is a full set of Windows Phone 7 labs including labs on push notifications available here:
www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=ca23285f-bab8-47fa-b364-11553e076a9a
For this section, I want to focus on the Windows Push
Notification Server Side Helper Library created by the Microsoft
evangelism team to assist developers quickly implement push notification
services. The library supports sending all three push notification
messages available, specifically Raw, Tile, and Toast. The library
abstracts out the complexity of implementing push notifications to just a
few lines of code for your application.
To get started, obtain a copy of the Push Notification Server Side Helper for WP7 "Recipe" from this link:
http://create.msdn.com/en-us/education/catalog/article/pnhelp-wp7
The sample code included with the download provides a
very powerful example of how to work with the library it includes a
"server" application as a Windows Presentation Foundation client. A real
application could be a Windows Service or similar construct on other
platforms.
What's great about the sample is that it demonstrates
a dialog between the client and the server. For example, the client
keeps the server aware of whether the user has pinned the application
tile. Once the user has pinned the tile the server-side application is
made aware and prepares to send tile updates. Even just switching tabs
on the server-side WPF application results in UI updates via RAW
notifications on the WP7 application. Run the sample to see it in
action.
5. Updating Application Tile Without Push Notification Services
Microsoft has done research that indicates end-users
like applications that support live tiles. It promotes "at a glance"
information delivery, such as when the Outlook tile indicates the number
of unread messages. As mentioned in the previous section, Microsoft
Push Notification Services provides fine-grained control over tile
updates for maximum personalization.
There are situations where an ISV develops an
application that doesn't need full push notification functionality but
the ISV would still like to support tile updates. Windows Phone 7
provides a client-side pull API that allows an application to update its
pinned tile on a pre-defined basis of every hour, every day, or every
week. No other update schedule is available. Delivery times will vary
within the specified schedule meaning if an application registers for a
tile update on a daily basis, the tile update can happen at any time
within that time period. Still, this can provide a simple way to add a
Live Tile to an application.
The API that provides this functionality is the Microsoft.Phone.Shell.ShellTileSchedule API. The API takes a URI to a web service or image located on a web site as well as an interval setting. Listing 1 shows the code.
Example 1. ShellTileNotificationSample MainPage.xaml.cs
using System;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
namespace ShellTileNotificationSample
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
}
//store as global variable so that the schedule
//can be Started and Stopped.
private ShellTileSchedule shellTileSchedule;
private void StartShellTileAppBarBtn_Click(object sender, EventArgs e)
{
StartShellTileSchedule();
}
private void StartShellTileSchedule()
{
shellTileSchedule = new ShellTileSchedule();
shellTileSchedule.Recurrence = UpdateRecurrence.Interval;
shellTileSchedule.Interval = UpdateInterval.EveryHour;
shellTileSchedule.StartTime = DateTime.Now;
shellTileSchedule.RemoteImageUri =
new Uri(@"http://apress.com/resource/bookcover/9781430232193?size=medium");
shellTileSchedule.Start();
}
private void StopShellTileAppBarBtn_Click(object sender, EventArgs e)
{
if (shellTileSchedule != null)
shellTileSchedule.Stop();
}
}
}
|
Create a private variable for the ShellTileSchedule
instance so that you can stop it at a later time if the user decides
for some reason to want to turn off Live Tile updates. In this example,
two application bar buttons titled start and stop control the schedule. Once the tile is pinned it can take up the full interval time before it is updated.
Tile updates do not occur if the phone is in an idle
state, meaning the screen is off or the lock screen is visible. As soon
as the screen is on and unlocked the tile will update. Figure 3 shows the updated tile.
The image at the URI must be less than 80KB in size
with a maximum download time of 15 seconds. If the API cannot download
the image within 15 seconds three consecutive times the schedule will be
removed.
You can have more than one
ShellTileSchedule available in your application, although only one
schedule can be active at a time. As an example, let's say you are
building an application that provides news coverage for all 32 NFL
football teams. You could create 32 URIs that are updated with team
specific content and present a menu to the user that says something like
"which team would you like to get live tile updates for?" Once the user
selects a team, create the corresponding ShellTileSchedule to that
team's URI, and voila! Customized live tile updates with just a few
lines of code.