Azure 通知中心 SDK 未设置自定义安装 ID

问题描述 投票:0回答:1

我正在使用 Azure 通知中心 SDK 为设备设置新安装。当我调用该服务时,我使用自定义字符串设置 InstallationId 属性。 然而,当我使用该字符串进行安装时,我收到 404 Not Found 响应。

var installation = new Installation()
        {
            InstallationId = deviceInstallation.InstallationId.Replace(" ", ""),
            PushChannel = deviceInstallation.PushChannel.Replace(" ", ""),
            PushChannelExpired = false,
            Tags = deviceInstallation.Tags,
            ExpirationTime = DateTime.Now.AddYears(1)
        };

 if (deviceInstallation.ExpiryDate.HasValue)
        {
            installation.ExpirationTime = deviceInstallation.ExpiryDate.Value;
        }

        if (_installationPlatforms.TryGetValue(deviceInstallation.Platform, out var platform))
        {
            installation.Platform = platform;
        }
        else
        {
            return null;
        }     
            await _hub.CreateOrUpdateInstallationAsync(installation, token);

然后我使用 PNS 令牌检索注册,以检查注册是否已完成,结果确实如此。

await _hub.GetRegistrationsByChannelAsync(channelToken, 100); 
c# azure asp.net-core push-notification azure-notificationhub
1个回答
0
投票

尽管如此,当我使用该字符串进行安装时,我收到了 404 Not Found 响应。

验证您的 Azure 通知中心配置、连接字符串和中心名称。我已经按照您的要求实现了以下代码,请检查。

using Microsoft.Azure.NotificationHubs;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

class Program
{
    private static string connectionString = "YourNotificationHubConnectionString";
    private static string hubName = "YourNotificationHubName";

    static async Task Main(string[] args)
    {
        var installationId = "YourInstallationId";
        var pushChannel = "YourPushChannel";
        var tags = new List<string> { "tag1", "tag2" };

        // Create an instance of NotificationHubClient
        var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);

        var installation = new Installation
        {
            InstallationId = installationId,
            PushChannel = pushChannel,
            PushChannelExpired = false,
            Tags = tags,
            ExpirationTime = DateTime.UtcNow.AddYears(1)
        };

        try
        {
            await hubClient.CreateOrUpdateInstallationAsync(installation);
            Console.WriteLine("Installation created successfully.");

            // Retrieve registrations using PNS token (pushChannel)
            var registrations = await hubClient.GetRegistrationsByChannelAsync(pushChannel, 100);
            foreach (var registration in registrations)
            {
                Console.WriteLine($"Registration Id: {registration.RegistrationId}");
                // You can perform further operations with the registration here
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"An error occurred: {ex.Message}");
        }
    }
}
  • 在检索过程中添加错误处理,以捕获安装检索期间的任何异常或错误。

  • 我在控制台应用程序中尝试并安装了 Microsoft.Azure.NotificationHubs 包。我能够构建没有错误的代码。

enter image description here

输出:

enter image description here

欲了解更多详情,请参阅此 SOlink

© www.soinside.com 2019 - 2024. All rights reserved.