Ewart MacLucas
Ewart MacLucas

Reputation: 51

Can MAUI .NET 8 work with the new FCM v1 on Azure Notification Hubs?

I'm trying to get push notifications working on .NET 8 MAUI and I was thinking about using Azure Notification Hubs to make it easier to implement for both Android and iOS.

I found a few questions, videos and articles on how to this (some example links below), however all of them refer to using the Legacy Cloud Messaging API and none are using the Firebase Cloud Messaging API (V1). We need to use V1 because the legacy version will stop working on 20-June-2024.

Microsoft's FAQ states

How can Xamarin customers migrate to FCM v1? Xamarin is now deprecated. Xamarin customers should migrate to MAUI, but MAUI is not currently supported by Azure Notification Hubs. You can, however, use the available SDKs and REST APIs with your MAUI applications. It's recommended that Xamarin customers move away from Notification Hubs if they need FCM v1 sends.

Which basically seems to suggest that if your using MAUI, don't use Notification Hubs.

I had a crack at it anyway because notification hubs does have an area for entering your FCM V1 API details but when I save the API details I get an error:

{"error":{"code":"BadRequest","message":"Invalid Fcm V1 credential. InvalidDataContractException: Cannot generate bearer token. Error message: PKCS8 data must be contained within '-----BEGIN PRIVATE KEY-----' and '-----END PRIVATE KEY-----'. (Parameter

The documentation states to get the email from the JSON file but the JSON file doesn't contain an email, but I've tried a variety of emails that relate and none work. Its hard to fathom that all the MAUI apps notifications would stop working on 20-June because MS didn't support this, and they have added a FCM v1 area inside azure notification hubs, so surely there must be a way of making this work? but then again why would they say move away from notification hubs!

  1. Has anyone successfully made notification hubs work with google FCM V1 on MAUI and if so, what is the trick?

  2. have i misunderstood the FAQ to say move away from notification hubs, why would they say that?

Here are the links I found that relate to the old version.

vladislavantonyuk -

https://puresourcecode.com/dotnet/maui/maui-push-notifications-using-azure-notification-hub-for-android/

https://stackoverflow.com/questions/78029274/net-8-maui-push-notification-what-to-do

Happy to put some cash on the line for a good answer to this!!

Upvotes: 3

Views: 1386

Answers (1)

Michal Dobrodenka
Michal Dobrodenka

Reputation: 1134

I did it yesterday. Ms could improve migration for .NET users. I waited till the end in hope they will get us straightforward migration tutorial or examples. Technically, I don't have a MAUI app, only net8.0-android app, but MAUI is just net8.0-android under the hood.

But to your points, entering credentials azure was straightforward (has nothing to do with MAUI or any other app technology), according to this link

https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-rest#create-google-service-account-json-file

enter image description here

Then the problem for me was the client library - I was using Xamarin.Azure.NotificationHubs.Android (it's android native library wrapped to work in Xamarin, which happens to work in net-android projects), but it doesn't have a successor for FCMv1. I was trying to create bindings to notification-hubs-android-sdk-fcm ( https://central.sonatype.com/artifact/com.microsoft.azure/notification-hubs-android-sdk-fcm/overview ) but I had problem with some event naming and unimplemented abstract class method, I gave up. (for iOS we did create bindings when we were migrating from Xamarin, because iOS can't use old xamarin libraries like android can). Another option would be calling Rest API directly, option which I wanted to avoid.

Trick was using Microsoft.Azure.NotificationHubs nuget, library I use in server backend, it's written in c# (as oposed to java android library with c# bindings). This library has class NotificationHubClient with some different methods to Xamarin.Azure.NotificationHubs.Android but migration wasn't that hard:

var hub = new NotificationHub(hubName, listenConnectionString, context); -> var hub = new NotificationHubClient(listenConnectionString, hubName); - !!! WARNING !!! be careful - notice changed order of conn string & hub name order in ctor! (both are string parameters, compiler won't help you with this one)

hub.UnregisterAll(token); -> hub.DeleteRegistrationsByChannelAsync(token).Wait();

hub.RegisterTemplate() -> hub.CreateFcmV1TemplateRegistrationAsync()

Since I use notification templates, I had to migrate them, since FCMv1 has slightly different format. Google has made quite a nice migration guide how to do it: https://firebase.google.com/docs/cloud-messaging/migrate-v1

My first attempts to register template in Azure NH were returned with error: Failed to parse BodyTemplate: Invalid template payload., so ANH is parsing and checking the notification payload when registering, before sending the notification.

To be honest I don't see a problem why even old Xamarin app wouldn't work with FCMv1 this way.

Upvotes: 0

Related Questions