VladislavAntonyuk / vladislavantonyuk.github.io

https://vladislavantonyuk.github.io
2 stars 1 forks source link

.NET 8 MAUI Push Notifications (Android) - Migrating Azure Notification Hub Legacy GCM/FCM protocol over to FCM V1 #109

Closed tommyw1996 closed 4 months ago

tommyw1996 commented 5 months ago

.NET 8 MAUI Push Notifications (Android) using Azure Notification Hub FCM V1 protocol

Following your tutorials I managed to get push notifications working using Legacy GCM/FCM protocol in .NET 8 MAUI (Android).

Unfortunately we now have to migrate this over to use FCM V1 due to Legacy FCM being out of support from June 2024 (!!)

Since applying the migration changes to the Azure Portal to configure the settings for Google (FCM v1), and registering my device using FCMV1 in my .NET MAUI project. I can no longer get the overridden method OnMessageReceived() to trigger anymore, implemented from Firebase.Messaging.FirebaseMessagingService.

.NET MAUI Changes image

Wondering if you have attempted to migrate this at all and if you've had any similar issues?

Any help would be much appreciated! Thanks!

VladislavAntonyuk commented 5 months ago

Probably the best way to check if Microsoft.Azure.NotificationHub is compatible with .NET MAUI. If it does, the registration process will be much simpler.

tommyw1996 commented 5 months ago

Finally managed to get it working. It came down to existing installations in my Notification Hub using GCM that I had to delete. And then updating the code in OnNewToken and DeviceInstallationService to now use Microsoft.Azure.NotificationHubs.NotificationHubClient with FcmV1Installation.

VladislavAntonyuk commented 5 months ago

Awesome, I will update the article with the new information

VladislavAntonyuk commented 4 months ago

@tommyw1996 were you able to save new credentials on this page? image

I generated Json file on this page image

But I get error on save: image

tommyw1996 commented 4 months ago

Hi @VladislavAntonyuk, yes but I did have some issues with this initially. One thing that got me was when you are entering the private key, you need to ensure you have entered the entire key from the firebase-adminsdk JSON file. The private key should include the text "-----BEGIN PRIVATE KEY-----" and "-----END PRIVATE KEY-----". Hope that helps!

VladislavAntonyuk commented 4 months ago

yes, the key also includes '\n' in the middle and the end of the private key. I copied the whole string from the beginning to the end. Did you make any configuration in Firebase? or maybe modified the private key?

tommyw1996 commented 4 months ago

Is your FCM API correctly enabled? image

VladislavAntonyuk commented 4 months ago

yes, I will try to create a new project. I have both New and legacy settings enabled

tommyw1996 commented 4 months ago

Hmm right, definitely seems odd why you're getting an error! I'll try think of anything else I did but I didn't modify anything to get this working. Another thing I would do is regenerate the admin SDK JSON file, check if the key has updated at all.

VladislavAntonyuk commented 4 months ago

the key is changed. The part of generated JSON: image

VladislavAntonyuk commented 4 months ago

Do you also have that \n in your key?

tommyw1996 commented 4 months ago

Yes my private key set in Azure Portal begins with "-----BEGIN PRIVATE KEY-----" and ends with "-----END PRIVATE KEY-----\n"

VladislavAntonyuk commented 4 months ago

Ok, that means it may be related only to permissions.

tommyw1996 commented 4 months ago

My service account also looks like this in Google Cloud Console image

VladislavAntonyuk commented 4 months ago

According to Microsoft nothing special should be required: https://learn.microsoft.com/en-us/azure/notification-hubs/firebase-migration-rest

tommyw1996 commented 4 months ago

If your project has just been set up. It might be one of those where you need to wait a little before it can be configured in Azure. Not 100% sure on this but the amount of times I have been stuck with something in Azure not working and it only required me to wait a day to try again and it works. Just another thought!

VladislavAntonyuk commented 4 months ago

The problem was on Azure side, I've just created a new Hub and was able to save changes: image

VladislavAntonyuk commented 4 months ago

I will update the article today. Thank you @tommyw1996

tommyw1996 commented 4 months ago

Nice! Strange that it wasn't working before but glad it's working now! Let me know if you have any trouble with integrating this with .NET MAUI. I've had FCM v1 working for a good month now with no issues so it's definitely possible. Happy to help.

plettb commented 4 months ago

I'm really struggling to get this working. Does anyone have a repo that could provide a working example? The repo that is referred to in the article (https://github.com/VladislavAntonyuk/WorldExplorer.git) no longer contains notifications. I went back to when it did but can't get it to build. I tried both the commit referenced in the article (8ce130a on 8/13/2023) and the latest I found that contained notifications (550f3152 on 10/11/23).

Using the "DeviceInstallationService.cs" from the article (which is substantially different from anything I actually found in the repo), I get the following errors. Platforms\Android\DeviceInstallationService.cs(10,16,10,37): error CS0103: The name 'GoogleApiAvailability' does not exist in the current context Platforms\Android\DeviceInstallationService.cs(13,16,13,24): error CS0103: The name 'Settings' does not exist in the current context Platforms\Android\DeviceInstallationService.cs(13,79,13,87): error CS0103: The name 'Settings' does not exist in the current context Platforms\Android\DeviceInstallationService.cs(22,33,22,76): error CS1061: 'Task' does not contain a definition for 'GetAwaiter' and no accessible extension method 'GetAwaiter' accepting a first argument of type 'Task' could be found (are you missing a using directive or an assembly reference?) Platforms\Android\DeviceInstallationService.cs(23,36,23,48): error CS0246: The type or namespace name 'Installation' could not be found (are you missing a using directive or an assembly reference?) Platforms\Android\DeviceInstallationService.cs(26,28,26,48): error CS0103: The name 'NotificationPlatform' does not exist in the current context Platforms\Android\DeviceInstallationService.cs(29,26,29,47): error CS0103: The name 'NotificationHubClient' does not exist in the current context I'm obviously missing some NuGets or have incorrect versions, but since I can't get the code from the repo to build either, I can't even seem to track down which packages/versions are the problem.

Any help would be much appreciated, and would definitely warrant a "coffee"! :)

VladislavAntonyuk commented 4 months ago

@plettb I will upload a sample to MauiSamples repo.

plettb commented 4 months ago

FANTASTIC!!! Thank-you VERY MUCH!

plettb commented 4 months ago

I've been struggling with Android notifications off-and-on for a couple of years. Unfortunately I never have enough time to understand it properly, and have never successfully implemented it. I feel like I've gotten close a few times, but this time I'd REALLY like to get it working.

VladislavAntonyuk commented 4 months ago

@plettb pushed changes. MauiNotifications project

plettb commented 4 months ago

Thank-you SO MUCH!!!