Windows Phone 7 has now got support for Push Notifications. Before diving into the implementation let us understand the working of Push Technology.
Push Technology:
This technology involves communication in form of messages initiated by a publisher or central server. Push services follow publish-subscribe model where in the server pushes the information to the clients and the client application subscribes and pulls the information.
Windows Phone 7 Push Notification functionality support :
Windows Phone 7 does not support multitasking, hence push notification is the only method to notify the user.
The push services work as follows in line of business applications:
Steps:
1) The cloud service sends notification requests to Push Notification service.
2) It routes the notification to the application. Notifications may be one of the three types:
a. Tile Notification – The tile notifications are handled by phone ,if the application is pinned to the start page. The title, tile image and the tile number are three items that can be altered with a tile notification. The phone receives tile notifications only when application is not in running state.
b. Toast Notifications – A toast notification comes in when the application is running. If the user clicks the notification, the application will launch.
c. Raw Notifications – Raw notifications include raw data of any format and size but within limits. The phone receives them only when the application is running.
3) Once the notification type is set, a notification channel is created to receive the push notifications at the client side. A subscription is also created which allows the cloud service to push notifications to that channel. A channel is represented by a URI which contains all of the information associated with the subscription.
4) Once an application receives the push notification, it can access the cloud service using the cloud service’s protocol to retrieve any needed information.
Now lets us go through the steps to complete our demo :
Step 1: Simulating the cloud service to sending Push Notifications.
Step 2 : Setting up notification channel to receive notifications.
Step 3: Registering with the notification service.
Step 4 : A client application on Windows Phone 7 receiving notifications.
The solution structure will include 3 projects:
NotificationService: A WCF project acting as a background process to send messages.
NotificationSender: A WPF project sending messages.
NotificationConsumer: Windows Phone 7 client application receiving messages and displaying them as notifications.
NotificationService Project:
The service contract includes methods to send toast messages, send raw messages and let client application subscribe for notifications:
|
[ServiceContract] //function to let client application subscribe to notification service. //method to send toast notifications //method to send raw notifications. } |
The class(Service1.svc.cs) implementing the service is as follows:
|
namespace NotificationService //Notification Types //Time interval required to send messages. public class Service1 : IService1 private static Dictionary<Guid, Uri> _clientUris = new Dictionary<Guid, Uri>(); public void Subscribe(Guid _uniqueID, string uri) else public void SendToastNotification(string message) var messageBytes = System.Text.Encoding.UTF8.GetBytes(toastMessage); foreach (var uri in _clientUris.Values) public void SendRawNotification(string message) foreach (var uri in _clientUris.Values) public void SendTileNotification(string message) private void SendMessage(Uri uri, byte[] messageBytes, NotificationType notificationType) request.Headers.Add("X-MessageID", Guid.NewGuid().ToString()); if(notificationType == NotificationType.Toast) request.Headers.Add("X-NotificationClass", ((int)BatchingInterval.RawImmediately).ToString()); using (var requestStream = request.GetRequestStream()) } |
Web.config file including endpoint settings for the service.
NotificationSender Project:
This WPF project sends text lines to service which are sent either as toast notifications or raw notifications.
MainWindow.xaml
Code behind for MainWindow.xaml.cs
|
public partial class MainWindow : Window private void SendtoastNotification_Click(object sender, RoutedEventArgs e) private void SendRawNotification_Click(object sender, RoutedEventArgs e) |
NotificationConsumer Project:
MainPage.xaml: XAML Code for the above design. All messages are added as items in Listbox.
|
<Grid x:Name="LayoutRoot" Background="Transparent"> <!–TitlePanel contains the name of the application and page title–> <!–ContentPanel – place additional content here–> <ListBox x:Name="lstOffers"> |
MainPage.xaml.cs code:
|
public partial class MainPage : PhoneApplicationPage public MainPage() if (IsolatedStorageSettings.ApplicationSettings.Contains("DeviceId")) SetupNotificationChannel(); private void SetupNotificationChannel() if (_channel == null) private void RegisterForNotifications() private void HttpNotificationReceived(HttpNotificationEventArgs e) private void RegisterWithNotificationService() svc.SubscribeCompleted += (s, e) => svc.SubscribeAsync(_deviceId, _channel.ChannelUri.ToString()); private void ToastReceived(NotificationEventArgs e) private void ErrorOccurred(NotificationChannelErrorEventArgs e) lstOffers.Items.Add(e.Message); private void ChannelUriUpdated(object sender, NotificationChannelUriEventArgs e) if (!_channel.IsShellToastBound) RegisterForNotifications(); |
Now let’s run the project:
First
1) Run the NotificationService, to start the service.
2) Start a new instance of NotificationSender application .
3) Start the Windows Phone 7 application to receive notifications.
Output:
So that was a simple demo on Push Notifications on Windows Phone 7. For receiving Tile notifications you can check below links and more information:
Excellent blogs:
http://chris.59north.com/post/Using-Windows-Phone-7-Push-Notifications.aspx
MSDN Links:
http://msdn.microsoft.com/en-us/library/ff402537%28v=VS.92%29.aspx