Android 上的 Azure 通知中心。如何发送给特定用户?
Azure Notification Hub on Android. How to send to specific user?
我想创建一个小应用程序来向我的 Android 设备发送通知。
我制作了一个 WPF 应用程序和一个 Android 应用程序,并使用 Azure 通知中心和 GCM 进行通知。
我可以为所有 android 设备发送通知,但不能为一台设备发送通知。
我来这里是为了得到你的帮助。
我的 WPF 应用程序是这样工作的:
private async void SendNotificationAsync()
{
var hub = NotificationHubClient.CreateClientFromConnectionString(Keys.FullConnectionString, Keys.NotificationHubName);
if (!string.IsNullOrEmpty(TxtTo.Text))
await hub.SendGcmNativeNotificationAsync(
"{'to': '" + TxtTo.Text + "', 'data': { 'message': '" + TxtMessage.Text + "', 'title': '" + TxtTitle.Text + "' } }");
else
{
await hub.SendGcmNativeNotificationAsync(
"{'data': { 'message': '" + TxtMessage.Text + "', 'title': '" + TxtTitle.Text + "' } }");
}
}
我的问题是:
我不明白如何获取 Android ID 设备注册,或者,这是我的 RegistrationId 使用我的 OnRegistered 方法生成的:
protected override void OnRegistered(Context context, string registrationId)
{
Log.Verbose(MyBroadcastReceiver.TAG, "GCM Registered: " + registrationId);
RegistrationId = registrationId;
ChangeTextID(registrationId);
Hub = new NotificationHub(Keys.NotificationHubName, Keys.ListenConnectionString, context);
try
{
Hub.UnregisterAll(registrationId);
}
catch (Exception ex)
{
Log.Error(MyBroadcastReceiver.TAG, ex.Message);
}
var tags = new List<string>();
try
{
Hub.Register(registrationId, tags.ToArray());
}
catch (Exception ex)
{
Log.Error(MyBroadcastReceiver.TAG, ex.Message);
}
}
但是如果我使用这个 ID 发送我的通知(在我的 WPF 应用程序中)它不起作用(替换 TxtTo.Text)
你能帮帮我吗?
除了您订阅该应用程序的标签外,您不需要使用任何东西来发送有针对性的推送通知。
当您为通知中心注册应用程序时,它会将其记录放入通知中心后端注册 table(您可以从 Azure 门户访问或使用 Service Bus Explorer。如果您将应用程序订阅到标记,并将通知发送到该标记,但应用程序未收到它,然后查看您是否在 table 中使用适当的标记来查看该应用程序。
这是一个很棒的功能,因为您可以在不知道用户 ID 或其他信息的情况下向用户发送有针对性的通知。 Just subscribe them 到那个标签。这在平台之间非常有效——我们使用这种方法为在不同设备和平台上订阅相同标签的用户发送有针对性的通知。
relevant. As i see, you send your notification not to a tag (try to add to SendGcmNativeNotificationAsync extra argument like here)。尝试将其发送到确切的标签(您可以从门户、通知中心仪表板 => 调试窗格执行此操作)。
我想创建一个小应用程序来向我的 Android 设备发送通知。
我制作了一个 WPF 应用程序和一个 Android 应用程序,并使用 Azure 通知中心和 GCM 进行通知。
我可以为所有 android 设备发送通知,但不能为一台设备发送通知。
我来这里是为了得到你的帮助。
我的 WPF 应用程序是这样工作的:
private async void SendNotificationAsync()
{
var hub = NotificationHubClient.CreateClientFromConnectionString(Keys.FullConnectionString, Keys.NotificationHubName);
if (!string.IsNullOrEmpty(TxtTo.Text))
await hub.SendGcmNativeNotificationAsync(
"{'to': '" + TxtTo.Text + "', 'data': { 'message': '" + TxtMessage.Text + "', 'title': '" + TxtTitle.Text + "' } }");
else
{
await hub.SendGcmNativeNotificationAsync(
"{'data': { 'message': '" + TxtMessage.Text + "', 'title': '" + TxtTitle.Text + "' } }");
}
}
我的问题是:
我不明白如何获取 Android ID 设备注册,或者,这是我的 RegistrationId 使用我的 OnRegistered 方法生成的:
protected override void OnRegistered(Context context, string registrationId)
{
Log.Verbose(MyBroadcastReceiver.TAG, "GCM Registered: " + registrationId);
RegistrationId = registrationId;
ChangeTextID(registrationId);
Hub = new NotificationHub(Keys.NotificationHubName, Keys.ListenConnectionString, context);
try
{
Hub.UnregisterAll(registrationId);
}
catch (Exception ex)
{
Log.Error(MyBroadcastReceiver.TAG, ex.Message);
}
var tags = new List<string>();
try
{
Hub.Register(registrationId, tags.ToArray());
}
catch (Exception ex)
{
Log.Error(MyBroadcastReceiver.TAG, ex.Message);
}
}
但是如果我使用这个 ID 发送我的通知(在我的 WPF 应用程序中)它不起作用(替换 TxtTo.Text)
你能帮帮我吗?
除了您订阅该应用程序的标签外,您不需要使用任何东西来发送有针对性的推送通知。 当您为通知中心注册应用程序时,它会将其记录放入通知中心后端注册 table(您可以从 Azure 门户访问或使用 Service Bus Explorer。如果您将应用程序订阅到标记,并将通知发送到该标记,但应用程序未收到它,然后查看您是否在 table 中使用适当的标记来查看该应用程序。
这是一个很棒的功能,因为您可以在不知道用户 ID 或其他信息的情况下向用户发送有针对性的通知。 Just subscribe them 到那个标签。这在平台之间非常有效——我们使用这种方法为在不同设备和平台上订阅相同标签的用户发送有针对性的通知。