How to send message between Firebase Cloud Messaging and Azure Notification Hub?

Robot GA 0 Reputation points
2025-11-29T01:56:18.3233333+00:00

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:

  1. I have already set up Google (FCM V1) in ANH
  2. The FCM set up is fine as I can register token and trigger test message in Portal
  3. I do not set up Google (GCM/FCM) as FCM do not support Legency Key anymore
  4. The ANH connecting string is a Full Access connecting string
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.
{count} votes

1 answer

Sort by: Most helpful
  1. Q&A Assist
    2025-11-29T01:56:30.4533333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    It seems you are encountering a 401 Unauthorized error 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:

    1. 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.
    2. 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.
    3. 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.
    4. 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.
    5. 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."}}}
      
    6. 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 Unauthorized error.

    If after checking these points you still face issues, consider reaching out to Azure support for more detailed assistance.


    References:

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.