Search code examples
powershelltoast

Toast notification disappears after few seconds, how to hold until taking any action on display?


I'm trying to make a small PowerShell program to display a toast notification after log on. I'd like the custom toast notification will stay there until user closes it. But but it disappears quickly. (about 5 seconds)

How can I do that ?

Script:

Import-Module BurntToast

$button = New-BTButton -Dismiss -Content "OK"

New-BurntToastNotification -Text 'Yoooo, this is a toast notification... from PowerShell!' -Button $button

Solution

  • The following works, using a variety of cmdlets from the BurntToast module, as of v0.8.5:

    Submit-BTNotification -Content (
      New-BTContent -Scenario IncomingCall `
                    -Audio (New-BTAudio -Silent) `
                    -Visual (
                      New-BTVisual -BindingGeneric (
                        New-BTBinding -Children (New-BTText -Content 'Testing...') `
                                      -AppLogoOverride (
                                        New-BTImage -AppLogoOverride -Crop Circle -Source $env:windir\System32\wpcatltoast.png
                                      )
                      )  
                    ) 
    )
    
    • -Scenario IncomingCall is the key to making the notification "sticky".
    • New-BTAudio -Silent suppresses the sound that accompanies the notification when it first appears.
      • If you don't use this, you'll get two ringing sounds in quick succession, instead of the single sound that is used by default.
      • Unfortunately, explicitly selecting the default sound with New-BTAudio -Source ms-winsoundevent:Notification.Default is not effective, as -Scenario IncomingCall seems to override it (the same goes for attempts to select any other sound).

    Note:

    • No button for dismissal is created, because a close icon is available by default.

      • A custom logo is displayed, which is shown to the left of the text.
      • Note that you seemingly can not customize the application name and logo in the title of the notification, which, when called from PowerShell, invariably shows the PowerShell logo and the title "Windows PowerShell" (even when calling from PowerShell (Core)).
    • While this Microsoft Q&A post suggests that adding a button implicitly makes a notification sticky, at least in the context of the BurntToast module that is not true, as you've experienced.

    • The docs for the underlying .NET APIs suggest that the Reminder and Alarm scenario values too make a notification sticky, but at least in the context of the BurntToast module that is not true; in fact, the latter seems to ignore these -Scenario values as of v0.8.5 and uses a default notification (same as -Scenario Default).

    • While there is a way to modify the default display duration (which is 5 seconds) via a registry value, as shown below, this requires a logoff or reboot to take effect, which means that you can not change it temporarily, in-session, and it will apply to all notifications (that do not explicitly control their display duration).

      # Set the default display duration to 300 seconds == 5 minutes.
      # Requires logoff or reboot to take effect.
      Set-ItemPropert 'HKCU:\Control Panel\Accessibility' MessageDuration 300