windows - powershell脚本创建Windows 10通知,并且在弹出窗口后消失

标签 windows powershell notifications windows-10

以下powershell脚本成功创建了通知,但是在小弹出窗口撤消后,它没有显示在通知中心上,有什么方法可以将其保留在通知中心中,直到用户将其关闭为止?

param([String]$prodName)    
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] > $null

$ToastTemplate = '
<toast launch="app-defined-string">
    <visual>
        <binding template="ToastGeneric">
            <text>'+$prodName+'</text>
        </binding>
    </visual>
</toast>'

Write-Output $ToastTemplate;

$currTime = (Get-Date).AddSeconds(10);
"currTime : " + $currTime

$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)

$schedNotification = New-Object Windows.UI.Notifications.ToastNotification($xml)
$schedNotification.SuppressPopup = $True
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($prodName)
$notifier.Show($schedNotification)

$schedNotification = New-Object Windows.UI.Notifications.ScheduledToastNotification($xml, $currTime)
$notifier.AddToSchedule($schedNotification)

最佳答案

如果显示这样的通知:

CreateToastNotifier("PowerShellAppId")

然后在“设置\系统\通知和操作”中,应注册名为“PowerShellAppId”的新应用。

编辑它,然后选择选项“在操作中心显示通知”。如果再次运行脚本,则消息应留在通知面板中。

在您的示例中,您将$prodName作为AppID。因此,每次您使用不同的prodName运行脚本时,Windows都会将其注册为单独的条目,并且您将不得不再次设置注册表标志(“在操作中心显示通知”)。

您可以像这样使用PowerShell来做到这一点:
Set-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$prodName" -Name "ShowInActionCenter" -Type Dword -Value "1"

考虑使用恒定的应用程序名称,例如NotificationManager之类的以简化操作。

关于windows - powershell脚本创建Windows 10通知,并且在弹出窗口后消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39344752/

相关文章:

ios - 如何在应用程序被杀死时从服务器推送通知

google-chrome - 更改 google chrome 桌面通知的大小

powershell - 如何在 PowerShell 中悄悄删除包含内容的目录

android - 在 android 中递增 notification.number

Windows 支持 Jane Street OCaml Core?

javascript - 使用VS CODE将javascript 'code snippet'应用程序安装为可安装的Windows .exe?

linq - 在 PowerShell 中查询 LINQ 样式的列表

powershell - 将 powershell 控制台窗口移动到屏幕左侧的最佳方法是什么?

c++ - 如何在 Windows 上部署 Qt 应用程序?

windows - 有没有办法在 SVN 仓库中设置文件。在结帐时被自动忽略?