Sending Notification From A Console Application Using Notification Hubs

Notification Hubs 

Notification Hub is introduced to manage the push notification in the human interface devices such as smartphones, Tablets and Desktops etc.

Notification Hubs use a full multiplatform, scaled-out push notification infrastructure, and considerably reduce the code that runs in the app backend. The following are some of the advantages using notification Hubs.

Multiple platforms

Supports for all major mobile platform such as Windows Store, iOS, Android, and Windows Phone apps for sending a push notification

Works with any backend

.NET, PHP, Java, Node, etc.

Scale

Notification hubs scale to millions of devices without the need of changes.

Rich set of delivery patterns

Broadcast – for all devices.

Unicast/Multicast – for a specified device.

To complete this blog, you need to have an Azure account. If you're new to Azure, I recommend reading Introduction to Microsoft Azure.

If you are new to notification Hubs. I recommend reading Notification Hubs in Azure

To complete this article following are the requirement you should have.

  • Visual Studio 2015 community edition or later
  • Azure Account

Let’s get started.

Open Visual Studio 2015 to create a simple console Application.

Click New Project, in the project creation wizard, select Visual C# >> Windows >> select console Application, as shown below.

 

Once the project creation is successful, click Tools, Click NuGet Package Manager and click Package Manager console.

 

This displays Package Manager console in Visual Studio 2015.

In Package Manager console, set the default project to our new console Application project and execute the command, given below.

  1. Install-Package Microsoft.Azure.NotificationHubs   
  

Open the file Program.cs and add the statement, given below.

  1. using Microsoft.Azure.NotificationHubs;   

In Program.cs file, add the method, given below.

  1. private static async void SendNotificationAsync()  
  2. {  
  3.     NotificationHubClient hub = NotificationHubClient  
  4.         .CreateClientFromConnectionString("<connection string with full access>""<hub name>");  
  5.     var toast = @"<toast><visual><binding template=""ToastText01""><text id=""1"">Hello from a .NET App!</text></binding></visual></toast>";  
  6.     await hub.SendWindowsNativeNotificationAsync(toast);  
  7. }  

NoteReplace the connection string with your connection string, which is available in Azure management portal.

 
 
 

Replace Hub name with your Hub Name that is displayed in Azure portal.

Add the code, given below in Main Function.

  1. SendNotificationAsync();  
  2. Console.ReadLine();  

After the coding process completes, it means press F5 button to execute your console Application.

Now, you receive toast notification to configure Windows or Windows phone Application.

Summary

In this blog, we will see about sending a notification, using console Application.
Ebook Download
View all
Learn
View all