Microsoft Azure - Send Push Notification Using Notification Hub

Introduction

This is a fresh writing of mine on app development with Windows Phone, for adding Silverlight using Microsoft Azure to send push notifications from the app.

Technical Requirements

  1. Microsoft Azure Account – you can always get a free Microsoft Azure account from here.
  2. Visual Studio 2015
  3. Windows Phone device or an emulator installed on your laptop to run the app.

Creating Notification Hub in Visual Studio 2015

Notification Hubs

Notification Hubs are used for broadcasting your push notification message to millions of people who are using your apps in their phones. Here, we will be working with the native platform of Windows Phone alone. All individual users who are using your particular app can receive the Push Notification with the help of Notification Hub on their Windows Phone.

Step 1

Open a browser and go for Microsoft Azure Portal and sign in with your Azure account on this.

Click on New - Web + Mobile - Notification Hub.

Windows Phone

Step 2

Mention the details for creating your Notification Hubs, enter the below details.
  • Notification Hub name
  • New Namespace
  • Location
  • Resource group – new one or an existing one
  • Subscription
  • Pricing Tier

    Windows Phone

Click on "Create" once it is configured. You will be getting a tile on the dashboard, as shown below, where the Notification Hub is getting deployed.

Windows Phone

And now, the Notification Hub has been deployed which will open up a window like this.

Windows Phone

Step 3

Go to "Manage" and click on "Access Policies", you can find the Connection Strings over here.

Windows Phone

Step 4

Now, you should configure your Notification Hub to send unauthenticated notification for the native platform of yours. Here, I will be using Notification Service for Windows Phone (MPNS).

Click on "Notification Services" in Manage, check on the "Enable unauthenticated push", click on "More", and save the notification service.

Windows Phone

You will be getting a "Notification Hub updated successfully!" message, as shown below.

Windows Phone

Creating a Silverlight app for the Notification Hub

Step 1 Run Visual Studio 2015 in your laptop.

Click on New Project >> Visual C# >> Windows >> Windows 8 >> Windows Phone >> Blank App (Windows Phone Silverlight – Visual C#), name your app and click on "OK".

Windows Phone

Step 2

Select the target version of Windows 8 OS on this pop-up window and click on "OK".

Windows Phone

Here, I will be selecting Windows Phone 8.1.

You will be getting a Visual Studio 2015 window, as shown below, for Windows Phone Silverlight applications.

Windows Phone

Step 3

In the Solution Explorer, move for NuGet packages, right click on the solution name, and click on "Manage NuGet Packages".

Windows Phone

Go for the NuGet packages manager window, click on "Browse" and search for WindowsAzure.Messaging.Managed, and click on it.

Windows Phone

Step 4

Install the NuGet package now by clicking on "Install".

Windows Phone

Step 5

You will be getting a license agreement window for the NuGet packages. Click on “I Accept” on the license agreement, as shown below.

Windows Phone

Your NuGet package will get installed on your solution file, as shown below.

Windows Phone

Step 6

On Solution Explorer, go for App.xaml.cs.

Windows Phone

Add the following "Using Statements" in App.xaml.cs.

  1. using Microsoft.Phone.Notification;  
  2. using Microsoft.WindowsAzure.Messaging;  
Windows Phone

Step 7

Add the following code in App.xaml.cs under Application_Launching.
  1. var channel = HttpNotificationChannel.Find("MyPushChannel");  
  2. if (channel == null) {  
  3.     channel = new HttpNotificationChannel("MyPushChannel");  
  4.     channel.Open();  
  5.     channel.BindToShellToast();  
  6. }  
  7. channel.ChannelUriUpdated += new EventHandler < NotificationChannelUriEventArgs > (async(o, args) => {  
  8.     var hub = new NotificationHub("<hub name>""<connection string>");  
  9.     var result = await hub.RegisterNativeAsync(args.ChannelUri.ToString());  
  10.     System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {  
  11.         MessageBox.Show("Registration " + result.RegistrationId, "Registered", MessageBoxButton.OK);  
  12.     });  
  13. });  
Note

Replace the notification hub name and the connection string in the above code.

Step 8


In the Solution Explorer, go for Properties, expand it, and click on WMAppManifest.xml.

Go for “Capabilities” pane and check on the option of “ID_CAP_PUSH_NOTIFICATION”. This will help you to send push notifications on your app that you have developed.

Windows Phone

Step 9

To run this program, you can use an emulator or you can even use a Windows Phone Device if you have it. Connect your device with the computer. Later, you can find the “Device” option in the Visual Studio 2015, as shown below.

Windows Phone

Deploy the program on your device by clicking on "Device" option here.

Windows Phone

The deployment will be undergone in your device and you can find the app in your mobile now.

Windows Phone

Now you can find the app named “Notifyphoneapp” in your device. Click on it and a registration message will be displayed in the app.

Windows Phone

Keynotes in Short 
  1. Creating Notification Hubs in Azure.
  2. Developing a Windows Phone Silverlight app on Visual Studio 2015.

Up Next
    Ebook Download
    View all
    Learn
    View all