Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a feature to read the content of currently displayed Toast Notifications #191

Open
IanBAdams opened this issue Jan 31, 2022 · 4 comments
Assignees

Comments

@IanBAdams
Copy link

Summary of the new feature/enhancement

Provide a feature to read the content of currently displayed Toast Notifications. The Get-BTHistory cmdlet lists the currently displayed Toast Notifications. However, this doesn't include the content of these. If I was able to read the content of the currently displayed Toast Notifications I would then be able to check for any which are of no interest and remove them using Remove-BTNotification.

Proposed technical implementation details (optional)

As an example the following Toast Notification is displayed regularly on my computer:

image

The new feature would allow me to check the contents of currently displayed Toast Notifications. If the contents matches the 'Items skipped during scan' Toast Notification I would then remove it using Remove-BTNotification.

Ideally, I would also like a feature provided which could run a script whenever a Toast Notification is displayed. However, this is a 'nice to have' since I would be able to include the necessary logic to check and remove Toast Notifications within one of my scripts which runs every 10 minutes.

@IanBAdams
Copy link
Author

IanBAdams commented Feb 2, 2022

I did a search for Get-BTHistory and came across 'Get-BurnToast #62'. Part of the comments indicated you could retrieve the contents of displayed Toast Notifications using '(Get-BTHistory).Data.Values'. This didn't work for me but one of the previous comments suggested using ' (Get-BTHistory).Content.InnerText'. This did work and allowed me to retrieve the Text for the Toast Notification but not the Header. An example follows of what I did:

$ToastHeader = New-BTHeader -Id '002' -Title 'Test Heading'
New-BurntToastNotification -Text "Test Line 1", "Test Line 2" -UniqueIdentifier "Test" -Header $ToastHeader -SuppressPopup

Get-BTHistory

Result

ExpirationTime : 05/02/2022 11:35:46 +00:00
Content : Windows.Data.Xml.Dom.XmlDocument
Tag : Test
SuppressPopup : False
Group : Test
RemoteId :
NotificationMirroring : Allowed
Priority : Default
Data :
ExpiresOnReboot : False

(Get-BTHistory).Content.InnerText

Result

Test Line 1Test Line 2

So this allows me to do what I'm looking for albeit without the Heading and without the line breaks in the Text.

PS Since Get-BTHistory isn't working with PowerShell 7.2.1 I've had to use Windows PowerShell 5.1 to test this.

@Windos
Copy link
Owner

Windos commented Mar 19, 2022

Get-BTHistory is effectively retrieving what is being sent to the Windows API so it's not really... friendly 😅

That example of looking into (Get-BTHistory).Data.Values only applies to what you're using "data bindings" as it's looking into what strings are being injected into the placeholders locations on the toast (more in this blog post)

The content property does give you full access to the XML object that is the notification, so you can see the raw XML:

(Get-BTHistory).Content.GetXML()

Or you can walk down the child elements (this, uhh, isn't intuitive), but this will show all the text and image elements:

(Get-BTHistory).Content.ChildNodes.where({$_.NodeName -eq 'toast'}).ChildNodes.ChildNodes.ChildNodes | Select-Object TagName, InnerText

n.b. images don't have "innertext" but this will at least show you that there was an image.

The problem is, there's no one way to create a toast, so I can't guarantee that this last option will always work to find the text you're looking for.

@IanBAdams
Copy link
Author

Once again many thanks for getting back to me on this.

Also, thanks for the explanation, suggestions and link to the blog post. I had looked through the various issues and blog posts but obviously must have missed this one somehow.

Looks like I've got a bit of reading and testing to do.

@IanBAdams
Copy link
Author

I read your blog post that explained about data bindings and now it makes sense why (Get-BTHistory).Data.Values didn’t work for me.

I’ve been playing around with the raw XML extracted by (Get-BTHistory).Content.GetXML(). From this I can obtain the heading, text and image source for a toast. You’re no doubt aware of this but just in case it’s helpful to someone else I’ve included an example below.

The following creates the example toast (you would need to substitute one of your own images after -AppLogo):
$ToastHeader = New-BTHeader -Id '001' -Title 'Heading Line'
New-BurntToastNotification -AppLogo 'C:\_Software\Software\Portable\AnyBoxImages\2013-08-09 @ 10.40.43.JPG' -Text "Line 1","Line 2","Line 3" -UniqueIdentifier "IA_Something" -Header $ToastHeader

$ToastXml = (Get-BTHistory).Content.GetXML() stores the XML for the toast within the $ToastXml variable.

($ToastXml | Select-Xml -Xpath '/toast/header').Node.Title outputs the header for the toast:
Heading Line

($ToastXml | Select-Xml -Xpath '/toast/visual/binding/text').Node.InnerText outputs the text for the toast:
Line 1
Line 2
Line 3

($ToastXml | Select-Xml -Xpath '/toast/visual/binding/image').Node.Src outputs the image source for the toast.
C:_Software\Software\Portable\AnyBoxImages\2013-08-09 @ 10.40.43.JPG

I've only managed to test on a few different toasts so doubt this will work in all circumstances, especially given your closing comment from your previous post

The problem is, there's no one way to create a toast, so I can't guarantee that this last option will always work to find the text you're looking for.

Now that I know a bit about data bindings the above won't work for these types of toast.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants