Forum Xamarin.iOS

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 2019

    @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?
  • YonYon Member

    However, the RegisteredForRemoteNotifications method should not be marshalled on the UI thread.

    @profexorgeek It hangs for a while, then I get a timeout exception if I don't call UnregisterAll & RegisterNative on the UI thread.

  • fmbarretofmbarreto Member

    @profexorgeek, maybe you can help me. I am testing Push Notification with Xamarin Forms. I followed the steps (sample: docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/azure-services/azure-notification-hub) and with FCM on Android it worked perfectly but on IOS with APNS does not. The worst that has no error. The only thing I noticed was that when debugging you don't access the void Hub.UnregisterAll but it doesn't show any error (I inserted a try ... cacth).
    When I send a test message through the azure panel, I receive the following message: Message was successfully sent, but there were no matching targets.

  • FrankSzendzielarzFrankSzendzielarz CZMember ✭✭

    @fmbarreto @profexorgeek Likewise. I am getting "No matching targets" no matter what I register with.

  • VipinKrishnaVipinKrishna Member ✭✭

    @CraigChampion Thanks.. There was some white space inside my connection string url created the same issue. solved now...!!!

  • odinhausodinhaus Member ✭✭

    Following the sample documentation does not work for me. The UnregisterAll method never calls back. Calling UnregisterAll (without doing anything in the callback which is never called) followed by RegisterNative (whose callback is also never called) also does not register the device.

    Neither method produces an error....it just doesn't work.

    Has ANYONE been able to make this work? I've been pulling my hair out for days now basically trying to get a "Hello World" version of this code to work, and nothing works.

  • FrankSzendzielarzFrankSzendzielarz CZMember ✭✭
    edited September 17

    Ok so after literally months of hunting down the issues, we have found the underlying cause and the platform authors not only have a fix, but the entire way of doing notifications in xamarin has completely changed:

    https://github.com/Azure/azure-notificationhubs-ios/issues/95

    @odinhaus Turns out they have a different library and the SBNotificationHub stuff was legacy, and had a bug.

Sign In or Register to comment.