How to send message between Firebase Cloud Messaging and Azure Notification Hub?
Hi, I have a difficulty when try to send the notification to Notification Hubs.
My Android App is already connected to Firebase and will get the new token from Firebase (FCM Token). I registered the Token to Notification Hubs successfully and tried the send test message in Azure Notification Hub Portal. The app can successfully pop up the message.
Next step, I trying to send the message to ANH by Firebase Token:
private async void SendToUserByToken(string? token)
{
try
{
string hubName = GlobalClient.HubName;
string connectionString = GlobalClient.AzureHubConnectString;
var hubClient = NotificationHubClient.CreateClientFromConnectionString(connectionString, hubName);
if (string.IsNullOrEmpty(token)) throw new Exception("Token is null or empty.");
string fcmPayload = $@"{{ ""message"" : {{ ""notification"" : {{ ""title"" : ""PushDemo"", ""body"" : ""System maintenance tonight at 11 PM.""}}, ""data"" : {{""action"" : ""open_message""}}}}}}";
var outcome = await hubClient.SendNotificationAsync(
new FcmV1Notification(fcmPayload),
token
);
GlobalApp.Log($"SendToUserByToken Success: {outcome.State}");
}
catch (Exception ex)
{
GlobalApp.Log($"SendToUserByToken Fail: {ex.Message}");
}
}
There is an error : 401 Unauthorized when triggering the SendNotificationAsync()
There are some setting:
- I have already set up Google (FCM V1) in ANH
- The FCM set up is fine as I can register token and trigger test message in Portal
- I do not set up Google (GCM/FCM) as FCM do not support Legency Key anymore
- The ANH connecting string is a Full Access connecting string
-
Praneeth Maddali 10,460 Reputation points β’ Microsoft External Staff β’ Moderator
Hi @Robot GA
The 401 Unauthorized youβre seeing when calling SendNotificationAsync with FcmV1Notification is almost always caused by the OAuth token that Azure Notification Hubs generates on your behalf being rejected by Google. The test-send from the portal works because it still uses a slightly different code path, but programmatic FCM v1 sends are very strict about the service-account credentials.
Could you please follow below steps:
- Recreate the service account key and update it in the hub
- Go to the Firebase Console, then Project settings, and Service accounts to generate a new private key (JSON).
- Copy the updated Project ID, Private Key, and Client Email. Ensure the private key includes the full -----BEGIN PRIVATE KEY----- and -----END PRIVATE KEY----- lines, along with the \n at the end.
- In the Azure portal, navigate to your Notification Hub, select Settings, then Google (FCM v1). Remove the old entries, paste in the new ones, and save your changes.
- Allow 5β10 minutes for changes to take effect, as Azure may cache the JWT temporarily.
- Please verify that the Firebase Cloud Messaging API (v1) is enabled
This API is separate from the legacy version and needs to be activated for the JWT flow to function properly. - Your code is correct and does not require any modifications. After the credentials are refreshed, the same call will proceed successfully, resulting in outcome.State = Enqueued.
Reference :
- Recreate the service account key and update it in the hub
Sign in to comment
1 answer
-
AI answer
It seems you are encountering a
401 Unauthorizederror when trying to send notifications from Azure Notification Hubs (ANH) using Firebase Cloud Messaging (FCM). Here are some steps to troubleshoot and ensure everything is set up correctly:- Check FCM Server Key: Make sure that the server key you are using in your Azure Notification Hub configuration is correct and matches the one from your Firebase project. This key is essential for authenticating requests to FCM.
- Verify Token Registration: Ensure that the FCM token you are using is correctly registered with the Notification Hub. You can check this in the Azure portal to confirm that the token is listed under the registered devices.
- Use FCM V1 API: Since you mentioned that you have set up Google (FCM V1) in ANH, ensure that your payload is correctly formatted for FCM V1. The payload you provided seems to be in the correct format, but double-check for any syntax issues.
- Check Permissions: Ensure that the service account associated with your FCM project has the necessary permissions to send messages. You might need to check the IAM settings in the Google Cloud Console.
- Test with a Simple Payload: Sometimes, complex payloads can cause issues. Try sending a simpler notification payload to see if that works:
{"message": {"notification": {"title": "Test Notification", "body": "This is a test."}}} - Review Logs: Check the logs in both Azure and Firebase to see if there are any additional error messages that could provide more context on the
401 Unauthorizederror.
If after checking these points you still face issues, consider reaching out to Azure support for more detailed assistance.
References:
AI-generated content may be incorrect. Read our transparency notes for more information.
