以下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/