|
-
May 16th, 2025, 02:42 PM
#1
VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfaces)
From the "How to interactively annoy your users" series, here's a VB6 project showcasing ToastNotifications (which were later insipidly renamed to "App Notifications") including some interactive elements (TextBox, ComboBox and Buttons) that the users can play with and respond to your notification.
A TypeLib with the required WinRT interface definitions is included in the ZIP archive, don't forget to set a reference to it.
These ToastNotifications use an XML format which adheres to some rather strict standards but is nevertheless quite customizable. You could provide the whole XML code from the beginning but this project shows how you can start with a built-in already provided template and add your own flavors to it. The base template used here included just a text-only notification with a bold title and two lines of text. Building on top of it, I added a banner-style image at the top (so-called "hero" image), a small "Logo" image, a TextBox to write something back, a ComboBox to select a fictive rating and two action buttons at the bottom.
This is how it looks in the Action Center:

The notification sends events back to your app communicating whatever happened to it (like being dismissed or users interacting with it in some way). While it is possible to receive these events in "offline" mode (like when users interact with the notification in the Action Center long after your app had been closed), this project demonstrates only real-time events and removes the notification from the Action Center after being dismissed.
Here is the demo project: ToastNotifications.zip (Updated to include image downloading)
Requirements: Windows 10 or later!
Last edited by VanGoghGaming; Jun 9th, 2025 at 06:07 AM.
Reason: Added new features
-
May 16th, 2025, 06:31 PM
#2
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
If anything desperately needed a x64 compatible tB version...
-
May 17th, 2025, 08:25 AM
#3
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Yeah, I was going to, but now we have to wait for Wayne to fix this SafeArray bug in tB so that it can report the correct GUID instead of IID_IDispatch all the time!
-
May 17th, 2025, 04:39 PM
#4
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
That didn't take long! Here is the x64 compatible tB version. Works great so far, no TypeLib is needed, I've included the WinRT interfaces as a package instead.
-
May 19th, 2025, 12:54 PM
#5
Fanatic Member
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
-
May 27th, 2025, 08:34 AM
#6
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Very nice.
It tried it, but haven't found too much documentation to customize the notification
Is there a site that explain all the possible parametsr ?
I want to get rid of the protocol 'in the sample and having 2 button 1 dismiss and the other to confirm (with some parameters) so I can retrieve it within the app
Dim i As Long
Set XmlDocument = ToastNotificationManagerStatics.GetTemplateContent(ToastTemplateType_ToastText04)
With XmlDocument.GetElementsByTagName(NewStringRef("toast"))
If .Length > 0 Then
With SetElementAttributes(.Item(0), "activationType", "background", "duration", "long", "useButtonStyle", "true")
With .AppendChild(XmlDocument.CreateElement(NewStringRef("actions")))
SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("input"))), "id", "textBox", "type", "text", "placeHolderContent", sComments
'With SetElementAttributes(.AppendChild(XmlDocument.CreateElement(NewStringRef("input"))), "id", "comboBox", "type", "selection", "defaultInput", "5")
' For i = 1 To 5
' SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("selection"))), "id", CStr(i), "content", "Rating: " & String$(i, ChrW$(&H2605))
' Next i
'End With
SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("action"))), "content", sButton1, "arguments", sArguments1, "activationType", "background", "hint-buttonStyle", "Success"
'SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("action"))), "content", "View with Bing Maps", "arguments", "bingmaps:?q=The Enchantments, Washington", "activationType", "protocol", "hint-buttonStyle", "Success"
SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("action"))), "content", vbNullString, "arguments", "dismiss", "activationType", "system", "hint-buttonStyle", "Critical"
'SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("action"))), "content", vbNullString, "arguments", "dismiss", "activationType", "system", "hint-buttonStyle", "Critical"
End With
End With
End If
End With
With XmlDocument.GetElementsByTagName(NewStringRef("binding"))
If .Length > 0 Then SetElementAttributes .Item(0), "template", "ToastGeneric"
End With
any idea ?
Thanks
NB : I made it as OCX, so easiest to add in an existing project
-
May 27th, 2025, 10:02 AM
#7
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Hey mate, good to see there's at least one person showing interest, haha! This sample is barely scratching the surface, there's a lot more you can do with toast notifications.
Here's the official Toast schema for the accomplished chef with a complete kitchen full of ingredients to burn bread!
As I mentioned in the first post above, this demo shows only the bare minimum functionality to get toast notifications working for Win32 apps. A lot of advanced options won't work without the full implementation.
The Respond to toast activations article tells you what needs to be done to achieve full functionality:
- You need to create a shell link (*.lnk file) for your application in the "StartMenu\Programs" folder (the same as most Installers or Setup programs would do for a complete application). The link must have a couple of properties set (with IPropertyStore) as described in the article. This is the easier part.
- You need to register a COM activator that implements the INotificationActivationCallback interface. This allows Windows to communicate with your app at all times and even start it in the background in order to send notification data that the user interacted with at a later time. This is the slightly more complicated part but not by much.
Without these two steps the only notifications that still work are those with activationType="protocol" (like the "bingmaps:" protocol in the example) and activationType="system" (like the "Dismiss" button in the example). The more advanced activation types (like "foreground" and "background") won't work without completing both steps outlined above.
I'm currently working on the COM activator part. Your app needs to act like a local COM server so other applications can connect to it (call CoRegisterClassObject at startup) and implement the IClassFactory interface. I'll post a complete sample later when it's done.
-
May 27th, 2025, 10:38 AM
#8
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
yop
I wanted to replace my old Notifications by a more modern one (and also nicer!)
Seems a bit more complicated !
I'll investigate when I'll have more time
Thanks again
-
May 27th, 2025, 11:29 AM
#9
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Well, I did an easy trick.
When creating the toast
Friend Sub SetupToast(sComments As String, sButton1 As String, sData As String)
Dim i As Long
Set XmlDocument = ToastNotificationManagerStatics.GetTemplateContent(ToastTemplateType_ToastText04)
With XmlDocument.GetElementsByTagName(NewStringRef("toast"))
If .Length > 0 Then
With SetElementAttributes(.Item(0), "activationType", "background", "duration", "long", "useButtonStyle", "true", "data", sData)
I added , "data", sData)
and in
Private Sub ITypedEventHandlerToastNotificationActivated_Invoke(ByVal Sender As IToastNotification, ByVal Args As IInspectable)
Dim ToastActivatedEventArgs As IToastActivatedEventArgs
Set ToastActivatedEventArgs = Args
RaiseEvent NotificationActivated(NewString(ToastActivatedEventArgs.Arguments).GetString, NewString(AsIPropertyValue(AsIMapIInspectable(Args).Lookup(NewStringRef("textBox"))).GetString).GetS tring, GetStringBetweenTags(NewString(AsIXmlNodeSerializer(XmlDocument).GetXml).GetString, "data-sa=""", """"))
End Sub
Just one button needed
Tomorrow, more tests
-
May 27th, 2025, 12:04 PM
#10
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Yes it's a nice trick but do try to use code tags when posting snippets of code to improve readability.
There are other advantages to doing it "by the book". For example if for some reason the user decides to turn off your notification from the context menu (the three dots icon in the toast) there will be no way to turn them back on in the absence of the link file. Your app won't appear in the list from Windows Settings -> System -> Notifications & actions -> Get notifications from these senders!
-
May 27th, 2025, 03:17 PM
#11
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
@VanGoghGaming: You know my ToastNotification example. It contains everything you need to fully utilize ToastNotification (create a link in the Start menu, the COM activator for INotificationActivationCallback). A few nice Toast examples are also included.
-
May 27th, 2025, 04:56 PM
#12
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
I was looking at your basic toast example included in your mega WinRT project. Now I see you also have a separate, much more comprehensive toast project. This is great, could've saved me a lot of time chatting back and forth with CoPilot, haha!
-
May 27th, 2025, 11:46 PM
#13
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
@Franky, where id this WinRT project ?
Can't find it here
-
May 28th, 2025, 12:08 AM
#14
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
 Originally Posted by Thierry69
@Franky, where id this WinRT project ?
Can't find it here 
https://www.activevb.de/cgi-bin/uplo...oad.pl?id=3918
However, my example doesn't use a TLB and is just a demo project. I would wait until VanGoghGaming integrates everything into a WinRT TLB.
-
May 28th, 2025, 12:17 AM
#15
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
 Originally Posted by VanGoghGaming
I was looking at your basic toast example included in your mega WinRT project. Now I see you also have a separate, much more comprehensive toast project. This is great, could've saved me a lot of time chatting back and forth with CoPilot, haha! 
In this example, I'm saving the unique GUID required for the ToastNotification in an external file. Normally, an installer would write this to the registry.
I'm waiting for the XAML controls to be available in your WinRT TLB. Don't stress, it'll happen sometime.
-
May 28th, 2025, 12:43 AM
#16
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
-
May 28th, 2025, 12:51 AM
#17
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
I've been generating the GUID based on a 16-bytes hash of the AppId string (which is App.Title by default) to avoid saving it anywhere. I've already got the COM Activator working fine and I'm currently testing it to see what is the best course of action when the app is not already started (like show the main form or just perform some action in response to the notification without showing any forms). There doesn't seem to be any difference between activationType "foreground" or "background". The COM Activator treats them exactly the same.
And yeah, it would be nice to have the XAML controls in the TLB sometime (it's a massive task though).
-
May 28th, 2025, 04:12 PM
#18
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
 Originally Posted by VanGoghGaming
There doesn't seem to be any difference between activationType "foreground" or "background". The COM Activator treats them exactly the same.
The difference of the activationType is explained here. https://stackoverflow.com/questions/...ackground-task
-
May 29th, 2025, 06:45 PM
#19
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Thanks for that article, it keeps mentioning a "background task" and as far as I was able to ascertain this is something exclusive to UWP apps. For unpackaged apps, the archaic "COM Activator" doesn't make any difference between "foreground" and "background" activation. However, it is possible to determine whether your app was started by the "COM Activator" since it passes the "-Embedding" command line argument.
@Thierry69, I have uploaded an updated version that includes the "COM Activator" and as such it unlocks all toast features that were unavailable in the previous version. You need to compile the executable though. You can run it from the IDE as well but the "COM Activator" will start your EXE when you interact with the toast notification since it doesn't know you are in IDE! 
You can now assign individual actions to each button as well as a generic action when the user clicks anywhere on the notification. You can also demonstrate "offline" functionality, like for example close the app immediately after displaying the notification and then interact with it from the "Action Center".
Another interesting thing to note is that when your notification contains "input" elements (like the TextBox or ComboBox above), it does not time-out and stays on screen until you interact with it.
-
May 30th, 2025, 05:01 PM
#20
Fanatic Member
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Why does this code not load the image, it only shows the button?
Code:
<action content="Reminder" arguments="action=reminder&amp;callId=938163" activationType="background" imageUri="https://storage.googleapis.com/rppico.appspot.com/reminder.png"/>
SetElementAttributes .AppendChild(XmlDocument.CreateElement(NewStringRef("action"))), "content", "Reminder", "arguments", "action=reminder&callId=938163", "activationType", "background", "imageUri", "https://storage.googleapis.com/rppico.appspot.com/reminder.png"
Some simple example with only xml text.
-
May 30th, 2025, 05:55 PM
#21
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Unpackaged apps can only load images from files (as shown in this project with the "file:///" protocol). You need an UWP app (like those in the Microsoft Store) to load images from the web.
-
Jun 2nd, 2025, 09:59 AM
#22
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
@VanGoghGaming: I'm not sure if this actually works. Copilot suggested the following when I asked if you could also load images into a ToastNotification via streams. It doesn't seem to work directly via streams, but it does work via a Base64 string. You'd have to try it out.
string base64Image = Convert.ToBase64String(File.ReadAllBytes("path_to_image.png"));
<image src='data:image/png;base64,{base64Image}'/>
In my large WinRT project, for example, there's the class CryptographicBuffer -> EncodeToBase64String(Buffer). Alternatively, something else could be used to convert a byte array to a Base64 string. If that works, you could also load images from the web into a byte array, convert them, and integrate them as shown above.
-
Jun 2nd, 2025, 09:22 PM
#23
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
It's an interesting idea but unfortunately it doesn't work. Unpackaged apps are stuck with loading images from files, so if you want an image from the web, you need to download it first (in the "Temp" folder for example).
-
Jun 3rd, 2025, 01:40 AM
#24
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Unfortunately, I didn't get around to testing it myself yesterday. It actually doesn't work. I specifically asked Copilot about what might be possible with unpacked applications. Copilot must have been hallucinating again.
-
Jun 4th, 2025, 12:35 AM
#25
Addicted Member
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
... in cToastNotification.cls
Code:
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" ( _
ByVal pCaller As Long, ByVal szURL As String, ByVal szFileName As String, _
ByVal dwReserved As Long, ByVal lpfnCB As Long) As Long
Private Function DownloadIfNeeded(sUrl As String, ByRef sLocalFile As String) As String
If LCase$(Left$(sUrl, 7)) = "http://" Or LCase$(Left$(sUrl, 8)) = "https://" Then
Dim sFilename As String
sFilename = App.Path & "\" & "img_" & Format$(Timer * 1000, "00000000") & "_" & Mid$(sUrl, InStrRev(sUrl, "/") + 1)
If URLDownloadToFile(0, sUrl, sFilename, 0, 0) = 0 Then
sLocalFile = sFilename
Else
sLocalFile = vbNullString
End If
Else
sLocalFile = sUrl
End If
If Len(sLocalFile) > 0 Then
DownloadIfNeeded = "file:///" & Replace(sLocalFile, "\", "/")
Else
DownloadIfNeeded = vbNullString
End If
End Function
Friend Sub SetImages(Optional sMainImageFile As String, Optional sLogoImageFile As String)
Dim sMainLocal As String, sLogoLocal As String
Dim sMainSrc As String, sLogoSrc As String
sMainSrc = DownloadIfNeeded(sMainImageFile, sMainLocal)
sLogoSrc = DownloadIfNeeded(sLogoImageFile, sLogoLocal)
With XmlDocument.GetElementsByTagName(NewStringRef("binding"))
If .Length > 0 Then
If Len(sLogoSrc) > 0 Then
SetElementAttributes .Item(0).AppendChild(XmlDocument.CreateElement(NewStringRef("image"))), _
"id", "1", "src", sLogoSrc, "placement", "appLogoOverride", "hint-crop", "circle"
End If
If Len(sMainSrc) > 0 Then
SetElementAttributes .Item(0).AppendChild(XmlDocument.CreateElement(NewStringRef("image"))), _
"id", "2", "src", sMainSrc, "placement", "hero"
End If
End If
End With
End Sub
-
Jun 4th, 2025, 01:36 AM
#26
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
@cliv: Yes, that would be a possibility. However, I assume that since ToastNotifiction is based on WinRT, VanGoghGaming would also use WinRT classes/interfaces to load files/images from the internet. The following option would be possible:
RandomAccessStreamWithContentType <- Windows.Storage.Streams.RandomAccessStreamReference.CreateFromUri(Windows.Foundation.Uri.CreateUri(" https://xxx.png")).OpenReadAsync
The contents of RandomAccessStreamWithContentType could then be written to disk.
-
Jun 9th, 2025, 06:34 AM
#27
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
Just updated this project following Franky's idea to download the image using WinRT methods. Now it includes a cDownloader class to encapsulate this functionality. Downloading is done asynchronously and the class raises an event when completed. The JPG image above is no longer included in the ZIP archive and instead it's downloaded on demand if it doesn't exist:
Added to Form_Load
Code:
If Not FileExists(App.Path, "Canyon.jpg") Then
cmdShowToastNotification.Enabled = False: Set Downloader = New cDownloader
With Downloader
.Uri = "https://picsum.photos/360/202?image=883"
.StartDownloadAsync
End With
End If
In addition to saving data to a file, the event also exposes a buffer object with the downloaded data as well its content type so it can be used separately for downloading anything else (not specific to this project). This code snippet shows how to process the contents of the downloaded buffer:
Code:
Private Function AsIBufferByteAccess(IBufferByteAccess As IBufferByteAccess) As IBufferByteAccess
Set AsIBufferByteAccess = IBufferByteAccess
End Function
Private Sub Downloader_DownloadCompleted(ContentType As String, DataBuffer As IBuffer)
Dim baData() As Byte ' Here we can process the contents of the downloaded buffer if desired
ReDim baData(0 To DataBuffer.Length - 1): vbaCopyBytes DataBuffer.Length, ByVal VarPtr(baData(0)), ByVal AsIBufferByteAccess(DataBuffer).Buffer
Downloader.SaveToFile App.Path, "Canyon.jpg"
cmdShowToastNotification.Enabled = True ' The image download is completed so we can show the notification
#If bInIDE Then
Debug.Print "ContentType", ContentType, "Bytes downloaded", DataBuffer.Length
#End If
End Sub
-
Jun 18th, 2025, 04:18 PM
#28
Fanatic Member
Re: VB6 - Simple ToastNotification with Interactive Elements (using WinRT XML interfa
 Originally Posted by VanGoghGaming
Just updated this project following Franky's idea to download the image using WinRT methods. Now it includes a cDownloader class to encapsulate this functionality. Downloading is done asynchronously and the class raises an event when completed. The JPG image above is no longer included in the ZIP archive and instead it's downloaded on demand if it doesn't exist:
Added to Form_Load
Code:
If Not FileExists(App.Path, "Canyon.jpg") Then
cmdShowToastNotification.Enabled = False: Set Downloader = New cDownloader
With Downloader
.Uri = "https://picsum.photos/360/202?image=883"
.StartDownloadAsync
End With
End If
In addition to saving data to a file, the event also exposes a buffer object with the downloaded data as well its content type so it can be used separately for downloading anything else (not specific to this project). This code snippet shows how to process the contents of the downloaded buffer:
Code:
Private Function AsIBufferByteAccess(IBufferByteAccess As IBufferByteAccess) As IBufferByteAccess
Set AsIBufferByteAccess = IBufferByteAccess
End Function
Private Sub Downloader_DownloadCompleted(ContentType As String, DataBuffer As IBuffer)
Dim baData() As Byte ' Here we can process the contents of the downloaded buffer if desired
ReDim baData(0 To DataBuffer.Length - 1): vbaCopyBytes DataBuffer.Length, ByVal VarPtr(baData(0)), ByVal AsIBufferByteAccess(DataBuffer).Buffer
Downloader.SaveToFile App.Path, "Canyon.jpg"
cmdShowToastNotification.Enabled = True ' The image download is completed so we can show the notification
#If bInIDE Then
Debug.Print "ContentType", ContentType, "Bytes downloaded", DataBuffer.Length
#End If
End Sub
Thanks work perfect
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|