尝试向Azure Notification Hub注册Xamarin App时发生未经授权的异常 [英] Unauthorized Exception when trying to register Xamarin App with Azure Notification Hub

查看:131
本文介绍了尝试向Azure Notification Hub注册Xamarin App时发生未经授权的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在尝试使用Azure Notification Hub通过GCM/Firebase向Xamarin Forms应用程序传递推送通知.

We are trying to use Azure Notification Hub to deliver push notifications via GCM / Firebase to a Xamarin Forms application.

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

This is the code we use to create the MobileServiceClient object in the PCL:

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

据我了解的文档,没有使用自定义HTTP处理程序也没关系,因为我们不需要提供任何其他标头或类似的内容.我们只需要通知,此时我们不需要Azure提供身份验证(为此,我们使用Azure Active Directory B2C).不过,我可能是错的,因为缺少身份验证标头显然会导致未授权的异常.

As far as I understood the documentation, it shouldn't matter that we are not using a custom HTTP handler as we don't need to supply any additional headers or anything like that. We just want the notifications, we don't require Azure to provide authentication at this point (we use Azure Active Directory B2C for that). I might be wrong though, as missing authentication headers could obviously cause an Unauthorized exception.

这是我们用来尝试向Azure Notification Hub注册应用程序的代码:

This is the code we use to try to register our application with Azure Notification Hub:

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();
    }
}

不幸的是,在等待push.RegisterAsync(RegistrationID,templates)中抛出了异常:

Unfortunately, an exception is thrown in await push.RegisterAsync(RegistrationID, templates):

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

我们已成功从GCM接收到注册ID,并在尝试向Azure注册我们的应用程序以接收推送通知时得到了例外.

We successfully receive a Registration ID from GCM and get the exception when trying to register our application with Azure to receive the push notifications.

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

Unfortunately, most examples concerning the Notification Hub use older code that doesn't require the second parameter in the MobileServiceClient constructor. The code is based on this repository: https://github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoAzurePush

在此先感谢您的帮助!

日期/时间同步已在测试设备上打开.这似乎引起了其他开发人员的关注,但这似乎不是这里的问题.

Date/Time synchronization is turned on on the testing device. This appearently caused similar issued for other developers, but doesn't seem to be the issue here.

推荐答案

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

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

根据您的描述和错误,我发现您已将错误的url参数传递给MobileServiceClient对象.

According to your description and error, I found you have pass the wrong url parameter to the MobileServiceClient object.

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

The Microsoft.WindowsAzure.MobileServices.MobileServiceClient's url is your mobile app's url not the notification hub url.

像下面这样:

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

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

After you have set the connection to the notification hub in the mobile service portal, the azure mobile SDK will automatic could get the push object according to the right MobileServiceClient.

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

I suggest you could also check you have already connected to the notification hub in your mobile app as below:

这篇关于尝试向Azure Notification Hub注册Xamarin App时发生未经授权的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆