Azure Notification Hubs Xamarin Forms Start does nothing

Ben Scott 1 Reputation point
2021-12-06T03:45:39.13+00:00

My life story:
We have been using Azure Notification Hubs since December last year, and are finally updating to the newest version. I started with trying to get the sample (https://github.com/Azure/azure-notificationhubs-xamarin) to work. As usual, the path for it was too long so it couldn't build and gave indecipherable error messages. I remembered the issue from last time, so I put it at my drive's root directory. It failed to build because it can't handle referencing the source code directly. So I deleted the nuget.config file, and made each project reference the Azure.NotificationHub.Client.Xamarin nuget package instead. Now I find that it can't build, google-services.json is referenced but not existing. So I go to firebase, add this test app, download the google-services.json, copy it in. The app builds no problem, deploys fine to two different phones. Neither ever call the OnInstallationSaved or OnInstallationSaveFailed methods.
So I decide to go back to the documentation for comparison. Interestingly, it says that for xamarin forms there shouldn't be any platform specific code required - only code in the main project. This seems to contradict needing a google-services.json, and the android and ios projects having the Azure.NotificationHub.Xamarin.iOS and Android nuget packages, which are apparently only necessary for Xamarin.Android and Xamarin.iOS.
This makes me decide that the sample project must be out of date or incorrect, so I decide to just try following the documentation in our existing app.

The actual problem:
I've removed the old nuget packages and added the new ones, removed all old references, and added the recommended code to my app.xaml.cs as follows:

... (App constructor code)
        var env = Resolver.Resolve<IServerEnvironmentService>().GetEnvironment();
        NotificationHub.NotificationMessageReceived += OnNotificationMessageReceived;
        NotificationHub.InstallationSaved += OnInstallatedSaved;
        NotificationHub.InstallationSaveFailed += OnInstallationSaveFailed;
        NotificationHub.Start(env.AzurePnListenConnectionString, env.AzurePnName);
        NotificationHub.AddTag("test");
        var template = new InstallationTemplate
        {
            Body = AzureConstants.FCMTemplateBody,
            Tags = new List<string> { "test" }
        };
        NotificationHub.SetTemplate("defaultTemplate", template);
        NotificationHub.UserId = "test";
    }

    private void OnNotificationMessageReceived(object sender, NotificationMessageReceivedEventArgs e)
    {
        LogService.Instance.Information($"Title: {e.Title}");
        LogService.Instance.Information($"Body: {e.Body}");
    }

    private void OnInstallatedSaved(object sender, InstallationSavedEventArgs e)
    {
        LogService.Instance.Information($"Installation ID: {e.Installation.InstallationId} saved successfully");
    }

    private void OnInstallationSaveFailed(object sender, InstallationSaveFailedEventArgs e)
    {
        LogService.Instance.Warning($"Failed to save installation: {e.Exception.Message}");
        NotificationHub.SaveInstallation();
    }

The app launches fine, calls the constructor code added above, but never calls any of the above private methods. Azure Test Send says no device is registered with the tag "test".
Could anyone direct me to some full and correct documentation, or a working sample, for the most recent version of azure notification hubs for xamarin forms?

Azure Notification Hubs
Azure Notification Hubs
An Azure service that is used to send push notifications to all major platforms from the cloud or on-premises environments.
262 questions
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,293 questions
{count} votes