title | description | services | author | ms.service | ms.topic | ms.date | ms.author | ms.custom |
---|---|---|---|---|---|---|---|---|
include file |
include file |
notification-hubs |
spelluru |
notification-hubs |
include |
03/30/2018 |
spelluru |
include file |
In this section, you send breaking news as tagged template notifications from a .NET console app.
-
In Visual Studio, create a new Visual C# console application:
-
On the Visual Studio main menu, select Tools > Library Package Manager > Package Manager Console and then, in the console window, enter the following string:
Install-Package Microsoft.Azure.NotificationHubs
-
Select Enter.
This action adds a reference to the Azure Notification Hubs SDK by using the Microsoft.Azure.Notification Hubs NuGet package. -
Open the Program.cs file, and add the following
using
statement:using Microsoft.Azure.NotificationHubs;
-
In the
Program
class, add the following method, or replace it if it already exists:private static async void SendTemplateNotificationAsync() { // Define the notification hub. NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<connection string with full access>", "<hub name>"); // Create an array of breaking news categories. var categories = new string[] { "World", "Politics", "Business", "Technology", "Science", "Sports"}; // Send the notification as a template notification. All template registrations that contain // "messageParam" and the proper tags will receive the notifications. // This includes APNS, GCM, WNS, and MPNS template registrations. Dictionary<string, string> templateParams = new Dictionary<string, string>(); foreach (var category in categories) { templateParams["messageParam"] = "Breaking " + category + " News!"; await hub.SendTemplateNotificationAsync(templateParams, category); } }
This code sends a template notification for each of the six tags in the string array. The use of tags ensures that devices receive notifications only for the registered categories.
-
In the preceding code, replace the
<hub name>
and<connection string with full access>
placeholders with your notification hub name and the connection string for DefaultFullSharedAccessSignature from the dashboard of your notification hub. -
In the Main method, add the following lines:
SendTemplateNotificationAsync(); Console.ReadLine();
-
Build the console app.