Skip to content

Commit 290c501

Browse files
committed
(#264) Add Urgent switch support to BurntToast cmdlets for important notifications
Add -Urgent switch to Submit-BTNotification and New-BurntToastNotification to designate toasts as "Important Notifications" that break through Focus Assist. Fixes #264
1 parent 5eaf783 commit 290c501

8 files changed

+61
-11
lines changed

Help/New-BTContent.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The `New-BTContent` function creates a new `ToastContent` object, the root confi
1818
| `Duration` | Microsoft.Toolkit.Uwp.Notifications.ToastDuration| Enum. How long the toast notification is displayed (Short/Long). | No |
1919
| `Header` | Microsoft.Toolkit.Uwp.Notifications.ToastHeader | ToastHeader object, created via `New-BTHeader`, categorizing the toast in Action Center. | No |
2020
| `Launch` | String | Data passed to the activation context when a toast is clicked. | No |
21-
| `Scenario` | Microsoft.Toolkit.Uwp.Notifications.ToastScenario| Enum. Tells Windows to treat the toast as an alarm, reminder, or more (`ToastScenario`). | No |
21+
| `Scenario` | Microsoft.Toolkit.Uwp.Notifications.ToastScenario| Enum. Tells Windows to treat the toast as an alarm, reminder, or more (`ToastScenario`). Will be ignored if toast is submitted with `-Urgent` switch on the Submit-BTNotification function, as the Urgent scenario takes precedence but cannot be set via this parameter. | No |
2222
| `Visual` | Microsoft.Toolkit.Uwp.Notifications.ToastVisual | **Required.** ToastVisual object, created by `New-BTVisual`, representing the core content of the toast. | Yes |
2323
| `ToastPeople` | Microsoft.Toolkit.Uwp.Notifications.ToastPeople | ToastPeople object, representing recipient/persons (optional, used for group chat/etc). | No |
2424
| `CustomTimestamp`| DateTime | Optional timestamp for when the toast is considered created (affects Action Center sort order). | No |

Help/New-BurntToastNotification.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Creates and displays a rich Toast Notification for Windows.
88

99
The `New-BurntToastNotification` function creates and displays a Toast Notification supporting text, images, sounds, progress bars, actions, snooze/dismiss, and more on Microsoft Windows 10+.
1010
Parameter sets ensure mutual exclusivity (e.g., you cannot use Silent and Sound together).
11+
The `-Urgent` switch will designate the toast as an "Important Notification" that can break through Focus Assist.
1112

1213
## PARAMETERS
1314

@@ -32,6 +33,7 @@ Parameter sets ensure mutual exclusivity (e.g., you cannot use Silent and Sound
3233
| `ActivatedAction` | ScriptBlock | Script block to invoke when the toast is activated (clicked). | No |
3334
| `DismissedAction` | ScriptBlock | Script block to invoke when the toast is dismissed by the user. | No |
3435
| `EventDataVariable`| String | The name of the global variable that will contain event data for use in event handler script blocks. | No |
36+
| `Urgent` | Switch | Designates the toast as an "Important Notification" (scenario 'urgent'), allowing breakthrough of Focus Assist. | No |
3537

3638
## INPUTS
3739

@@ -85,6 +87,14 @@ New-BurntToastNotification -Text 'Integration Complete' -Attribution 'Powered by
8587

8688
Displays a notification with attribution at the bottom, e.g., "Powered by BurntToast".
8789

90+
### Example: Important Notification with Urgent
91+
92+
```powershell
93+
New-BurntToastNotification -Text 'Critical System Alert' -Urgent
94+
```
95+
96+
Marks the notification as "Important", causing it to break through Focus Assist.
97+
8898
## LINKS
8999

90100
- [New-BTButton](New-BTButton.md)

Help/Submit-BTNotification.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Submits a completed toast notification for display.
77
## DESCRIPTION
88

99
The `Submit-BTNotification` function submits a completed toast notification to the operating system's notification manager for display.
10-
This function supports advanced scenarios such as event callbacks for user actions or toast dismissal, sequence numbering to ensure correct update order, unique identification for toast replacement, expiration control, and direct Action Center delivery.
10+
This function supports advanced scenarios such as event callbacks for user actions or toast dismissal, sequence numbering to ensure correct update order, unique identification for toast replacement, expiration control, direct Action Center delivery, and designating a notification as "Important" (using the `-Urgent` switch) so that it can break through Focus Assist.
1111

1212
When a script block is supplied for any of the event actions (`ActivatedAction`, `DismissedAction`, or `FailedAction`), the function ensures that only one registration for a logically identical handler is allowed per notification event type. This is accomplished by normalizing and hashing the script block; the resulting hash uniquely identifies the action for event registration purposes. Attempting to register the same handler multiple times for a given event will not create a duplicate subscription, but instead will produce an informative warning.
1313

@@ -27,6 +27,7 @@ Specifying `-EventDataVariable` implicitly enables the behavior of `-ReturnEvent
2727
| `DataBinding` | Hashtable | Hashtable mapping strings to binding keys in a toast notification. Enables advanced updating scenarios. | No |
2828
| `ExpirationTime` | DateTime | When the notification is no longer relevant and should be removed from the Action Center. | No |
2929
| `SuppressPopup` | Switch | If set, the notification is delivered directly to the Action Center (bypassing immediate display). | No |
30+
| `Urgent` | Switch | If set, designates the toast as an "Important Notification" (scenario 'urgent') which can break through Focus Assist, ensuring the notification is delivered even when user focus mode is enabled. | No |
3031
| `ActivatedAction` | ScriptBlock | A script block executed if the user activates/clicks the toast notification. | No |
3132
| `DismissedAction` | ScriptBlock | A script block executed if the user dismisses the toast notification. | No |
3233
| `FailedAction` | ScriptBlock | A script block executed if the notification fails to display properly. | No |

Tests/New-BurntToastNotification.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,9 @@ Context 'include attribution string' {
182182
$Log | Should -Be $Expected
183183
}
184184
}
185+
186+
Context 'Urgent scenarios' {
187+
It 'does not throw when using -Urgent' {
188+
{ New-BurntToastNotification -Text 'Critical Alert' -Urgent -WhatIf } | Should -Not -Throw
189+
}
190+
}

Tests/Submit-BTNotification.Tests.ps1

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,10 @@ Describe 'Submit-BTNotification' {
4747
$output | Should -Not -Contain "Duplicate or conflicting OnActivated ScriptBlock event detected"
4848
}
4949
}
50+
Context 'Urgent scenario' {
51+
It 'runs without error with -Urgent' {
52+
$mockContent = [Activator]::CreateInstance([Microsoft.Toolkit.Uwp.Notifications.ToastContent])
53+
{ Submit-BTNotification -Content $mockContent -Urgent -WhatIf } | Should -Not -Throw
54+
}
55+
}
5056
}

src/Public/New-BTContent.ps1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
2727
.PARAMETER Scenario
2828
Enum. Tells Windows to treat the toast as an alarm, reminder, or more (ToastScenario).
29+
Will be ignored if toast is submitted with `-Urgent` switch on the Submit-BTNotification function, as the Urgent scenario takes precedence but cannot be set via this parameter.
2930
3031
.PARAMETER Visual
3132
Required. ToastVisual object, created by New-BTVisual, representing the core content of the toast.

src/Public/New-BurntToastNotification.ps1

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
.DESCRIPTION
77
The New-BurntToastNotification function creates and displays a Toast Notification supporting text, images, sounds, progress bars, actions, snooze/dismiss, attribution, and more on Microsoft Windows 10+.
88
Parameter sets ensure mutual exclusivity (e.g., you cannot use Silent and Sound together).
9+
The `-Urgent` switch will designate the toast as an "Important Notification" that can break through Focus Assist.
910
1011
.PARAMETER Text
1112
Up to 3 strings to show within the Toast Notification. The first is the title.
@@ -64,6 +65,9 @@
6465
.PARAMETER EventDataVariable
6566
The name of the global variable that will contain event data for use in event handler script blocks.
6667
68+
.PARAMETER Urgent
69+
If set, designates the toast as an "Important Notification" (scenario 'urgent'), allowing it to break through Focus Assist.
70+
6771
.INPUTS
6872
None. You cannot pipe input to this function.
6973
@@ -186,7 +190,9 @@
186190

187191
[scriptblock] $DismissedAction,
188192

189-
[string] $EventDataVariable
193+
[string] $EventDataVariable,
194+
195+
[switch] $Urgent
190196
)
191197

192198
$ChildObjects = @()
@@ -300,6 +306,10 @@
300306
$ToastSplat.Add('EventDataVariable', $EventDataVariable)
301307
}
302308

309+
if ($Urgent) {
310+
$ToastSplat.Add('Urgent', $true)
311+
}
312+
303313
if ($PSCmdlet.ShouldProcess( "submitting: $($Content.GetContent())" )) {
304314
Submit-BTNotification @ToastSplat
305315
}

src/Public/Submit-BTNotification.ps1

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
4848
.PARAMETER EventDataVariable
4949
If specified, assigns the $Event variable from notification callbacks to this global variable name (e.g., -EventDataVariable MyVar gives $global:MyVar in handlers). Implies ReturnEventData.
5050
51+
.PARAMETER Urgent
52+
If set, designates the toast as an "Important Notification" (scenario 'urgent') which can break through Focus Assist, ensuring the notification is delivered even when user focus mode is enabled.
53+
5154
.INPUTS
5255
None. You cannot pipe input to this function.
5356
@@ -71,6 +74,7 @@
7174
[hashtable] $DataBinding,
7275
[datetime] $ExpirationTime,
7376
[switch] $SuppressPopup,
77+
[switch] $Urgent,
7478
[scriptblock] $ActivatedAction,
7579
[scriptblock] $DismissedAction,
7680
[scriptblock] $FailedAction,
@@ -84,17 +88,29 @@
8488

8589
$ToastXml = [Windows.Data.Xml.Dom.XmlDocument]::new()
8690

91+
$ToastXmlContent = $Content.GetContent()
92+
8793
if (-not $DataBinding) {
88-
$CleanContent = $Content.GetContent() -Replace '<text(.*?)>{', '<text$1>'
89-
$CleanContent = $CleanContent.Replace('}</text>', '</text>')
90-
$CleanContent = $CleanContent.Replace('="{', '="')
91-
$CleanContent = $CleanContent.Replace('}" ', '" ')
92-
93-
$ToastXml.LoadXml($CleanContent)
94-
} else {
95-
$ToastXml.LoadXml($Content.GetContent())
94+
$ToastXmlContent = $ToastXmlContent -replace '<text(.*?)>{', '<text$1>'
95+
$ToastXmlContent = $ToastXmlContent.Replace('}</text>', '</text>')
96+
$ToastXmlContent = $ToastXmlContent.Replace('="{', '="')
97+
$ToastXmlContent = $ToastXmlContent.Replace('}" ', '" ')
9698
}
9799

100+
$ToastXml.LoadXml($ToastXmlContent)
101+
102+
# I've been unable to redirect the error from adjusting attributes on the Toast XML
103+
# to just direct it to null, and so I'm temporarily changing the Error Action
104+
# Preference and then changing it back to what it was originally.
105+
$PrevErrorActionPref = $ErrorActionPreference
106+
$ErrorActionPreference = 'SilentlyContinue'
107+
108+
if ($Urgent) {
109+
$ToastXml.GetElementsByTagName('toast')[0].SetAttribute('scenario', 'urgent')
110+
}
111+
112+
$ErrorActionPreference = $PrevErrorActionPref
113+
98114
$Toast = [Windows.UI.Notifications.ToastNotification]::new($ToastXml)
99115

100116
if ($DataBinding) {

0 commit comments

Comments
 (0)