gpt4 book ai didi

c# - 尝试向 Azure 通知中心注册 Xamarin 应用程序时出现未经授权的异常

转载 作者:行者123 更新时间:2023-12-03 03:06:02 24 4
gpt4 key购买 nike

我们正在尝试使用 Azure 通知中心通过 GCM/Firebase 将推送通知传递到 Xamarin Forms 应用程序。

这是我们用来在 PCL 中创建 MobileServiceClient 对象的代码:

public App()
{
mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("https://namespace.servicebus.windows.net/NotificationHubName", new ModernHttpClient.NativeMessageHandler());
}

据我了解文档,我们不使用自定义 HTTP 处理程序并不重要,因为我们不需要提供任何额外的 header 或类似的内容。我们只需要通知,此时不需要 Azure 提供身份验证(我们使用 Azure Active Directory B2C)。但我可能是错的,因为缺少身份验证 header 显然可能会导致未经授权的异常。

这是我们用来尝试向 Azure 通知中心注册我们的应用程序的代码:

public async void Register(Microsoft.WindowsAzure.MobileServices.Push push, IEnumerable<string> tags)
{
try
{
const string templateBodyGCM = "{\"data\":{\"message\":\"$(messageParam)\"}}";

JObject templates = new JObject();
templates["genericMessage"] = new JObject
{
{"body", templateBodyGCM}
};

await push.RegisterAsync(RegistrationID, templates);
Log.Info("Push Installation Id", push.InstallationId.ToString());
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
Debugger.Break();
}
}

不幸的是,await push.RegisterAsync(RegistrationID, templates) 中抛出异常:

{Microsoft.WindowsAzure.MobileServices.MobileServiceInvalidOperationException: The request could not be completed.  (Unauthorized)

我们成功从 GCM 收到注册 ID,并在尝试向 Azure 注册我们的应用程序以接收推送通知时遇到异常。

不幸的是,大多数有关通知中心的示例都使用旧代码,不需要 MobileServiceClient 构造函数中的第二个参数。该代码基于此存储库:https://github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzurePush

预先感谢您的帮助!

编辑:测试设备上打开了日期/时间同步。这似乎给其他开发者带来了类似的问题,但似乎不是这里的问题。

最佳答案

mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("https://namespace.servicebus.windows.net/NotificationHubName", new ModernHttpClient.NativeMessageHandler());

根据您的描述和错误,我发现您向MobileServiceClient对象传递了错误的url参数。

Microsoft.WindowsAzure.MobileServices.MobileServiceClient 的 URL 是您的移动应用的 URL,而不是通知中心 URL。

如下:

mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("http://yourmobileappname.azurewebsites.net", new ModernHttpClient.NativeMessageHandler());

在移动服务门户中设置与通知中心的连接后,azure mobile SDK将自动根据正确的MobileServiceClient获取推送对象。

我建议您还可以检查您是否已连接到移动应用程序中的通知中心,如下所示:

enter image description here

关于c# - 尝试向 Azure 通知中心注册 Xamarin 应用程序时出现未经授权的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44395331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com