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
- Microsoft Azure Account – you can always get a free Microsoft Azure account from here.
- Visual Studio 2015
- 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.
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
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.
And now, the Notification Hub has been deployed which will open up a window like this.
Step 3
Go to "Manage" and click on "Access Policies", you can find the Connection Strings over here.
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.
You will be getting a "Notification Hub updated successfully!" message, as shown below.
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".
Step 2
Select the target version of Windows 8 OS on this pop-up window and click on "OK".
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.
Step 3
In the Solution Explorer, move for NuGet packages, right click on the solution name, and click on "Manage NuGet Packages".
Go for the NuGet packages manager window, click on "Browse" and search for WindowsAzure.Messaging.Managed, and click on it.
Step 4
Install the NuGet package now by clicking on "Install".
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.
Your NuGet package will get installed on your solution file, as shown below.
Step 6
On Solution Explorer, go for App.xaml.cs.
Add the following "Using Statements" in App.xaml.cs.
- using Microsoft.Phone.Notification;
- using Microsoft.WindowsAzure.Messaging;
Step 7
Add the following code in App.xaml.cs under Application_Launching.
- var channel = HttpNotificationChannel.Find("MyPushChannel");
- if (channel == null) {
- channel = new HttpNotificationChannel("MyPushChannel");
- channel.Open();
- channel.BindToShellToast();
- }
- channel.ChannelUriUpdated += new EventHandler < NotificationChannelUriEventArgs > (async(o, args) => {
- var hub = new NotificationHub("<hub name>", "<connection string>");
- var result = await hub.RegisterNativeAsync(args.ChannelUri.ToString());
- System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => {
- MessageBox.Show("Registration " + result.RegistrationId, "Registered", MessageBoxButton.OK);
- });
- });
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.
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.
Deploy the program on your device by clicking on "Device" option here.
The deployment will be undergone in your device and you can find the app in your mobile now.
Now you can find the app named “Notifyphoneapp” in your device. Click on it and a registration message will be displayed in the app.
Keynotes in Short - Creating Notification Hubs in Azure.
- Developing a Windows Phone Silverlight app on Visual Studio 2015.