Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

uwp - 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://docs.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://docs.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-F8BC1E53F4E6WinFormSampleAppActivator" duration="short">
    <visual>
        <binding template="ToastGeneric">
            
            <image placement="appLogoOverride" src="C:UsersdksilOneDriveDesktopGos.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!!!

question from:https://stackoverflow.com/questions/65835196/windows-toast-notification-com-not-working

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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:UsersdksilOneDriveDesktopGos.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:UsersdksilOneDriveDesktopGo" />
        
    </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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...