Desktop bridge toast open app

1.6k views Asked by At

I've develop a WPF application that I've converter through Desktop App Converter and runs ok.

I´ve added toast notifications calling UWP APIs from desktop app as is explained here https://blogs.msdn.microsoft.com/tiles_and_toasts/2015/10/16/quickstart-handling-toast-activations-from-win32-apps-in-windows-10/

But to be able to open the app automatically when user tap on the toast, I had to create a shortcut and register a COM component same way as https://github.com/WindowsNotifications/desktop-toasts and it's also ok.

I think It´s too much work compared with a UWP app where default behaviour is just this, open app without any code.

How could I open the app tapping toast in my converted app throw the Bridge, same way as a truly UWP?

Thanks.

3

There are 3 answers

3
Franklin Chen - MSFT On BEST ANSWER

I think It´s too much work compared with a UWP app where default behaviour is just this, open app without any code.

There is a simple solution, the technical points as follow:

  1. Using the protocol association which described in the Desktop Bridge app extensions article
  2. Using the protocol ActivationType to launch the corresponding app, ref Adaptive and interactive toast notifications

Some detailed steps we need to notice:

  1. Using protocal activation type in your payload of toast notification: <toast activationType='protocol' launch='mytoastsample:'> <visual> <binding template='ToastGeneric'> <text>Click to launch Wpf Toast Sample</text> </binding> </visual> </toast>

  2. After converting the application to UWP app, we need to open the Output directory and locate the AppxManifest.xml file.

  3. Append the protocol association extension in the AppxManifest.xml file

<Application Id="WpfToastSample" Executable="WpfToastSample.exe" EntryPoint="Windows.FullTrustApplication"> <uap:VisualElements DisplayName="WpfToastSample" Description="WpfToastSample" BackgroundColor="#777777" Square150x150Logo="Assets\SampleAppx.150x150.png" Square44x44Logo="Assets\SampleAppx.44x44.png" /> <Extensions> <uap3:Extension Category="windows.protocol"> <uap3:Protocol Name="mytoastsample" Parameters="/p &quot;%1&quot;" /> </uap3:Extension> </Extensions> </Application>

  1. Follow Manually convert your app to UWP using the Desktop Bridge to repack and resign your app

I created a sample in here

The screenshot(gif): LINK

0
Stefan Wick On

It's actually simpler than this. Once you are using the Desktop Bridge, you can create toasts the same way as UWP apps. Clicking on the toast will launch your Desktop Bridge app in the correct mode. I have published a couple of samples recently that do this. You can download them from the Windows Store and find the sources on GitHub. Links and more details are in this blog post. Let me know if this doesn't help. I can send a more concise 'hello world' sample for this in WPF, if needed.

Thanks, Stefan Wick

0
dkackman On

I have found (in my case a manually converted WinForms app) that toast notifications (using protocol or foreground activation) start new instances of the app. This is good when the app isn't running when the user interacts with the toast. But it's bad if the app is running.

To get around this I've gone to using a Mutex so that the second instance can exit gracefully if another instance is already around. The toast visual argument get based to Main as if they are command line arguments.

I am using a couple helpers to do this which are on GitHub: https://github.com/dkackman/DesktopBridgeEnvironment

So my Main looks like this (where SingleInstance and ExecutionEnvironment are the helper types)

static void Main(string [] args)
{
    using (var instance = new SingleInstance(ExecutionEnvironment.Current.AppId))
    {
        if (instance.IsFirstInstance)
        {
            ExecutionEnvironment.Current.StartupArgs = args;

            HockeyClient.Current.Configure("xxxxxxxxxxxxxxxxxxx");

            try
            {
                HockeyClient.Current.SendCrashesAsync();
            }
            catch { }

            using (var program = new Program())
            {
                program.Show();
            }
        }
    }
}

Elsewhere in the program, the startup args are inspected to see how to process the notification, if any. Kind of brute force, but I couldn't get it to work any other way without multiple app instances being spawned.