Skip to content

Latest commit

 

History

History
78 lines (57 loc) · 3.34 KB

notification-hubs-send-categories-template.md

File metadata and controls

78 lines (57 loc) · 3.34 KB
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.

  1. In Visual Studio, create a new Visual C# console application:

    The Console Application link

  2. 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
    
  3. Select Enter.
    This action adds a reference to the Azure Notification Hubs SDK by using the Microsoft.Azure.Notification Hubs NuGet package.

  4. Open the Program.cs file, and add the following using statement:

    using Microsoft.Azure.NotificationHubs;
  5. 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.

  6. 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.

  7. In the Main method, add the following lines:

    SendTemplateNotificationAsync();
    Console.ReadLine();
  8. Build the console app.