首页 > 其他分享 >【Azure Notification Hub】如何手动删除 Notification Hub 中已注册的设备

【Azure Notification Hub】如何手动删除 Notification Hub 中已注册的设备

时间:2024-02-20 21:11:09浏览次数:26  
标签:Hub Windows Notification var UI registration Azure using notification

问题描述

在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

相关文章

  • 【转】在 github 添加一个 SSH key
    参考:https://zhuanlan.zhihu.com/p/345846941、登录到 https://github.com,在设置页面的左侧菜单里找到SSHandGPGkeys2、打开GitBash3、运行 ssh-keygen-trsa-b4096-C"你的邮箱",注意要填写与github.com里相同的邮箱,不需要接收验证码啥的4、按回车三次,就成......
  • 酒香还怕巷子深?如何打造一个优秀的GitHub开源项目
    GitHub现在已经成了日常开发中必不可少的网站,日常工作和学习中要用到好多上面的开源项目,评价项目质量好坏的一个重要标准就是看Star和Fork的数量,如果看到个Star超过100以上的,基本上这个项目是靠谱的,如果超过1000过,那已经算是很流行了,至于一万以上的,基本上都是如雷贯耳的存在了。......
  • github raw文件cdn加速
    ref:https://blog.csdn.net/neve_give_up_dan/article/details/104817638可以使用该CDN网站进行加速:https://www.jsdelivr.com/官方示例:LoadanyGitHubrelease,commit,orbranch:/gh/user/repo@version/fileLoadexactversion:/gh/jquery/[email protected]/dist/jquery.m......
  • GitHub 热搜项目--电视直播软件:my-tv
    1.GitHub热搜项目1.1开箱即用的电视直播软件:my-tv主语言:C,Star:10k,周增长:6.9k这是一款开源、免费、无广告、不用注册的电视直播软件,适用于Android5及以上的手机和电视盒子。它安装即用、启动快,没有花里胡哨的UI和弹框,内置中央台、地方台等优质直播源,画质高清、播放流畅,......
  • 初始化本地已有项目到github
    1、在github新建一个仓库;2、在本地项目根目录执行gitinit命令;gitinit3、执行gitremoteaddorigin关联github仓库;gitremoteaddorigin NieQimingG/detectory:mywaytogo(github.com)4、从远程分支拉去main分支并于本地main分支合并;gitpulloriginmai......
  • 开年!5 款令人惊艳的开源项目「GitHub 热点速览」
    朋友们开工大吉啊!我刚从假期模式切换回来,完全无心工作有些不在状态,比如开机密码错了好几次......
  • 【Azure Function App】在VS Code中,创建好Function App后部署到Azure中,无法选择Subscr
    问题描述在VSCode中,创建好FunctionApp后部署到Azure中,无法选择Subscriptions问题解答对于无法使用VSCode 部署FunctionApp 到Azure,最近有一个更新,导致了AzureResource 插件的 v0.8.0 版本不支持中国区登录目前的解决办法是:通过手动安装的方式把VSCode中的Azu......
  • 【Azure Webjob + Redis】WebJob一直链接Azure Redis一直报错 Timeout Exception
    问题描述运行在AppService上的Webjob连接AzureRedis出现TimeoutException。 错误截图: 参考AzureRedis对于超时问题的排查建议,在修改MinThread后,问题依旧。流量突增和线程池配置流量激增时,如果 ThreadPool  设置不佳,则可能导致对Redis服务器已发送但尚未在客......
  • vulnhub-DC-6
    ......
  • IDEA 2024.1:Spring支持增强、GitHub Action支持增强、更新HTTP Client等
    有段时间没有更新IDEA了,早上看到IntelliJIDEA2024.1EAP5发布的邮件提示,瞄了一眼,发现真的是越来越强了,其中不少功能对我来说还是非常有用的。也许这些能力对关注DD的小伙伴也有帮助,所以搞篇博客介绍和推荐一下。Spring、Quarkus等主流框架的支持增强SearchEverywhere功能......