4
votes

i've written an Azure function and connected the output to a notification-hub to send push notifications using APNS. It works fine as long as i send the notification to all registered devices, but i don't know how to use tags in order to address a specific user. If i try to use a tag, i get an error message saying "Exception while executing function: Functions.SendSinglePushNotification. Microsoft.Azure.WebJobs.Host: Error while handling parameter notification after function returned:. Microsoft.Azure.NotificationHubs: notification.Tag property should be null."

Here's my code so far:

#r "Microsoft.Azure.NotificationHubs"
#r "Newtonsoft.Json"


using System;
using Microsoft.Azure.NotificationHubs;
using Newtonsoft.Json;using         
Microsoft.Azure.WebJobs.Host.Bindings.Runtime;

public static void Run(HttpRequestMessage req, TraceWriter log,Binder     
binder, out AppleNotification notification)
{
    string user = "Test";
    string tagExpression = "Test";
    string userTag = req.GetQueryNameValuePairs()
    .FirstOrDefault(q => string.Compare(q.Key, "userid", true) == 0)
    .Value;

    string apnsNotificationPayload = "{\"aps\": {\"alert\": \"Test: (" + user + ")\" }}";
    notification = new AppleNotification(apnsNotificationPayload); 
}

I was trying to use notification = new
AppleNotification(apnsNotificationPayload,tagExpression); but that does not work. How can i achieve that?

Thanks a lot and best regards

1
I have a similar use case, did you manage to make any progress on the above?Ade

1 Answers

0
votes

I had similar issue. Eventually, what worked for me was constructing Notification client manually. I am developing functions in Visual Studio, so my code is slightly different from yours.

    [FunctionName("MyFunction")]
    public static async Task Run([ServiceBusTrigger("queuename", AccessRights.Listen, Connection =
    "<connection-settings-name>")] string myQueueItem, TraceWriter log)
{
    log.Info($"C# Queue trigger function processed: {myQueueItem}");

    var notificationHubSas = "<DefaultFullSharedAccessSignature from Azure portal>";
    var notificationHubName = "myhub";
    var nhClient = NotificationHubClient.CreateClientFromConnectionString(notificationHubSas, notificationHubName);
    var tags = "";
    await nhClient.SendTemplateNotificationAsync(<notification payload>, tags);
}