You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

推送通知返回NoTargetFound问题排查求助

Azure Notification Hub 推送返回NoTargetFound但注册存在,重试后可成功的问题排查
  • 近期遇到推送异常:部分通知无法被用户正常接收,我们使用APS和FCMv1模板注册,将userId设为注册标签。
  • 根据官方文档,NoTargetFound结果表示通知中心未找到匹配标签表达式的注册,但实际通过GetRegistrationsByTagAsync查询能找到对应注册。
  • 编写了测试控制台应用(代码如下),测试发现首次发送通知大多返回NoTargetFound,但重复调用SendNotificationsAsync(notification, tagExpression)几次后,最终会返回Completed,用户可正常收到通知。
  • 该问题出现在两个不同的Notification Hub(标准版、免费版各一个),但全新创建的免费版Hub不会出现此问题。
  • 目前可以通过创建新Hub并迁移解决,但不确定未来新Hub是否会复现,需要排查根本原因。
public async Task RegisterAndSendPush()
{
    // 新创建的Hub运行正常,旧Hub会出现TargetNotFound错误,需要重试才能发送成功
    var connectionString = "<azure-notificationhub-sharedaccesskey>";
    var hubPath = "<hub-name>";

    var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubPath);

    var deviceToken = "my-device-token";
    var userId = "my-user-id";
    var languageCode = "en";

    var registration = await hubClient.GetRegistrationsByTagAsync(tag: userId, top: 0);
    if (registration is null)
    {
        var template = $@"
        {{
            ""aps"": {{
              ""alert"": {{
                ""body"": ""$(message_{languageCode})"",
                ""title"": ""$(title_{languageCode})""
              }}
            }}
        }}";
        
        await hubClient.CreateOrUpdateRegistrationAsync(new AppleTemplateRegistrationDescription(deviceToken, template, [userId])
        {
            RegistrationId = await hubClient.CreateRegistrationIdAsync(),
            TemplateName = "my-template"
        });
    }

    for (var i = 0; i < 5; i++)
    {
        await SendTestNotification(hubClient, userId, i + 1);
        await Task.Delay(1000);
    }
}

private async Task SendTestNotification(NotificationHubClient hubClient, string userId, int i)
{
    var templateProperties = new Dictionary<string, string>()
    {
        ["message_en"] = "en " + i.ToString(),
        ["title_en"] = DateTimeOffset.Now.ToString("t"),
        ["message_fr"] = "fr " + i.ToString(),
        ["title_fr"] = DateTimeOffset.Now.ToString("t")
    };

    var notification = new TemplateNotification(templateProperties);

    await InternalSendAndRetry(hubClient, notification, tagExpression: userId, i);
}

private async Task InternalSendAndRetry(NotificationHubClient hubClient, TemplateNotification notification, string tagExpression, int i)
{
    Console.WriteLine($"Sending notification '{i}' to '{tagExpression}'");

    var outcome = await hubClient.SendNotificationAsync(notification, tagExpression);
    if (string.IsNullOrWhiteSpace(outcome.NotificationId))
    {
        // Hub未返回notificationId,无法确认结果
        return;
    }

    var details = await hubClient.GetNotificationOutcomeDetailsAsync(outcome.NotificationId);

    while (details.State == NotificationOutcomeState.Processing)
    {
        Console.WriteLine($"State: {details.State}, Details: {details.PnsErrorDetailsUri}, ApnsOutcomeCounts: {details.ApnsOutcomeCounts?.FirstOrDefault().Key}, FcmV1OutcomeCounts: {details.FcmV1OutcomeCounts?.FirstOrDefault().Key}");
        await Task.Delay(1000);
        details = await hubClient.GetNotificationOutcomeDetailsAsync(outcome.NotificationId);
    }

    if (details.State == NotificationOutcomeState.NoTargetFound)
    {
        var registration = await hubClient.GetRegistrationsByTagAsync(tagExpression, top: 0);
        if (registration is null)
        {
            Console.WriteLine("NoTargetFound且无对应注册");
            return;
        }
        Console.WriteLine("NoTargetFound但找到匹配注册,正在重试");
        await InternalSendAndRetry(hubClient, notification, tagExpression, i);
    }
    else
    {
        Console.WriteLine($"State: {details.State}, Details: {details.PnsErrorDetailsUri}, ApnsOutcomeCounts: {details.ApnsOutcomeCounts?.FirstOrDefault().Key}, FcmV1OutcomeCounts: {details.FcmV1OutcomeCounts?.FirstOrDefault().Key}");
    }
}

内容的提问来源于stack exchange,提问作者matomato13

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 00:07:04