Getting Started with Notification Hubs
This topic shows you how to use Azure Notification Hubs to send push notifications to a Windows Store or Windows Phone 8.1 (non-Silverlight) application. If you are targeting Windows Phone 8.1 Silverlight, please refer to the Windows Phone version. In this tutorial you create a blank Windows Store app that receives push notifications using the Windows Push Notification service (WNS). When complete, you will be able to broadcast push notifications to all the devices running your app using your notification hub.
This tutorial walks you through these basic steps to enable push notifications:
- Register your app for push notifications
- Configure your Notification Hub
- Connecting your app to the Notification Hub
- Send notifications from your back-end
This tutorial demonstrates a simple broadcast scenario using Notification Hubs. Be sure to follow along with the next tutorial to learn how to use Notification Hubs to address specific users and groups of devices. This tutorial requires the following:
Microsoft Visual Studio Express 2013 for Windows with Update 2
This version of Visual Studio is required to create a universal app project. If you just want to create a Windows Store app, you need Visual Studio 2012 Express for Windows 8.
An active Windows Store account
An active Azure account.
If you don't have an account, you can create a free trial account in just a couple of minutes. For details, see Azure Free Trial.
Completing this tutorial is a prerequisite for all other Notification Hubs tutorials for Windows Store apps.
Register your app for the Windows Store
To send push notifications to Windows Store apps from Mobile Services, you must submit your app to the Windows Store. You must then configure your notification hub to integrate with WNS.
If you have not already registered your app, navigate to the Submit an app page in the Dev Center for Windows Store apps, log on with your Microsoft account, and then click App name.
Type a name for your app in App name, click Reserve app name, and then click Save.
This creates a new Windows Store registration for your app.
In Visual Studio, create a new Visual C# Store Apps project using the Blank App template.
In Solution Explorer, right-click the Windows Store app project, click Store, and then click Associate App with the Store....
The Associate Your App with the Windows Store wizard appears.
In the wizard, click Sign in and then log in with your Microsoft account.
Click the app that you registered in step 2, click Next, and then click Associate.
This adds the required Windows Store registration information to the application manifest.
(Optional) Repeat steps 4-6 for the Windows Phone Store app project.
Back in the Windows Dev Center page for your new app, click Services.
In the Services page, click Live Services site under Microsoft Azure Mobile Services.
In the App Settings tab, make a note of the values of Client secret and Package security identifier (SID).
The client secret and package SID are important security credentials. Do not share these values with anyone or distribute them with your app.
Configure your Notification Hub
Log on to the Azure Management Portal, and click NEW at the bottom of the screen.
Click on App Services, then Service Bus, then Notification Hub, then Quick Create.
Type a name for your notification hub, select your desired Region, and then click Create a new Notification Hub.
Click the namespace you just created (usually notification hub name-ns), then click the Configure tab at the top.
Select the tab Notification Hubs at the top, and then click the notification hub you just created.
Select the tab Configure at the top, enter the Client secret and Package SID values you obtained from WNS in the previous section, and then click Save.
Select the tab Dashboard at the top, and then click Connection Information. Take note of the two connection strings.
Your notification hub is now configured to work with WNS, and you have the connection strings to register your app and send notifications.
Connecting your app to the Notification Hub
In Visual Studio, right-click the solution, then click Manage NuGet Packages.
This displays the Manage NuGet Packages dialog box.
This downloads, installs, and adds a reference in all projects to the Azure Messaging library for Windows using the WindowsAzure.Messaging.Managed NuGet package.
Open the App.xaml.cs project file and add the following
In a universal project, this file is located in the
Also in App.xaml.cs, add the following InitNotificationsAsync method definition to the App class:
private async void InitNotificationsAsync()
var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
var hub = new NotificationHub("<hub name>", "<connection string with listen access>");
var result = await hub.RegisterNativeAsync(channel.Uri);
// Displays the registration ID so you know it was successful
if (result.RegistrationId != null)
var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
This code retrieves the ChannelURI for the app from WNS, and then registers that ChannelURI with your notification hub.
Make sure to replace the "hub name" placeholder with the name of the notification hub that appears in the portal on the Notification Hubs tab (for example, mynotificationhub2 in the previous example). Also replace the connection string placeholder with the DefaultListenSharedAccessSignature connection string that you obtained in the previous section.
At the top of the OnLaunched event handler in App.xaml.cs, add the following call to the new InitNotificationsAsync method:
This guarantees that the ChannelURI is registered in your notification hub each time the application is launched.
In Solution Explorer double-click Package.appxmanifest of the Windows Store app, in Notifications, set Toast capable to Yes:
From the File menu, click Save All.
(Optional) Repeat the previous step in the Windows Phone Store app project.
Press the F5 key to run the app. A popup dialog with the registration key is displayed.
(Optional) Repeat the previous step to run the other project.
Your app is now ready to receive toast notifications.
Send notification from your back-end
Right-click the solution, select Add and New Project..., then under Visual C# click Windows and Console Application and click OK.
This adds a new Visual C# console application to the solution. You can also do this in a separate solution.
Right-click the , click Tools, then click Library Package Manager, then click Package Manager Console.
This displays the Package Manager Console.
In the console window, set Default project to your new console application project, then in the console window execute the following command:
This adds a reference to the Azure Service Bus SDK with the WindowsAzure.ServiceBus NuGet package.
Open the file Program.cs and add the following
In the Program class, add the following method:
private static async void SendNotificationAsync()
NotificationHubClient hub = NotificationHubClient
.CreateClientFromConnectionString("<connection string with full access>", "<hub name>");
var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";
Make sure to replace the "hub name" placeholder with the name of the notification hub that appears in the portal on the Notification Hubs tab. Also, replace the connection string placeholder with the connection string called DefaultFullSharedAccessSignature that you obtained in the section "Configure your Notification Hub."
Make sure that you use the connection string with Full access, not Listen access. The listen access string does not have permissions to send notifications.
Then add the following lines in the Main method:
With the console application set as the startup project, press the F5 key to run the application.
You will receive a toast notification on all registered devices. Clicking or taping on the toast banner loads the app.
You can find all the supported payloads in the toast catalog, tile catalog, and badge overview topics on MSDN.
In this simple example you sent broadcast notifications to all your Windows devices. In order to target specific users, refer to the tutorial Use Notification Hubs to push notifications to users. If you want to segment your users by interest groups, see Use Notification Hubs to send breaking news. To learn more about how to use Notification Hubs, see Notification Hubs Guidance.