简体   繁体   中英

Unauthorized Exception when trying to register Xamarin App with Azure Notification Hub

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

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

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.

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

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

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

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.

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

Thanks in advance for any help!

Edit: 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 = new Microsoft.WindowsAzure.MobileServices.MobileServiceClient(" https://namespace.servicebus.windows.net/NotificationHubName ", new ModernHttpClient.NativeMessageHandler());

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

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

Like below:

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

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM