【问题标题】:Continuous push message giving BadRequest from Azure从 Azure 发出 BadRequest 的持续推送消息
【发布时间】:2018-08-07 05:33:36
【问题描述】:

我的服务在 azure 通知中心注册。并使用我的 .net 服务器 API 将通知推送到特定时间范围内的特定设备。 一切正常,除了当我尝试在相同的代码中发送多个推送时,除了第一个之外,它卡在“BadRequest”。

下面是代码

public static async void SendAzzurePushNotification()
{
    for (int i = 0; i < 10; i++)
    {
        HttpStatusCode pushNotificationStatus = await CreateAndPushAsync("user_37");
        Console.WriteLine(pushNotificationStatus);
    }
}

static async Task<HttpStatusCode> CreateAndPushAsync(string tag)
{
    HttpStatusCode pushNotificationStatus = HttpStatusCode.NotImplemented;
    try
    {
        HttpResponseMessage response = null;

        string uri = "<HUBURI>";

        client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("SharedAccessSignature", <SASTOKEN>);
        client.DefaultRequestHeaders.Add("ServiceBusNotification-Format", "gcm");
        client.DefaultRequestHeaders.Add("ServiceBusNotification-Tags", tag);
        client.DefaultRequestHeaders.Add("x-ms-version", "2015-01");

        response = await client.PostAsync(uri,
                new StringContent("{\"data\":{\"message\":\"Notification Hub test notification\"}}", Encoding.UTF8, "application/json"));

        pushNotificationStatus = response.StatusCode;
    }
    catch (Exception ex)
    {
        throw;
    }

    return pushNotificationStatus;
}

上面的代码第一次给我 Created 状态,然后是 BadRequest。如果我从邮递员等客户端调用相同的 api。它工作正常。

我还尝试了来自 azure 通知中心的 nuget 包,关于代码如下。这解决了我的上述问题,但它不会返回任何状态码,我可以在我的上述代码中获得成功。

NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<CONNECTIONSTRING>", "<HUB>");
NotificationOutcome outcome = await hub.SendGcmNativeNotificationAsync("{\"data\":{\"message\":\"Notification Hub test notification\"}}", "user_37");

【问题讨论】:

  • 你以前用过Microsoft.Azure.NotificationHubs吗? bcoz 它不需要提供你用client 做的所有事情,使用上面的包你可以发送多个通知
  • 请检查我的更新代码
  • 你想要来自 hub 的状态码吗?
  • 不完全是,实际上我需要一些机制来发现设备是否接收到该通知。但是是的,即使我从 azure 获得状态,现在也很好。
  • 所以我在这里添加答案试试这个

标签: c# push-notification


【解决方案1】:

使用您的tags 和您的notification-data 调用send 方法

private static readonly string Endpoint = @"Your End Point";
private static readonly string HubName = @"You Hub Name";
private static NotificationHubClient Hub { get { return NotificationHubClient.CreateClientFromConnectionString(Endpoint, HubName); } }

public static async Task Send(string[] tags, object data)
{
    try
   {

        string payload = string.Empty;
        string json_gcm = string.Empty;

        if (data.GetType() != typeof(string))
        {
            //If your notification data is of type
            payload = JsonConvert.SerializeObject(data);
            json_gcm = "{ \"data\" : " + payload + "}";
        }
        else
        {
            //If your notification data is simply is in string
            payload = Convert.ToString(data);
            json_gcm = "{ \"data\" : {\"message\":\"" + payload + "\"}}";
        }


        // Android
        NotificationOutcome gcmOutcome = null;
        gcmOutcome = await Hub.SendGcmNativeNotificationAsync(json_gcm, tags);

        if (gcmOutcome != null)
        {
            if (!((gcmOutcome.State == NotificationOutcomeState.Abandoned) || (gcmOutcome.State == NotificationOutcomeState.Unknown)))
            {
                //Do code when notification successfully send to Android
             }
        }
    }
    catch (Exception ex)
    {
        //Do code when any exception occurred while sending notification  


    } 
}

NotificationOutcomeState: 以枚举的形式为您提供状态代码,表示您的通知是否已成功发送。

您可以根据需要忽略 if-else 块。

试一次可能对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-03
    • 2019-03-28
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-30
    相关资源
    最近更新 更多