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.