3

I am building a prototyping using Azure Functions and Notification Hub.

Within my function I want a select a bunch of devices based on a geo spatial query and send a notification to these resulted devices.

I have notifications working with Firebase and Android but how can I target a random group of devices based on a query result?

#r "Microsoft.Azure.NotificationHubs"
#load "location.csx"


using System;
using System.Net;
using Microsoft.Azure.NotificationHubs;


public static async Task Run(LocationInfo message, IAsyncCollector<Notification> notification, TraceWriter log)
{
    log.Info($"Sending GCM notification of a new user");
    string gcmNotificationPayload = "{\"data\": {\"message\": \"test message")\" }}";
    log.Info($"{gcmNotificationPayload}");
    await notification.AddAsync(new GcmNotification(gcmNotificationPayload));
}
Chirdeep Tomar
  • 4,281
  • 8
  • 37
  • 66

1 Answers1

2

Your solution will depend on what you're trying to achieve. You didn't give much details about what kind of scenarios you're targeting.

But in general, without knowing what specifically you're looking for, you may find the following examples useful:

You might also benefit from learning about Routing and tag expressions in Notification Hubs.

Nikita R.
  • 7,245
  • 3
  • 51
  • 62
  • 1
    Unfortunately, I couldn't be more explicit in defining my problem. I have looked at both of those approaches. I am sure pre-defined tags isn't a solution for me as devices I want to target will change depending upon the data from the database. Essentially, what I am looking for is to get a bunch from devices from the database based on the spatial query and notify on those devices, they might be ios or android. – Chirdeep Tomar Mar 31 '17 at 16:29
  • If you already have the database and can run spatial queries, then keep NH registration id (or an id tag) as a column in user table. When the query returns a list of users to notify, use the registrationId or tag to push to those devices. This is no different than a generic push. Otherwise, I'm not sure whether you're asking about how to use NH itself or which DB technology to chose? – Nikita R. Mar 31 '17 at 17:02
  • I am using DocumentDB which can run spatial queries. And that's exactly what I wish to do but I didn't think tag was a suitable approach as I could have millions of devices. How can I define tags in Azure functions notification hub output binding. I am following https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-notification-hubs – Chirdeep Tomar Mar 31 '17 at 17:21
  • Bind on `tagExpression`. Your `tagExpression` will be a single tag, which will be the user ID to send the push to. When you do a device registration, provide the user ID as a tag and save the ID in the DB. When you return the query, it will become your `tagExpression`. NH was designed to handle millions on pushes, that scale is not a problem. – Nikita R. Mar 31 '17 at 19:02
  • In azure function there a way to define a tag in JSON file but not dynamically through code. – Chirdeep Tomar Mar 31 '17 at 20:26
  • There's [Template example using JSON](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-notification-hubs#template-example-using-json) section in the [guide you mentioned](https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-notification-hubs). Is that what you are looking for? – Nikita R. Mar 31 '17 at 20:32
  • I am looking for run time binding option http://stackoverflow.com/questions/39855409/how-do-i-use-binder-to-perform-dynamic-bindings-in-my-c-sharp-function but notification hub doesn't support it. No NotificationAttribute available in Microsoft.Azure.WebJobs – Chirdeep Tomar Apr 01 '17 at 12:31