lcj
lcj

Reputation: 1797

(400) BadRequest using Azure Notification Hub from an Azure Function when Test page is working

I have Azure Notifications Hubs set up on iOS device and it is receiving messages from the Test Send page in Azure Notification Hubs, but I cannot seem to get an Azure Function Timer to send notifications using the backend adapted code examples for specific devices and specific users.

I keep getting this error:

Error: The remote server returned an error: (400) BadRequest. Reason: Bad Request..TrackingId:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx,TimeStamp:2024-03-26T10:35:00.5448540Z

The code is:

    private async Task sendMessageAsync() {

        _logger.LogInformation("SendingMessage...");

        
        NotificationHubClient hub = NotificationHubClient
                        .CreateClientFromConnectionString("Endpoint=<endpoint string>"
                        , "<hub name>");

        try {

            var msg = "\"{\"aps\":{\"alert\":\"Notification Hub test notification\"}}";
            await hub.SendAppleNativeNotificationAsync(msg);


        } catch (Exception e) {
            Console.WriteLine("Error: " + e.Message);
        }

    }

I copied the DefaultFullSharedAccessSignature from the Notification Hub and am using the right Notification Hub name. The test message itself is taken from the Notification Hub test page. I am sending from Azure Functions running on my MacOS using Visual Studio, but don't think that matters.

I am using this in the .csproj:

<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />

This post, Getting Bad Request (400) on Azure Notification Hub's "Test Send", didn't apply as the test send is working. this post, NotificationHub BadRequest didn't have a resolution. This post, Azure Apple Push Notification Error: 400 Bad Request, the test was not working. This post, azure-notificationhubs IOS Bad request, the test was not working.

These posts seem to use the same method:

UPDATE

I thought I might try a console app, to avoid all the local function complexity in case that was somehow the problem, but still received the 400 error.

using Microsoft.Azure.NotificationHubs;
using System.Threading.Tasks;


namespace NotificationConsole;

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Hello, World!");
        SendNotificationAsync().Wait();
    }

    static async Task SendNotificationAsync()
    {

        NotificationHubClient hub = NotificationHubClient
                    .CreateClientFromConnectionString("<endpoint connection string"
                    , "<hub name>");

        NotificationOutcome outcome = null;

        try {

            var msg = "\"{\"aps\":{\"alert\":\"Notification Hub test notification\"}}";
            outcome = await hub.SendAppleNativeNotificationAsync(msg,"");

        } catch (Exception e) {
            Console.WriteLine("Error: " + e.Message);
            Console.WriteLine(e.StackTrace);
        }
    }
}

I followed this post, Push notification does not send from console using Azure, and the article in the answer, Sending Notification From A Console Application Using Notification Hubs, which was a Windows push, but I just replaced the call and still it didn't work.

Upvotes: 0

Views: 294

Answers (1)

lcj
lcj

Reputation: 1797

It was a payload error, as the comments in this post state. So I used the payload from this article:

    var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}";
    outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);
    break;

and it worked.

Upvotes: 0

Related Questions