我在 iOS 设备上设置了 Azure 通知中心,并且它正在从 Azure 通知中心的“测试发送”页面接收消息,但我似乎无法让 Azure 函数计时器使用针对特定设备的后端改编代码示例来发送通知和特定用户。 我不断收到此错误:
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
代码是:
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);
}
}
我从通知中心复制了
DefaultFullSharedAccessSignature
并使用正确的通知中心名称。测试消息本身取自通知中心测试页面。我使用 Visual Studio 从 MacOS 上运行的 Azure Functions 发送数据,但我认为这并不重要。
我在 .csproj 中使用它:<PackageReference Include="Microsoft.Azure.NotificationHubs" Version="4.2.0" />
这篇文章在 Azure 通知中心的“测试发送”上收到错误请求 (400)
,不适用,因为测试发送正在运行。这篇文章,NotificationHub BadRequest 没有解决方案。这篇文章,Azure Apple 推送通知错误:400 错误请求,测试不起作用。这篇文章,azure-notificationhubs IOS Bad request,测试不起作用。 这些帖子似乎使用相同的方法:
更新
我想我可以尝试一个控制台应用程序,以避免所有本地函数的复杂性,以防万一出现问题,但仍然收到 400 错误。
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);
}
}
}
我关注了这篇文章,推送通知不会使用 Azure 从控制台发送
,以及答案中的文章,使用通知中心从控制台应用程序发送通知,这是一个 Windows 推送,但我只是替换了调用和还是没用。
中的评论所述。所以我使用了这篇文章中的有效负载:
var alert = "{\"aps\":{\"alert\":\"" + "From " + user + ": " + message + "\"}}";
outcome = await Notifications.Instance.Hub.SendAppleNativeNotificationAsync(alert, userTag);
break;
并且成功了。