无法将 Azure NotificationHub 推送消息从 .NET 控制台应用程序传送到 (Android) 设备
Failing to deliver Azure NotificationHub push messages to (Android) devices from .NET console app
我已经使用较新的 Azure 门户中的 "Quick Start" 说明配置了 .NET 应用服务,以发布应用服务以用作我的移动后端。我还为其配置了一个通知中心,并且能够通过 Azure 移动后端端点从 Xamarin.Forms Android 应用程序发送推送通知。我也可以在已注册的 Android 设备上接收推送通知。
我需要能够从 C# 控制台应用程序向已注册的设备发送推送通知,但无论我尝试什么,推送消息都没有送达。
我正在使用以下代码尝试发送推送消息。当我 运行 它时,执行结束于...
var result = await hub.SendTemplateNotificationAsync(templateParams);
...并且控制台应用程序在不等待响应的情况下终止,并且推送通知永远不会传送到已注册的设备。
public async void PushSmartAlerts()
{
string notificationHubName = "DevXXXXNotificationHub";
string notificationHubConnection = "Endpoint=sb://devXXXXXnotificationhubnamespace.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=SnXXXXXXXXXXXXXXXXXXXXXXXX9N8=";
hub = NotificationHubClient .CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
Dictionary<string, string> templateParams = new Dictionary<string, string>();
templateParams["messageParam"] = "Hello";
var result = await hub.SendTemplateNotificationAsync(templateParams);
}
谁能告诉我哪里出了问题,或者我是不是用错了方法?
解决了。我使用 NotificationHubClient 创建和发送通知的方式没有任何问题。它不起作用的原因是控制台应用程序不支持 Main 方法中的 await
,因此在发送推送通知之前执行就结束了。通过将代码更改为以下内容,推送通知已发送并送达。
string notificationHubName = "xxxxxxx";
string notificationHubConnection = "Endpoint=sb://xxxxxx";
hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName, true);
Dictionary<string, string> notification = new Dictionary<string, string>()
{
{"messageParam", "It works. Tap to view."}
};
var task = hub.SendTemplateNotificationAsync(notification);
task.Wait();
我已经使用较新的 Azure 门户中的 "Quick Start" 说明配置了 .NET 应用服务,以发布应用服务以用作我的移动后端。我还为其配置了一个通知中心,并且能够通过 Azure 移动后端端点从 Xamarin.Forms Android 应用程序发送推送通知。我也可以在已注册的 Android 设备上接收推送通知。
我需要能够从 C# 控制台应用程序向已注册的设备发送推送通知,但无论我尝试什么,推送消息都没有送达。
我正在使用以下代码尝试发送推送消息。当我 运行 它时,执行结束于...
var result = await hub.SendTemplateNotificationAsync(templateParams);
...并且控制台应用程序在不等待响应的情况下终止,并且推送通知永远不会传送到已注册的设备。
public async void PushSmartAlerts()
{
string notificationHubName = "DevXXXXNotificationHub";
string notificationHubConnection = "Endpoint=sb://devXXXXXnotificationhubnamespace.servicebus.windows.net/;SharedAccessKeyName=DefaultFullSharedAccessSignature;SharedAccessKey=SnXXXXXXXXXXXXXXXXXXXXXXXX9N8=";
hub = NotificationHubClient .CreateClientFromConnectionString(notificationHubConnection, notificationHubName);
Dictionary<string, string> templateParams = new Dictionary<string, string>();
templateParams["messageParam"] = "Hello";
var result = await hub.SendTemplateNotificationAsync(templateParams);
}
谁能告诉我哪里出了问题,或者我是不是用错了方法?
解决了。我使用 NotificationHubClient 创建和发送通知的方式没有任何问题。它不起作用的原因是控制台应用程序不支持 Main 方法中的 await
,因此在发送推送通知之前执行就结束了。通过将代码更改为以下内容,推送通知已发送并送达。
string notificationHubName = "xxxxxxx";
string notificationHubConnection = "Endpoint=sb://xxxxxx";
hub = NotificationHubClient.CreateClientFromConnectionString(notificationHubConnection, notificationHubName, true);
Dictionary<string, string> notification = new Dictionary<string, string>()
{
{"messageParam", "It works. Tap to view."}
};
var task = hub.SendTemplateNotificationAsync(notification);
task.Wait();