Right now, there are 3 ways to send notifications to Windows Phone.
1. Native Notification by using MPNS (Microsoft Push Notification Service, team blog)
2. Windows Azure Mobile Services
3. Windows Azure notification hubs.
In this post I will describe short sample, which holds most explanation in code itself. Creating of the hub is in detail is described in this article. This article describes what you have to do before you start coding.
This post is a recap of all with an additional step which is missing gin the original article.
Open the configuration of the hub and either provide a certificate or check the box to enable unauthenticated push notifications.
If you don’t do that the phone will not receive any notification and no any error will be shown anywhere.
Register notification template in the app
Following code will register one toast notification with tag “wptoast”.
private void Application_Launching(object sender, LaunchingEventArgs e) { var channel = HttpNotificationChannel.Find("daenetchannel"); if (channel == null) { channel = new HttpNotificationChannel("daenetchannel"); channel.Open(); channel.BindToShellToast(); channel.BindToShellTile(); } registerNotifications(channel.ChannelUri.ToString()).ContinueWith(t=>{ if (t.Exception == null) { ///… } }); channel.ChannelUriUpdated += new EventHandler<NotificationChannelUriEventArgs>(async (o, args) => { await registerNotifications(args.ChannelUri.ToString()); }); }
private static async System.Threading.Tasks.Task registerNotifications(string uri) { var hub = new NotificationHub("xyz..", "Endpoint=sb://****.servicebus.windows.net/;SharedAccessKeyName=” + DefaultListenSharedAccessSignature;SharedAccessKey=****="); string wpToast2 = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" + "<wp:Notificationxmlns:wp=\"WPNotification\">" + "<wp:Toast>" + "<wp:Text1>$(msg)</wp:Text1>" + "</wp:Toast> " + "</wp:Notification>"; await hub.RegisterTemplateAsync(uri, wpToast2, "WPToastTemplate", new List<string>() { "wptoast" }); } |
Send notification from your service?
The code to send the toast template notification looks like this:
private void SendToastTemplateNotification() { NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString ("Endpoint=sb://******.servicebus.windows.net/; SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=****=", "xyz…"); hub.SendTemplateNotificationAsync(new Dictionary<string, string> { {"msg", m_Txt.Text} }, "wpToast").ContinueWith(t => { if (t.Exception == null) { } }) ; } |
Posted
Sep 24 2013, 08:00 PM
by
Damir Dobric