繁体   English   中英

powershell脚本创建Windows 10通知,弹出后它会消失

[英]powershell script creates Windows 10 notification and it disappears after popup

以下powershell脚本成功创建了一个通知但是在小弹出窗口缩回之后它没有显示在Notification Center上,是否有任何方法将它留在通知中心,直到用户解除它为止?

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")

然后在“Settings \\ System \\ Notifications&Actions”中,应该注册名为“PowerShellAppId”的新应用程序。

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


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

你可以使用PowerShell这样做:

Set-ItemProperty "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$prodName" -Name "ShowInActionCenter" -Type Dword -Value "1"

考虑使用常量应用程序名称,例如NotificationManager来简化操作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM