Xamarin.iOS cannot register with Azure Notification Hub in iOS 13.1.2

Cdn_EuroCdn_Euro Member ✭✭✭

I have been dealing with this issue for a few days now.

Yesterday an update was released for the Xamarin.Azure.NotificationHubs.iOS NuGet package.

The way to register for the notification hub is NOW different in the sample on the Github link for the nuget package than in the Microsoft Azure documentation.

I narrowed down the problem I believe to getting the right connection string.
The Error occurs at this line: var hub = new SBNotificationHub(connectionString,HubName);

------Documentation way of registering with hub("old" way, works only for iOS 12 for me):

public override void RegisteredForRemoteNotifications(UIApplication application, NSData deviceToken)
{
    Hub = new SBNotificationHub(Constants.ListenConnectionString, Constants.NotificationHubName);

    Hub.UnregisterAllAsync (deviceToken, (error) => {
        if (error != null)
        {
            System.Diagnostics.Debug.WriteLine("Error calling Unregister: {0}", error.ToString());
            return;
        }

        NSSet tags = null; // create tags if you want
        Hub.RegisterNativeAsync(deviceToken, tags, (errorCallback) => {
            if (errorCallback != null)
                System.Diagnostics.Debug.WriteLine("RegisterNativeAsync error: " + errorCallback.ToString());
        });
    });
}

------Github sample code updated yesterday for the Xamarin.Azure.NotificationHubs.iOS nuget package:

public override void RegisteredForRemoteNotifications (UIApplication application, NSData deviceToken)
        {
            // Connection string from your azure dashboard
            var cs = SBConnectionString.CreateListenAccess(
                new NSUrl("sb://" + HUB_NAME + "-ns.servicebus.windows.net/"),
                HUB_LISTEN_SECRET);

            // Register our info with Azure
            var hub = new SBNotificationHub (cs, HUB_NAME);
            hub.RegisterNative (deviceToken, null, err => {

                if (err != null) {
                    Console.WriteLine("Error: " + err.Description);
                    homeViewController.RegisteredForNotifications ("Error: " + err.Description);
                } else  {
                    Console.WriteLine("Success");
                    homeViewController.RegisteredForNotifications ("Successfully registered for notifications");
                }
            });
        }

OR same code from different azure documenation also updated 13 hours ago as of now:

public override void RegisteredForRemoteNotifications (UIApplication app, NSData deviceToken)
{
    // Connection string from your azure dashboard
    var cs = SBConnectionString.CreateListenAccess(
        new NSUrl("sb://yourservicebus-ns.servicebus.windows.net/"),
        "YOUR-KEY");

    // Register our info with Azure
    var hub = new SBNotificationHub (cs, "your-hub-name");
    hub.RegisterNativeAsync (deviceToken, null, err => {
        if (err != null)
            Console.WriteLine("Error: " + err.Description);
        else
            Console.WriteLine("Success");
    });
}

I cannot seem to create the connection string properly.
On Azure Portal the connection string looks like this in the Notification Hub Access policies tab:

Endpoint=sb://namespace.servicebus.windows.net/;SharedAccessKeyName=ListenSharedPolicyName;SharedAccessKey=AccessKey

How to create the connection string properly please? Which is the servicebus part, which is the key(or the secret) part exactly, why is there the "-ns" part.

Best attempt so far was creating the connection string like this:

public override void RegisteredForRemoteNotifications(
            UIApplication application, NSData deviceToken)
        {

            try
            {
                var cs = SBConnectionString.CreateListenAccess(
                new NSUrl("sb://" + AppConstants.HUBNAME + "-ns.servicebus.windows.net/"),
                AppConstants.AccessKey);

                var hub = new SBNotificationHub(cs, AppConstants.DEV3_HUBNAME);

                // await hub.UnregisterAllAsync(deviceToken);

                hub.RegisterNative(deviceToken, null, err => {

                    if (err != null)
                    {
                        Console.WriteLine("Error: " + err.Description);
                        //homeViewController.RegisteredForNotifications("Error: " + err.Description);
                    }
                    else
                    {
                        Console.WriteLine("Success");
                        //homeViewController.RegisteredForNotifications("Successfully registered for notifications");
                    }
                });
            }
            catch(Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }

But I get this error:

Could not initialize an instance of the type 'Foundation.NSUrl': the
native 'initWithString:' method returned nil. It is possible to ignore
this condition by setting ObjCRuntime.Class.ThrowOnInitFailure to
false.

Best Answers

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    The error was caused by the invalid string passed into NSUrl , (like special characters , space characters ).

    What is the value of AppConstants.HUBNAME ?

  • Cdn_EuroCdn_Euro Member ✭✭✭
    edited October 4

    @ColeX The hubname is just simply a string, the actual hub name of the hub hosted in Azure the way it shows as a resource on the dashboard in Azure Portal, such as "mynotificationshub". I am starting to think this has to do with iOS 13, the Azure.NotificationHubs.iOS nuget package was updated 2 days ago and the code signature is different but still getting the error. In the sample code they add the "-ns" to the connection string, either way looks very different than the connection string that you can copy straight off Azure portal.

  • Cdn_EuroCdn_Euro Member ✭✭✭

    @CraigChampion wow thanks cant wait to give it a try.

  • imagineblueeyesimagineblueeyes USUniversity ✭✭

    Hey Cdn_Euro, whatever happened with this? I am in the same spot.

    Can you provide code samples of how to cure this?

    Thanks...

  • Cdn_EuroCdn_Euro Member ✭✭✭
    @profexorgeek thank you for your answer. I am currently using the async versions of those methods for iOS, is there any problem with that? Also the push notification registration has been working for me ever since you guys updated the WindwosAzure.NotificationHubs.iOS Nuget Packageto to version 2.0.4. Do i need to make any changes to the code since then?
Sign In or Register to comment.