使用Azure NotificationHub DirectSend方法推送通知

时间:2018-08-16 13:08:50

标签: c# azure azure-notificationhub

我正在尝试在没有注册句柄的Android平台中发送推送通知。

https://azure.microsoft.com/en-us/updates/push-notification-hubs-batch-direct-send/ https://docs.microsoft.com/en-us/previous-versions/azure/reference/mt608572(v%3dazure.100)

我尝试使用NotificationHubClient.SendDirectNotificationAsync(notification,handle)方法,但出现错误。

  

{     “ Message”:“发生错误。”,     “ ExceptionMessage”:“直接发送不支持模板通知”,     “ ExceptionType”:“ System.ArgumentException”,

public class NotificationMessage : Microsoft.Azure.NotificationHubs.Notification
{
    public NotificationMessage(IDictionary<string, string> additionalHeaders, string tag)
        : base(additionalHeaders, tag)
    {

    }
    protected override string PlatformType => "gcm";

    protected override void OnValidateAndPopulateHeaders()
    {

    }
}

public async Task<HttpResponseMessage> DirectSend(string pns,string handle, [FromBody]string message)
    {
        Microsoft.Azure.NotificationHubs.NotificationOutcome outcome = null;
        HttpStatusCode ret = HttpStatusCode.InternalServerError;           

        switch (pns.ToLower())
        {

            case "gcm":
                NotificationMessage notificationMessage = new NotificationMessage(new Dictionary<string, string>(), string.Empty);
                notificationMessage.Body = "{ \"data\" : {\"message\":\"" + "From AMS : " + message + "\"}}";
                notificationMessage.ContentType = "application/json";
                outcome = await Notifications.Instance.Hub.SendDirectNotificationAsync(notificationMessage, handle);
                break;
        }

        if (outcome != null)
        {
            if (!((outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Abandoned) ||
                (outcome.State == Microsoft.Azure.NotificationHubs.NotificationOutcomeState.Unknown)))
            {
                ret = HttpStatusCode.OK;
            }
        }

        return Request.CreateResponse(ret);
    }

注意 Microsoft.Azure.NotificationHubs.Notification是Abstract类,因此我在NotificationMessage类中实现了该类并作为参数传递。我认为我的Abstract类实现不正确。

知道我做错了什么吗?

0 个答案:

没有答案