问题描述
在Notification Hub中注册了设备后,从Azure门户上没有找到相应的入口来删除已注册设备 (Active Devices)
如果使用C# SDK是否有办法删除呢?
问题解答
可以的,查看Notification Hub的文档,可以通过注册ID来删除一个注册设备:https://docs.azure.cn/zh-cn/notification-hubs/notification-hubs-push-notification-registration-management#example-code-to-register-with-a-notification-hub-from-a-backend-using-a-registration-id
示例代码
MainPage.xaml.cs
using Microsoft.Azure.NotificationHubs; using Microsoft.WindowsAzure.Messaging; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Runtime.InteropServices.WindowsRuntime; using System.Threading.Tasks; using Windows.Foundation; using Windows.Foundation.Collections; using Windows.Networking.PushNotifications; using Windows.UI.Popups; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Controls.Primitives; using Windows.UI.Xaml.Data; using Windows.UI.Xaml.Input; using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409 namespace NoteHubApp { /// <summary> /// An empty page that can be used on its own or navigated to within a Frame. /// </summary> public sealed partial class MainPage : Page { //Connection String & Notification Hub Name const string nhName = "NH Name"; const string managementConnectionString = "NH Connection String"; NotificationHubClient hub = NotificationHubClient.CreateClientFromConnectionString(managementConnectionString, nhName); List<string> registrationIds = new List<string>(); public MainPage() { this.InitializeComponent(); } private void MyButton_Click(object sender, RoutedEventArgs e) { RegisterDevices(); RefreshAllRegistration(); } private async void RegisterDevices() { var channel = await PushNotificationChannelManager.CreatePushNotificationChannelForApplicationAsync(); // create a registration description object of the correct type, e.g. var reg = new WindowsRegistrationDescription(channel.Uri); // Create var result = await hub.CreateRegistrationAsync(reg); // Displays the registration ID so you know it was successful if (result.RegistrationId != null) { registrationIds.Add(result.RegistrationId); var dialog = new MessageDialog("Registration successful: " + result.RegistrationId); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); } } private void Refresh_Click(object sender, RoutedEventArgs e) { RefreshAllRegistration(); } private async void RefreshAllRegistration() { registrationIds.Clear(); var allRegistrations = await hub.GetAllRegistrationsAsync(100); showresult.Text = "Registrations Total is " + allRegistrations.Count().ToString() + "\n"; foreach (var registration in allRegistrations) { registrationIds.Add(registration.RegistrationId); showresult.Text += registration.RegistrationId + "\n"; } //var dialogs = new MessageDialog("Get Registrations Number " + allRegistrations.Count().ToString()); //dialogs.Commands.Add(new UICommand("OK")); //await dialogs.ShowAsync(); } private void Delete_Click(object sender, RoutedEventArgs e) { DeleteDevices(); } private async void DeleteDevices() { if (registrationIds.Count > 0) { var rid = registrationIds[registrationIds.Count - 1]; // Get by ID var r = await hub.GetRegistrationAsync<RegistrationDescription>(rid); try { // delete await hub.DeleteRegistrationAsync(r); registrationIds.Remove(rid); var dialog = new MessageDialog("Delete Registration: " + rid); dialog.Commands.Add(new UICommand("OK")); await dialog.ShowAsync(); } catch { } RefreshAllRegistration(); } } } }
MainPage.xaml
<Page x:Class="NoteHubApp.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:NoteHubApp" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" Height="1027" Width="1623"> <Grid Margin="0,0,66,30"> <Button x:Name="MyButton" Content="Register One!" Click="MyButton_Click" Height="244" Width="243" Margin="48,610,0,0" VerticalAlignment="Top" RenderTransformOrigin="2.758,-2.919" Background="#33228919"/> <Button x:Name="MyButton_Copy" Content="Refresh All Register!" Click="Refresh_Click" Height="809" Width="198" Margin="723,36,0,0" VerticalAlignment="Top" Background="#33D26136"/> <Button x:Name="MyButton_Copy1" Content="Delete Old One!" Click="Delete_Click" Height="238" Width="374" Margin="315,613,0,0" VerticalAlignment="Top" Background="#33172D5D"/> <TextBox x:Name="showresult" HorizontalAlignment="Left" Margin="30,28,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Height="556" Width="664" Background="#667B857A"/> </Grid> </Page>
UI 展示如下:
参考资料
注册管理 (DeleteRegistrationAsync) : https://docs.azure.cn/zh-cn/notification-hubs/notification-hubs-push-notification-registration-management#example-code-to-register-with-a-notification-hub-from-a-backend-using-a-registration-id
标签:Hub,Windows,Notification,var,UI,registration,Azure,using,notification From: https://www.cnblogs.com/lulight/p/18024066