You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Windows Toast通知中基于选择框运行自定义协议?

解决Toast选择框触发自定义协议的问题

你的核心问题在于:静态XML里的protocol类型action无法动态关联选择框的选中值,必须通过监听Toast激活事件来获取用户选择,再执行对应协议。以下是修改方案:

关键修改点

  • 将RUN按钮的activationType从protocol改为foreground,让PowerShell能捕获激活事件并获取输入值
  • 添加Toast激活事件的监听逻辑,根据选中的ID执行对应自定义协议

修改后的完整脚本

$ToastTitle = ""
$Signature = ""
$BannerImage = 'C:\Users\anowak\OneDrive - Phlexglobal Ltd\Desktop\Personal\Scripts\Toast\lake.jpg'
$EventTitle = "Testing Title"
$EventText = "This is where there would normally be info. This is just a test though."
$site = "http://google.com/"
$MoreInfoTitle = "More Information"
$Snooze = $true
$CustomHello = "Hello $env:USERNAME"

# 指定启动器App ID,用于声明Toast通知归属,不影响功能
$LauncherID = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'

# 加载必要程序集
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastActivatedEventArgs, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
 
# 构建Toast XML模板
[xml]$toastTemplate = @"
<toast duration="long" scenario="reminder">
    <visual>
        <binding template="ToastGeneric">
            <text>$CustomHello</text>
            <text>$ToastTitle</text>
            <text placement="attribution">$Signature</text>
            <image placement="hero" src="$BannerImage"/>
            <group>
                <subgroup>
                    <text hint-style="title" hint-wrap="true" >$EventTitle</text>
                </subgroup>
            </group>
            <group>
                <subgroup>
                    <text hint-style="body" hint-wrap="true" >$EventText</text>
                </subgroup>
            </group>
        </binding>
    </visual>
     <actions>
        <input id="MySelection" type="selection" title="Choose:" defaultInput="1">
            <selection id="1" content="Zenek:"/>
            <selection id="2" content="NetExt"/>
            <selection id="3" content="Shorts"/>
            <selection id="4" content="Busy"/>
            <selection id="5" content="Time"/>
        </input>
        <action activationType="foreground" arguments="runAction" hint-inputId="MySelection" content="RUN"/>
        <action arguments="dismiss" content="" activationType="system"/>
    </actions>
</toast>
"@

# 准备Toast XML对象
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::New()
$ToastXml.LoadXml($ToastTemplate.OuterXml)
$ToastMessage = [Windows.UI.Notifications.ToastNotification]::New($ToastXML)

# 添加激活事件监听
$null = Register-ObjectEvent -InputObject $ToastMessage -EventName Activated -Action {
    # 获取用户选择的ID
    $selectedId = $EventArgs.UserInput["MySelection"]
    
    # 根据选择ID执行对应自定义协议,替换为你的实际协议地址
    switch ($selectedId) {
        "1" { Start-Process "zenek://your-custom-protocol" }
        "2" { Start-Process "netext://your-custom-protocol" }
        "3" { Start-Process "shorts://your-custom-protocol" }
        "4" { Start-Process "busy://your-custom-protocol" }
        "5" { Start-Process "time://your-custom-protocol" }
    }
}

# 显示Toast通知
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($LauncherID).Show($ToastMessage)

# 保持脚本运行以监听事件,若需后台运行可自行调整
Read-Host "按回车键退出"

补充说明

  • 需将switch块中的协议地址替换为你实际的自定义协议
  • 若需要后台无窗口处理事件,可将activationType改为background,但需额外配置Windows后台任务(适合复杂场景)
  • 最后的Read-Host用于维持脚本运行,避免事件监听随脚本退出失效,可根据需求调整为后台运行逻辑

内容的提问来源于stack exchange,提问作者Nawad-sama

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 18:46:11