powershell 脚本创建 Windows 10 通知并在弹出后消失 [英] powershell script creates Windows 10 notification and it disappears after popup

查看:102
本文介绍了powershell 脚本创建 Windows 10 通知并在弹出后消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下 powershell 脚本成功创建了一个通知,但在小弹出窗口收回后它不会显示在通知中心,有什么方法可以将它留在通知中心直到用户关闭它?

the following powershell script successfully creates a notification but after the little popup retracts it doesn't show on the Notification Center, any way to leave it in the notification center until the user dismisses it ?

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)

推荐答案

如果你这样显示你的通知:

If you show your notification like this:

CreateToastNotifier("PowerShellAppId")

然后在您的设置\系统\通知和操作"中,应该注册名为PowerShellAppId"的新应用.

Then in your "Settings \ System \ Notifications & Actions", there should register new app named "PowerShellAppId".

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

Edit it, and select option "Show notifications in action center". If you run your script again, message should leave in notification panel.

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

In your example you have $prodName as AppID. So each time, you run script with different prodName, Windows will register it as separate entry, and you will have to set registry flag ("Show notifications in action center") again.

您可以像这样使用 PowerShell 执行此操作:

You can do it using PowerShell like this:

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

考虑使用固定的应用名称,比如 NotificationManager 之类的东西来简化事情.

Consider using constant app name, something like NotificationManager to simplify things.

这篇关于powershell 脚本创建 Windows 10 通知并在弹出后消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆