powershell script creates Windows 10 notification and it disappears after popup

风格不统一 提交于 2019-12-11 06:23:57

问题


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)

回答1:


If you show your notification like this:

CreateToastNotifier("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.


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.

You can do it using PowerShell like this:

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

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



来源:https://stackoverflow.com/questions/39344752/powershell-script-creates-windows-10-notification-and-it-disappears-after-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!