尝试向 Azure 通知中心注册 Xamarin App 时出现未经授权的异常
Unauthorized Exception when trying to register Xamarin App with Azure Notification Hub
我们正在尝试使用 Azure 通知中心通过 GCM/Firebase 将推送通知传送到 Xamarin Forms 应用程序。
这是我们用来在 PCL 中创建 MobileServiceClient object 的代码:
public App()
{
mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("https://namespace.servicebus.windows.net/NotificationHubName", new ModernHttpClient.NativeMessageHandler());
}
据我对文档的理解,我们不使用自定义 HTTP 处理程序并不重要,因为我们不需要提供任何额外的 headers 或类似的东西。我们只需要通知,此时我们不需要 Azure 提供身份验证(为此我们使用 Azure Active Directory B2C)。不过我可能错了,因为缺少身份验证 headers 显然会导致未经授权的异常。
这是我们用来尝试向 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
在此先感谢您的帮助!
编辑:Date/Time 同步已在测试设备上打开。这似乎对其他开发人员造成了类似的问题,但似乎不是这里的问题。
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 移动SDK 将自动根据正确的MobileServiceClient 获取推送对象。
我建议您还可以检查您是否已经连接到您的移动应用程序中的通知中心,如下所示:
我们正在尝试使用 Azure 通知中心通过 GCM/Firebase 将推送通知传送到 Xamarin Forms 应用程序。
这是我们用来在 PCL 中创建 MobileServiceClient object 的代码:
public App()
{
mobileServiceClient = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient("https://namespace.servicebus.windows.net/NotificationHubName", new ModernHttpClient.NativeMessageHandler());
}
据我对文档的理解,我们不使用自定义 HTTP 处理程序并不重要,因为我们不需要提供任何额外的 headers 或类似的东西。我们只需要通知,此时我们不需要 Azure 提供身份验证(为此我们使用 Azure Active Directory B2C)。不过我可能错了,因为缺少身份验证 headers 显然会导致未经授权的异常。
这是我们用来尝试向 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
在此先感谢您的帮助!
编辑:Date/Time 同步已在测试设备上打开。这似乎对其他开发人员造成了类似的问题,但似乎不是这里的问题。
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 移动SDK 将自动根据正确的MobileServiceClient 获取推送对象。
我建议您还可以检查您是否已经连接到您的移动应用程序中的通知中心,如下所示: