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

2

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 one time got 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.

And most of the times this:

================================================================= Native Crash Reporting ================================================================= Got a SIGSEGV while executing native code. This usually indicates a fatal error in the mono runtime or one of the native libraries used by your

application.

================================================================= Native stacktrace: ================================================================= 0x10473c190 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x104732a00 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x10473fc54 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x197a47894 - /usr/lib/system/libsystem_platform.dylib : 0x10299027c - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x1029903c4 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x102997b70 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x102998154 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x1029980cc - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x102995e74 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x102993aec - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x102992824 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x104714530 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x104702694 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x1029da3ac - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x102d59af4 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x104742fb4 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x1047dc688 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x1047dfb40 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x10299ce1c - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x10299cb14 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x19bd632f8 - /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore : 0x197a05610 - /usr/lib/system/libdispatch.dylib : 0x197a06184 - /usr/lib/system/libdispatch.dylib : 0x1979e935c - /usr/lib/system/libdispatch.dylib : 0x197cb63c4 - /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : 0x197cb13b8 - /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : 0x197cb08bc - /System/Library/Frameworks/CoreFoundation.framework/CoreFoundation : CFRunLoopRunSpecific 0x1a1b1c328 - /System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices : GSEventRunModal 0x102d59af4 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x104742fb4 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x1047dc688 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x1047e1834 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : mono_pmip 0x104727df0 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x10488da04 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : _Z9__isctypeim 0x1029d8f34 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x197b3b460 - /usr/lib/system/libdyld.dylib :

================================================================= Basic Fault Address Reporting ================================================================= Memory around native instruction pointer (0x197a46f44): 0x197a46f34 c0 03 5f d6 1f 20 03 0x19bd466d4 - /System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore : UIApplicationMain 0x103683020 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x1035dbc7c - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x1035dbc04 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) 0x1029d9064 - /private/var/containers/Bundle/Application/3DE84EC0-A042-42E6-8301-B233D104676A/MyAppName.iOS.app/MyAppName.iOS : (null) d5 1f 20 03 d5 01 ec 7c 92 .._.. . .. ....|. 0x197a46f44 20 00 c0 3d c3 f9 ff 10 62 04 c1 3c 02 0c 40 92 .. =....b. .<.. @. 0x197a46f54 63 00 02 cb 61 00 c0 3d 00 1c a1 4e 05 00 00 14 c...a..= ...N.... 0x197a46f64 1f 20 03 d5 1f 20 03 d5 1f 20 03 d5 20 0c c1 3c . ... ... .. . .<

================================================================= Managed Stacktrace: ================================================================= at <0xffffffff> at ApiDefinition.Messaging:void_objc_msgSend_IntPtr_IntPtr_IntPtr <0x00007> at WindowsAzure.Messaging.SBNotificationHub:RegisterNative <0x00333>
at MyAppName.iOS.AppDelegate:RegisteredForRemoteNotifications <0x002db> at System.Object:runtime_invoke_dynamic <0x00103> at <0xffffffff> at UIKit.UIApplication:UIApplicationMain <0x00007> at UIKit.UIApplication:Main <0x0002b> at UIKit.UIApplication:Main <0x00043> at MyAppName.iOS.Application:Main <0x000a3> at

System.Object:runtime_invoke_dynamic <0x00103>

azure
xamarin.ios
apple-push-notifications
asked on Stack Overflow Oct 4, 2019 by Razvan Emil • edited Oct 4, 2019 by Razvan Emil

2 Answers

2

I made this work if anyone has the same issue, below is the working code. Works with NuGet package:

Xamarin.Azure.NotificationHubs.iOS v2.0.4

  try
     {
           AzureNotifHub = new SBNotificationHub(HUB_LISTENSHARED_CONNSTRING, HUBNAME);

          // update registration with Azure Notification Hub
           await AzureNotifHub.UnregisterAllAsync(deviceToken);


           var tags = new NSSet(AppConstants.IOSSUBSCRIPTIONTAGS.ToArray());
           await AzureNotifHub.RegisterNativeAsync(deviceToken, tags);

           var templateExpiration = DateTime.Now.AddDays(120).ToString(System.Globalization.CultureInfo.CreateSpecificCulture("en-US"));
                    await AzureNotifHub.RegisterTemplateAsync(deviceToken, "defaultTemplate", AppConstants.IOS_APNS_TEMPLATE_BODY, templateExpiration, tags);
      }
      catch (Exception ex)
      {
           Debug.WriteLine(ex.Message);
      }
answered on Stack Overflow Oct 8, 2019 by Razvan Emil
1

I had the same issue. It was because of few white space inside the connectionString url. Removed and issue solved!!!

answered on Stack Overflow Jul 23, 2020 by Vipin Krishna

User contributions licensed under CC BY-SA 3.0