来自PowerShell的Toast通知在操作中心中消失

9
我正在使用此代码从PowerShell脚本发送通知。 PowerShell本身由(持久)Java应用程序启动。
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] > $null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$template = "<toast><visual><binding template=`"ToastText02`"><text id=`"1`">Title</text><text id=`"2`">Test results</text></binding></visual></toast>"
$xml.LoadXml($template)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("sbt").Show($toast)

它们会在屏幕上弹出,在操作中心可见一段时间,但很快就会消失。由相同代码发送的 XAML 应用程序通知会停留一段时间。是否有办法改变通过脚本发送的通知的行为?


你确定 "sbt" 是正确的applicationId吗? - Mathias R. Jessen
好的,它确实显示通知,但由于某种原因,无法在不带参数的情况下调用“CreateToastNotifier”。 - synapse
我认为由于PowerShell任务显示通知后可能立即退出,因此操作中心会查看它并认为“进程不再运行=适合过期”。而如果您使用Java应用程序的applicationId,则它可能会发现通知仍然相关。 - Mathias R. Jessen
1
我曾尝试通过将上述代码置于循环中来从PowerShell交互应用程序发送通知,但它们也会消失。 - synapse
1个回答

8
您必须为每个您希望在操作中心中保留通知的 AppID 设置一个注册表键。
例如,如果您想要保留您的 irssi 通知,请添加以下键:HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\irssi,并设置DWORD名称为ShowInActionCenter,值为1
感谢“Passing By”在本文章评论中提供的解决方案以及Mattias Fors创建文章。
更新:您也可以在“通知和操作”设置中按应用程序切换此选项。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接