0
votes

I created Azure Notification Hub and added the server key from Firebase to the section "GCM/FCM" in notification hub. After which, I used the shared access key and notification hub name to create the installation (following the Azure documentation). Here's the code that I used:

[FunctionName("TestFunction")]
public static async Task<IActionResult> RunAsync(
[HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]
        HttpRequest req, ILogger log)
    {
        var deviceUpdate = new DeviceInstallation()
        {
            installationId = "<myInstallationid>",
            pushChannel = "<DeviceTokenGeneratedByFirebase>",
            platform = "fcm",
            tags = new string[] {"notificationhubtag1"},
        };
        
        var responseMessage = new HttpResponseMessage();

        try
        {
            responseMessage = await Put(deviceUpdate);
        }
        catch
        {
            log.LogInformation("exception occured");
        }
        
        return new OkObjectResult(responseMessage);
    }
    
    // Custom API
    public static async Task<HttpResponseMessage> Put(DeviceInstallation deviceUpdate)
    {
        NotificationHubClient hub = new NotificationHubClient(fullAccessConnString, hubName);
        
        Installation installation = new Installation();
        installation.InstallationId = deviceUpdate.installationId;
        installation.PushChannel = deviceUpdate.pushChannel;
        installation.Tags = deviceUpdate.tags;

        switch (deviceUpdate.platform)
        {
            case "mpns":
                installation.Platform = NotificationPlatform.Mpns;
                break;
            case "wns":
                installation.Platform = NotificationPlatform.Wns;
                break;
            case "apns":
                installation.Platform = NotificationPlatform.Apns;
                break;
            case "fcm":
                installation.Platform = NotificationPlatform.Fcm;
                break;
            default:
                throw new HttpResponseException(HttpStatusCode.BadRequest);
        }
        
        await hub.CreateOrUpdateInstallationAsync(installation);
        
        return new HttpResponseMessage(HttpStatusCode.OK);

    }

    public class DeviceInstallation
    {
        public string installationId { get; set; }
        public string platform { get; set; }
        public string pushChannel { get; set; }
        public string[] tags { get; set; }
        
    }

When I run this code, I get a successful message back. The Azure portal itself does not show much information about the no. of devices registered or active devices information in the Azure notification hub. However, I was able to confirm that the installation exists by making a GET request to the installation API directly through this call:

https://<myNotificationHubNameSpace>.servicebus.windows.net/<myHubName>/installations/<myInstallationId>/?api-version=2015-01

This call returned me a 200 OK with the installation I created from the earlier step. The response looked like this:

{"installationId":"<myInstallationId>","pushChannel":"<DeviceTokenGeneratedByFireBase>","pushChannelExpired":false,"platform":"gcm","expirationTime":"9999-12-31T23:59:59.9999999Z","tags":["notificationhubtag1"]}

So, I went to the Azure Notification Hub and sent a test message from the "Test Send" tab and used the tag "notificationhubtag1" in the SendTo tags field. I got back the successful message that said "The Notification was successfully sent to the Push Notification System" and also got the Registration number.

However, I don't see any notifications being sent to the app itself. How can I debug more information about this specific message being pushed. Where did it get pushed?

Is there any way to find more information on the pushed messages, installed devices etc on the notification hub itself? I found an old post about checking logs which said to switch to standard tier instead of free Tier for more information. I have made the switch to standard tier but I don't see any difference in the way overview or activity logs is displayed to me between Free or Standard Tier.

1

1 Answers

0
votes

Notification Hubs acts as a bit of a proxy. So the message you saw in the Portal when performing the test send means it did successfully hand off the notification to FCM to be sent and got a success response back from FCM. This also means the device token was valid.

At that point there is no additional data Notification Hubs can provide since it is no longer in the system.

When we've seen issues like this in the past it tends to imply there is something not quite right in the app configuration itself. Likely there is some logic missing for handling the notification intent.

From the code snippet above, it looks like you are creating the installation from some server side piece and not from the application. My assumption is this means you are still working on integrating the Android SDK for Notification Hubs, since the normal flow is to have devices register themselves with the Hub.

We document the full end-to-end steps here: https://docs.microsoft.com/en-us/azure/notification-hubs/notification-hubs-android-push-notification-google-fcm-get-started

I would mostly recommend looking closely at the various code changes necessary on the Android side. Manifest updates and code changes. If you continue to have issues receiving notifications, please feel free to open a support request in the Portal and we can dig deeper into what's going on.