Azure函数:向特定用户发送通知

4
我已经写了一个Azure函数并将其输出连接到通知中心,使用APNS发送推送通知。 只要我将通知发送到所有已注册的设备,它就可以正常工作,但是我不知道如何使用标签来定向发送通知给特定用户。 如果我尝试使用标签,则会出现错误消息,该消息说“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.”。 以下是我的代码:
#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); 
}

我尝试使用notification = new AppleNotification(apnsNotificationPayload,tagExpression); 但是这并不起作用。我该怎么做才能实现它呢?

非常感谢,祝好!


我有一个类似的用例,你在上面是否取得了任何进展? - Ade
1个回答

0

我曾经遇到过类似的问题。最终,对我有用的是手动构建通知客户端。我正在使用Visual Studio开发函数,所以我的代码与你的略有不同。

    [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);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接