Introduction
This article is a continuation of the Notification Hub on Microsoft Azure series. Please go through the following link to learn more about Notification Hub.
Note - You should have a Notification Hub created to work on this demo.
Technical Requirements
- Microsoft Azure account – click here to get an Azure account.
- Visual Studio 2015 installed on your laptop.
Click here to go to Part 1: Send Push Notification with UWP Apps in Microsoft Azure.
Step 17
Open Visual Studio 2015 and create a new project for UWP as Blank App in Universal. Go for "Manage NuGet Packages" in Solution Explorer, as shown below.
Step 18
Go to "Browse" and search for "WindowsAzure.Messaging.Managed" and click "Install".
Step 19
Click on "I Accept" for the license agreement to add the WindowsAzure.Messaging.Managed NuGet Packages in your solution file.
After the package is added, you will get a notification that the package has been added.
Step 20
Open App.xaml.cs in the Solution Explorer and copy the below "Using Statements" on it.
- using Windows.Networking.PushNotifications;
- using Microsoft.WindowsAzure.Messaging;
- using Windows.UI.Popups;
Step 21
Again, in App.xaml.cs, add the below code for InitNotificationsAsync method definition to the App class.
- private async void InitNotificationsAsync()
- {
- var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync();
-
- var hub = new NotificationHub("< your hub name>", "<Your DefaultListenSharedAccessSignature connection string>");
- var result = await hub.RegisterNativeAsync(channel.Uri);
-
-
- if (result.RegistrationId != null)
- {
- var dialog = new MessageDialog("Registration successful: " + result.RegistrationId);
- dialog.Commands.Add(new UICommand("OK"));
- await dialog.ShowAsync();
- }
- }
Note
Replace the <your hub name> with your Notification Hub name and <Your DefaultListenSharedAccessSignatureconnectionstring> with your Notification Hub’s connection string which you can find in the Access Policies of Notification Hub account.
Step 22
Now, at the top of OnLaunched Event handler in App.xaml.cs, add the below method.
- InitNotificationsAsync();
Step 23
Click on the "Local Machine" to run this app on your Visual Studio 2015.
You can find the status of the project getting build on your Visual Studio in the below pane, as shown below.
You can also find the app in your Start menu of your laptop and now the app is ready to receive toast notifications.
Step 24
Let's send some notifications using "Test Send" in the Notification Hub now.
Log into Azure portal and go for the respective Notification Hub to send notifications.
Step 25
Click on "Test Send" in the Notification Hub.
Select "Windows" in Platforms dropdown.
Select Notification Type as "Toast".
Click on "Send" now.
This will send a test message with the help of Notification Hub. You can also find the result of the notification service with the help of the result pane available there.
This will help you to send notifications to all the users who are using this app in their devices all over the world, with the help of Notification Hub on Microsoft Azure.
Keynotes in Short
- Creating a Visual Studio Project on UWP.
- Adding NuGet packages.
- Sending messages from Notification Hubs on Microsoft Azure.