我编写了一个Azure功能,并将输出连接到通知中心,以使用APNS发送推送通知.只要我将通知发送到所有已注册的设备,它就可以正常工作,但我不知道如何使用标签来解决特定用户的问题.如果我尝试使用标记,我会收到一条错误消息,说“执行函数时出现异常:Functions.SendSinglePushNotification.Microsoft.Azure.WebJobs.Host:函数返回后处理参数通知时出错:Microsoft.Azure.NotificationHubs:notification.标签属性应为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);
但这不起作用.我怎样才能实现这一目标?
非常感谢和最诚挚的问候
解决方法
我有类似的问题.最终,对我有用的是手动构建Notification客户端.我在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); }