【问题标题】:Is there a way to send Push Notification through a NotificationHub with the Webjob SDK?有没有办法通过带有 Webjob SDK 的 NotificationHub 发送推送通知?
【发布时间】:2019-05-10 17:12:16
【问题描述】:

我对使用 Webjob SDK 还很陌生,我正在尝试使用带有 SDK 3 的 Webjob 将通知推送到 NotificationHub。

我一直在尝试使用 Microsoft.Azure.Webjobs.Extensions.NotificationHub。它似乎不适用于 Webjob SDK 3,所以我一直在使用 SDK 2..

程序.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs;

namespace WJNotificationHub
{
    class Program
    {
        static void Main()
        {
            var config = new JobHostConfiguration();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            config.UseNotificationHubs();

            var host = new JobHost(config);

            host.RunAndBlock();
        }
    }
}

函数.cs

using System.IO;
using Microsoft.Azure.NotificationHubs;
using Microsoft.Azure.WebJobs;
using Newtonsoft.Json;

namespace WJNotificationHub
{
    public class Functions
    {
        public static void ProcessQueueMessage([QueueTrigger("queue")] string message, TextWriter log, [NotificationHub] out Notification notification)
        {
            log.WriteLine(message);

            notification = new GcmNotification(message.ToGcmPayload());
        }
    }

    public static class PlatformNotificationsExtensions
    {
        public static string ToGcmPayload(this string message)
        {
            var gcmPayloadModel = new
            {
                data = new
                {
                    message = message
                }
            };

            return JsonConvert.SerializeObject(gcmPayloadModel);
        }
    }
}

使用此代码,我有以下异常:

Exception while executing function: Functions.ProcessQueueMessage
Microsoft.Azure.WebJobs.Host.FunctionInvocationException : Exception while executing function: Functions.ProcessQueueMessage ---> System.InvalidOperationException : Exception binding parameter 'notification' ---> System.NullReferenceException : La référence d'objet n'est pas définie à une instance d'un objet.   

还有没有办法用 SDK 3 做到这一点?

【问题讨论】:

    标签: c# azure azure-webjobs azure-webjobssdk


    【解决方案1】:

    System.InvalidOperationException : 异常绑定参数“通知”

    更新Azure Function and Web Jobs Tools版本并重启。

    还有没有办法用 SDK 3 做到这一点?

    简而言之

    Notification Hub 仅支持1.x,而Webjob SDK 3 支持.NETStandard 2.0

    代码可以参考article

    【讨论】:

    • 您好,感谢您的回答。我设法用 SDK 2 做到了,但现在我希望在 NotificationHub 上的同一功能中发送多个通知。文章对此进行了一些讨论,但在这一点上没有进一步讨论。您有什么见解或解决方案吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2016-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多