Azure Notification Hubs Push Notification Id is blank

Chris Job 1 Reputation point
2022-04-05T14:24:45.537+00:00

I am going thru Microsoft's documentation for sending a push notification. I have the notificationhub up and running, a basic android app. I can successful send a message via the portal to the app.
I am trying to send a message to the app via the code below but notification id is
always blank

Snippet of my code

namespace PushToApp
{
class Program
{

    static async Task Main()
    {
        var config = new ConfigurationBuilder()
            .SetBasePath(Directory.GetCurrentDirectory())
            .AddJsonFile("config.json", optional: false)
            .Build();

        var nhClient = NotificationHubClient.CreateClientFromConnectionString(config["PrimaryConnectionString"], config["HubName"], true);

        var message = "This a test";

        var fcmDeviceId = Guid.NewGuid().ToString();

        var fcmInstallation = new Installation
        {
            InstallationId = "fake-fcm-install-id",
            Platform = NotificationPlatform.Fcm,
            PushChannel = fcmDeviceId, 
            PushChannelExpired = false,
            Tags = new[] { "fcm" }
        };

        await nhClient.CreateOrUpdateInstallationAsync(fcmInstallation);
        await SendNotificationAsync(nhClient, message);
    }


    private static async Task SendNotificationAsync(NotificationHubClient nhClient, string message)
    {

        var alert = "{\"aps\":{\"alert\":\"" + message + "\",\"sound\":\"default\"}}";

        var outcomeFcm = await nhClient.SendFcmNativeNotificationAsync(alert);

        Console.WriteLine("result: " + outcomeFcm.Results.Count());
        Console.WriteLine("state: " + outcomeFcm.State);
        Console.WriteLine("Tracking Id: " + outcomeFcm.TrackingId);
        Console.WriteLine("notification Id: " + outcomeFcm.NotificationId);

    }
 } //class

} //namespace

`

Output:
Count: 3
result:1
state:DetailedStateAvailable
Tracking Id:a8b19b49-9857-401f-a1b1-af7dcb896e8b
notification Id:

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
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Panagiotis Tzedakis 22 Reputation points
    2022-04-06T18:28:19.157+00:00

    Hi ChrisJob-0463,
    First of all, I think you use the wrong payload for sending a push notification to Android. The json payload you used in code line 31 is for Apple Push Notification service (APNs), not FCM.

    I suggest you use the following payload and see what happens:

    {
    "notification":{
    "title":"Notification Hub Test Notification",
    "body":"This is a sample notification delivered by Azure Notification Hubs."
    },
    "data":{
    "property1":"value1",
    "property2":42
    }
    }

    For more info: https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support

    Hope it helps.
    P.