【问题标题】:Notification Hub Delivery failure C#通知中心传递失败 C#
【发布时间】:2017-09-29 13:34:45
【问题描述】:

我有两个通知中心客户端(nodejs 和 C#),都用于将消息推送到中心。

Node 客户端发送完美,但 C# 客户端完成时发送 no 消息。

下面是每个客户端使用的sn-ps。

C#

NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString("<Connection String>", "<Hub Name>");
var notice = @"{'aps':{'alert':'Notification Hub test notification'}}";
var result = await hub.SendAppleNativeNotificationAsync(notice, "<tag>");
Console.WriteLine(result.State);

NodeJS

var azure = require('azure');
var notificationHubService = azure.createNotificationHubService('<Hub Name>','<Connection String>')
var notice = "{'aps':{'alert':'Notification Hub test notification'}}"
notificationHubService.apns.send("<tag>", notice, function(error, res){
    console.log(res);
});

在发送 Android 通知和直接从 Azure 门户测试功能发送的消息时,两者都可以正常工作。

任何帮助将不胜感激。

【问题讨论】:

    标签: c# azure push azure-notificationhub azure-servicebus-queues


    【解决方案1】:

    邓肯,

    我认为问题是因为您使用单引号时您的有效负载格式不正确。请尝试以下操作:

    var notice = "{\"aps\":{\"alert\":\"Notification Hub test notification\"}}";
    

    【讨论】:

    • 我检查了这个并且有效载荷是有效的,结果的状态是“入队”。如果我故意破坏有效负载,我会得到“(400)错误请求。提供的通知有效负载无效”。
    • @Duncan 通知发送后,您设备的注册是否被删除?
    • 不,通知中心上的注册仍然存在,我仍然可以从节点客户端推送。
    • 你是对的,但你的消息结构是错误的。
    【解决方案2】:

    您可以尝试使用EnableTestSend 功能并查看NotificationOutcome 属性以获取详细的错误消息。这将向 10 台设备发送测试发送消息。

    bool enableTestSend = true;
    NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(connString, hubName, enableTestSend);
    
    var outcome = await hub.SendWindowsNativeNotificationAsync(toast);
    Console.WriteLine(outcome.State);
    
    foreach (RegistrationResult result in outcome.Results)
    {
        Console.WriteLine(result.ApplicationPlatform + "\n" + result.RegistrationId + "\n" + result.Outcome);
    }
    

    【讨论】:

    • 我已经用测试标志运行了集线器,结果没有区别。发送后的状态是“入队”,但没有送达。
    【解决方案3】:

    TLDR - 不允许 ''(单引号)!有效载荷必须包含alert

    不是一个非常令人兴奋的解决方案,但是......

    • 如果通知负载即使在字符串文字中也包含单引号,则通知中心会将其排入队列而不会出现格式错误的异常,但是当它到达 APNS 时,它将不会被传递,因为根据他们的说法,负载是无效的。
    • 如果通知负载不存在 alert 键,它也会被排队,但不会由 APNS 传递

    【讨论】:

      【解决方案4】:

      正如我同事的回答中已经提到的,您需要在 C# 中使用双引号作为第一步。

      其次,还需要对json字符进行转义: 您的有效负载应如下所示:

      var notice = "{{\"aps\":{{\"alert\":\"Notification Hub test notification\"}}}}";
      

      基本上你可以通过添加额外的 {{ }} 来转义 json 大括号 { }。

      这会将有效的 json 有效负载发送到 SendAppleNativeNotificationAsync() 方法。现在发送的有效载荷如下所示:

      {"aps":{"alert":"Notification Hub 测试通知"}}(这是 iOS 上正确的通知格式)

      这是来自 Apple 开发者网页的valid json payloads for iOS

      您始终可以使用您正在使用的 Azure 通知中心中的“测试发送”功能来测试您发送的 json 是否是有效的通知负载。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-11
        • 2017-11-11
        • 1970-01-01
        • 1970-01-01
        • 2011-07-03
        • 1970-01-01
        相关资源
        最近更新 更多