Search code examples
windowsuwpnotificationscomtoast

Windows Toast Notification, COM Not working


I have been working on toast notification with the below requirements.

  • The application needs to handle all the types of activation Types(foreground, background, protocol)

So I created a sample UWP app. its working for foreground and background. But when I create a toast from the powershell. The notification is not getting activated.

I followed the steps in - https://learn.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast-desktop?tabs=msix-sparse

I also verified and can see the GUID in registry.

Repo to the sample app - https://learn.microsoft.com/en-us/windows/uwp/design/shell/tiles-and-notifications/send-local-toast You need to add a nuget package to make it work - Microsoft.Toolkit.Uwp.Notifications

Powershell script -


[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null

$APP_ID = 'DF068783-F662-4A13-8BFC-F8BC1E53F4E6'

$template = @"
<toast activationType="protocol" launch="DF068783-F662-4A13-8BFC-F8BC1E53F4E6\WinFormSampleAppActivator" duration="short">
    <visual>
        <binding template="ToastGeneric">
            
            <image placement="appLogoOverride" src="C:\Users\dksil\OneDrive\Desktop\Go\s.jpg" />
            
            
            <text><![CDATA[Test]]></text>
            
            
            <text><![CDATA[some test]]></text>
            
        </binding>
    </visual>
    
    <audio src="ms-winsoundevent:Notification.Default" loop="false" />
    
    
    <actions>
        
        <action activationType="protocol" content="I'm a button" arguments="DF068783-F662-4A13-8BFC-F8BC1E53F4E6" />
        
    </actions>
    
</toast>
"@

$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($template)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($APP_ID).Show($toast)
    

enter image description here

Help Appreciated!!!


Solution

  • After spending hours on google and trying a hell lot of stuff. I found that the problem was with the Power shell script.

    The below PS script works. If you can tell Why this one works but not the above one it will be great.

    $null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
    $null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
    
    $App = "af954980-b64d-4c13-858f-614bcbeb295e_304r6e67w7w2y!App"
    
    [xml]$Toast = @"
    <toast duration="short">
        <visual>
        <binding template="ToastGeneric">
            <image placement="appLogoOverride" src="C:\Users\dksil\OneDrive\Desktop\Go\s.jpg" />
            <text>Hello</text>
            <text>Deepak Dash</text>
        </binding>
        </visual>
        <audio src="ms-winsoundevent:Notification.Default" loop="false" />
        <actions>
            
            <action activationType="Protocol" content="I'm a button" arguments="C:\Users\dksil\OneDrive\Desktop\Go" />
            
        </actions>
    </toast>
    "@
    
    
    # Load the notification into the required format
    $ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
    $ToastXml.LoadXml($Toast.OuterXml)
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($ToastXml)
    

    Note - I have also tried by setting $App as "af954980-b64d-4c13-858f-614bcbeb295e_304r6e67w7w2y!App". This also did not work for the previous PS script.

    Thanks, Deepak Dash