问题
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