1
votes

How can I set up my push notifications so the user can select which notification to subscribe to? For example take a weather app, User A wants to subscribe to notification when a tornado is approaching, but User B doesn't care about that, and only wants to be notified if flood is approaching. So in the app there is a setting to allow users to subscribe to either or both of these notifications.

Now on my backend I have a webjob that is gathering the weather data from online sources and store in SQL database, I create a notification hub in Azure, and add code to my webjob like this example

https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-windows-store-dotnet-get-started-wns-push-notification#optional-send-notifications-from-a-console-app

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

When I get certain weather data that indicated Flood or Tornado can I do something like this to send the notification

if(flood)
SendNotificationAsync(flood) //only sends notification to User B
else if(tornado)
SendNotificationAsync(tornado) //only send notification to User A

Can this be accomplished using a single notification hub? Or do I have to create multiple hubs?

1

1 Answers

2
votes

Erotavlas,

To accomplish that you'll have to take a look at tags. In your case you'll have two tags: "Flood" and "Tornado".

If you check SendNotificationAsync you'll notice that you can provide tags

public Task<NotificationOutcome> SendNotificationAsync(Notification notification, IEnumerable<string> tags)

I don't know if you are using Installation or Registration model to register your devices, but both Installation and RegistrationDescription have a property named Tags.

When registering/updating a user's device, depending on what the user wants to subscribe to, you will set the proper tag(s).

Example:

Registration for Tornado

        var installation = new Installation()
        {
            InstallationId = installationId ,
            Platform = platform ,
            PushChannel = token,
            Tags = new string[] { "Tornado" }
        };

        await notificationHubClient.CreateOrUpdateInstallationAsync(installation);

Sending Notification to Tornado subscribers

   await notificationHubClient.SendNotificationAsync(notification, new string[] { "Tornado" });