From 644005e5e16dd0265a5b6b15bd996a293d99b583 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Thu, 10 Dec 2020 16:00:26 -0800 Subject: [PATCH 01/10] Works with .NET 5 --- .../Microsoft.Toolkit.Uwp.Notifications.csproj | 13 +++++++------ global.json | 2 +- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj index 414590f4f2b..b02027feca6 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj @@ -3,7 +3,7 @@ - netstandard1.4;uap10.0;net461;netcoreapp3.1 + netstandard1.4;uap10.0;net461;netcoreapp3.1;net5.0;net5.0-windows10.0.17763.0 $(DefineConstants);NETFX_CORE Windows Community Toolkit Notifications @@ -25,16 +25,17 @@ - - - $(DefineConstants);WINDOWS_UWP;WIN32 - + + + + $(DefineConstants);WINDOWS_UWP;WIN32 + - + diff --git a/global.json b/global.json index 80d84a257c6..a3d78890e48 100644 --- a/global.json +++ b/global.json @@ -1,5 +1,5 @@ { "msbuild-sdks": { - "MSBuild.Sdk.Extras": "2.0.54" + "MSBuild.Sdk.Extras": "3.0.22" } } \ No newline at end of file From 186d154c7dcf159004c09d1e507bd3f8082b207a Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Fri, 11 Dec 2020 13:14:21 -0800 Subject: [PATCH 02/10] Keep UWP min version at 10240 --- .../Microsoft.Toolkit.Uwp.Notifications.csproj | 2 +- build/build.cake | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj index 601b634682b..f58d2721c1d 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj @@ -3,7 +3,7 @@ - netstandard1.4;uap10.0;net461;netcoreapp3.1;net5.0;net5.0-windows10.0.17763.0 + netstandard1.4;uap10.0.19041;net461;netcoreapp3.1;net5.0;net5.0-windows10.0.17763.0 $(DefineConstants);NETFX_CORE Windows Community Toolkit Notifications diff --git a/build/build.cake b/build/build.cake index edefaa1eea7..f1bd29f808d 100644 --- a/build/build.cake +++ b/build/build.cake @@ -223,6 +223,7 @@ Task("Package") } .SetConfiguration("Release") .WithTarget("Pack") + .WithTarget("Microsoft_Toolkit_Uwp_Notifications") .WithProperty("GenerateLibraryLayout", "true") .WithProperty("PackageOutputPath", nupkgDir); @@ -313,8 +314,8 @@ Task("MSTestUITest") ////////////////////////////////////////////////////////////////////// Task("Default") - .IsDependentOn("Build") - .IsDependentOn("Test") + // .IsDependentOn("Build") + // .IsDependentOn("Test") .IsDependentOn("Package"); Task("UpdateHeaders") From 9dd9fd5209c88e05639129ddc18815c01cea30e8 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Fri, 11 Dec 2020 15:06:42 -0800 Subject: [PATCH 03/10] Undo accidentally committed change --- build/build.cake | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/build/build.cake b/build/build.cake index f1bd29f808d..edefaa1eea7 100644 --- a/build/build.cake +++ b/build/build.cake @@ -223,7 +223,6 @@ Task("Package") } .SetConfiguration("Release") .WithTarget("Pack") - .WithTarget("Microsoft_Toolkit_Uwp_Notifications") .WithProperty("GenerateLibraryLayout", "true") .WithProperty("PackageOutputPath", nupkgDir); @@ -314,8 +313,8 @@ Task("MSTestUITest") ////////////////////////////////////////////////////////////////////// Task("Default") - // .IsDependentOn("Build") - // .IsDependentOn("Test") + .IsDependentOn("Build") + .IsDependentOn("Test") .IsDependentOn("Package"); Task("UpdateHeaders") From 3920ebce4d355750c8e3acbd722c3c1118fe6343 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Mon, 21 Dec 2020 18:28:04 -0800 Subject: [PATCH 04/10] Aleader/notifications net 5 and native (#3644) * Making package works but throws at runtime * I think it all works! * Remove comment and update description --- Directory.Build.targets | 2 +- ...Microsoft.Toolkit.Uwp.Notifications.csproj | 27 ++++--- ...Microsoft.Toolkit.Uwp.Notifications.nuspec | 80 +++++++++++++++++++ 3 files changed, 95 insertions(+), 14 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec diff --git a/Directory.Build.targets b/Directory.Build.targets index ab132c6cf4c..4423ad40f2f 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -1,7 +1,7 @@ - true + true diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj index f58d2721c1d..2a456cf9275 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj @@ -1,22 +1,13 @@  - - - netstandard1.4;uap10.0.19041;net461;netcoreapp3.1;net5.0;net5.0-windows10.0.17763.0 + netstandard1.4;uap10.0.19041;net461;netcoreapp3.1;net5.0;net5.0-windows10.0.17763.0;native $(DefineConstants);NETFX_CORE Windows Community Toolkit Notifications - - Generate tile, toast, and badge notifications for Windows 10 via code, with the help of IntelliSense. - Adds Support for adaptive tiles and adaptive/interactive toasts for Windows 10. It is part of the Windows Community Toolkit. - Supports C# and C++ UWP project types. - Also works with C# portable class libraries and non-UWP C# projects like server projects. - This project contains outputs for netstandard1.4, uap10.0 and native for WinRT. - - notifications win10 windows 10 tile tiles toast toasts badge xml uwp c# csharp c++ true 10240 10.0.10240.0 + Microsoft.Toolkit.Uwp.Notifications.nuspec @@ -55,6 +46,10 @@ + + + 10.0.19041.0 + winmdobj @@ -64,8 +59,7 @@ native UAP,Version=v10.0 uap10.0 - 10.0.19041.0 - 10.0.10240.0 + 10.0.19041.0 $(DefineConstants);NETFX_CORE;WINDOWS_UWP;WINRT false .NETCore @@ -77,5 +71,12 @@ UAP$(TargetPlatformMinVersion.Replace('.', '_')) true + + + + + buildOutput=bin\$(Configuration);version=$(Version) + + diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec new file mode 100644 index 00000000000..eae33f7a3dc --- /dev/null +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec @@ -0,0 +1,80 @@ + + + + Microsoft.Toolkit.Uwp.Notifications + $version$ + Windows Community Toolkit Notifications + Microsoft.Toolkit + + true + https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/license.md + https://github.com/windows-toolkit/WindowsCommunityToolkit + https://raw.githubusercontent.com/windows-toolkit/WindowsCommunityToolkit/master/build/nuget.png + The official way to send toast notifications on Windows 10 via code rather than XML, with the help of IntelliSense. Supported by all C# app types, including WPF, UWP, WinForms, and Console, even without packaging your app as MSIX. Also supported by C++ UWP apps. + + Additionally, generate notification payloads from your ASP.NET web server to send as push notifications, or generate notification payloads from class libraries. + + For UWP/MSIX apps, you can also generate tile and badge notifications. + https://github.com/windows-toolkit/WindowsCommunityToolkit/releases + (c) .NET Foundation and Contributors. All rights reserved. + notifications win10 windows 10 tile tiles toast toasts badge xml uwp c# csharp c++ wpf winforms + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From ad7be92b9e93e1237e4cc1e02c4f9eee2abb8cf1 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Tue, 22 Dec 2020 13:43:35 -0800 Subject: [PATCH 05/10] Support for UWP 10240 Just using winmd version --- Directory.Build.targets | 7 ++++--- .../Microsoft.Toolkit.Uwp.Notifications.nuspec | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index 4423ad40f2f..2ebf66dcd4d 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -1,7 +1,7 @@ - true + true @@ -12,8 +12,9 @@ - - + + + diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec index eae33f7a3dc..207251e3d46 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec @@ -24,7 +24,7 @@ - + @@ -49,6 +49,7 @@ + @@ -76,5 +77,9 @@ + + + + \ No newline at end of file From b454de11b0c82ae8621c58dc8f31fb5a66725b13 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Tue, 22 Dec 2020 17:59:20 -0800 Subject: [PATCH 06/10] Interop working on all but UWP 15063 and lower --- Directory.Build.targets | 3 ++- .../Microsoft.Toolkit.Uwp.Notifications.csproj | 6 ++++-- .../Microsoft.Toolkit.Uwp.Notifications.nuspec | 16 ++++++++-------- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/Directory.Build.targets b/Directory.Build.targets index 2ebf66dcd4d..2101233710b 100644 --- a/Directory.Build.targets +++ b/Directory.Build.targets @@ -5,7 +5,8 @@ - + + true $(MSBuildThisFileDirectory)toolkit.snk diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj index 2a456cf9275..7df3c50fda3 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj @@ -5,8 +5,6 @@ $(DefineConstants);NETFX_CORE Windows Community Toolkit Notifications true - 10240 - 10.0.10240.0 Microsoft.Toolkit.Uwp.Notifications.nuspec @@ -49,6 +47,8 @@ 10.0.19041.0 + 16299 + 10.0.16299.0 @@ -60,6 +60,8 @@ UAP,Version=v10.0 uap10.0 10.0.19041.0 + 10240 + 10.0.10240.0 $(DefineConstants);NETFX_CORE;WINDOWS_UWP;WINRT false .NETCore diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec index 207251e3d46..363c6d3fe16 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec @@ -69,17 +69,17 @@ - - - - + + + + - - - - + + + + \ No newline at end of file From 056e0be0c47c2ef40179b06af70207be4de87c98 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Wed, 23 Dec 2020 15:12:26 -0800 Subject: [PATCH 07/10] Fix warnings --- .../Compat/Desktop/DesktopBridgeHelpers.cs | 7 +- .../DesktopNotificationManagerCompat.cs | 64 ++----------------- .../Compat/Desktop/Native/NativeMethods.cs | 32 ++++++++++ .../Toasts/Compat/Desktop/Win32AppInfo.cs | 6 +- .../Compat/ToastNotificationManagerCompat.cs | 16 ++--- 5 files changed, 43 insertions(+), 82 deletions(-) create mode 100644 Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Native/NativeMethods.cs diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopBridgeHelpers.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopBridgeHelpers.cs index 6134db08869..500d0418cd2 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopBridgeHelpers.cs +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopBridgeHelpers.cs @@ -20,9 +20,6 @@ internal class DesktopBridgeHelpers { private const long APPMODEL_ERROR_NO_PACKAGE = 15700L; - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); - private static bool? _hasIdentity; public static bool HasIdentity() @@ -37,10 +34,10 @@ public static bool HasIdentity() { int length = 0; var sb = new StringBuilder(0); - GetCurrentPackageFullName(ref length, sb); + NativeMethods.GetCurrentPackageFullName(ref length, sb); sb = new StringBuilder(length); - int error = GetCurrentPackageFullName(ref length, sb); + int error = NativeMethods.GetCurrentPackageFullName(ref length, sb); _hasIdentity = error != APPMODEL_ERROR_NO_PACKAGE; } diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopNotificationManagerCompat.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopNotificationManagerCompat.cs index 38359aa56dc..164c50e8672 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopNotificationManagerCompat.cs +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/DesktopNotificationManagerCompat.cs @@ -50,7 +50,7 @@ public static void RegisterAumidAndComServer(string aumid) } // If running as Desktop Bridge - if (DesktopBridgeHelpers.IsRunningAsUwp()) + if (DesktopBridgeHelpers.HasIdentity()) { // Clear the AUMID since Desktop Bridge doesn't use it, and then we're done. // Desktop Bridge apps are registered with platform through their manifest. @@ -94,7 +94,7 @@ public static void RegisterActivator() // Big thanks to FrecherxDachs for figuring out the following code which works in .NET Core 3: https://github.com/FrecherxDachs/UwpNotificationNetCoreTest var uuid = typeof(T).GUID; uint cookie; - CoRegisterClassObject( + NativeMethods.CoRegisterClassObject( uuid, new NotificationActivatorClassFactory(), CLSCTX_LOCAL_SERVER, @@ -151,14 +151,6 @@ public int LockServer(bool fLock) } } - [DllImport("ole32.dll")] - private static extern int CoRegisterClassObject( - [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, - [MarshalAs(UnmanagedType.IUnknown)] object pUnk, - uint dwClsContext, - uint flags, - out uint lpdwRegister); - /// /// Creates a toast notifier. You must have called first (and also if you're a classic Win32 app), or this will throw an exception. /// @@ -198,7 +190,7 @@ private static void EnsureRegistered() if (!_registeredAumidAndComServer) { // Check if Desktop Bridge - if (DesktopBridgeHelpers.IsRunningAsUwp()) + if (DesktopBridgeHelpers.HasIdentity()) { // Implicitly registered, all good! _registeredAumidAndComServer = true; @@ -223,55 +215,7 @@ private static void EnsureRegistered() /// public static bool CanUseHttpImages { - get { return DesktopBridgeHelpers.IsRunningAsUwp(); } - } - - /// - /// Code from https://github.com/qmatteoq/DesktopBridgeHelpers/tree/master/DesktopBridge.Helpers/Helpers.cs - /// - private class DesktopBridgeHelpers - { - private const long APPMODEL_ERROR_NO_PACKAGE = 15700L; - - [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] - private static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); - - private static bool? _isRunningAsUwp; - - public static bool IsRunningAsUwp() - { - if (_isRunningAsUwp == null) - { - if (IsWindows7OrLower) - { - _isRunningAsUwp = false; - } - else - { - int length = 0; - var sb = new StringBuilder(0); - GetCurrentPackageFullName(ref length, sb); - - sb = new StringBuilder(length); - int error = GetCurrentPackageFullName(ref length, sb); - - _isRunningAsUwp = error != APPMODEL_ERROR_NO_PACKAGE; - } - } - - return _isRunningAsUwp.Value; - } - - private static bool IsWindows7OrLower - { - get - { - int versionMajor = Environment.OSVersion.Version.Major; - int versionMinor = Environment.OSVersion.Version.Minor; - double version = versionMajor + ((double)versionMinor / 10); - return version <= 6.1; - } - } + get { return DesktopBridgeHelpers.HasIdentity(); } } } } diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Native/NativeMethods.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Native/NativeMethods.cs new file mode 100644 index 00000000000..2de3b811bb7 --- /dev/null +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Native/NativeMethods.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +#if WIN32 + +using System; +using System.Runtime.InteropServices; +using System.Text; + +namespace Microsoft.Toolkit.Uwp.Notifications +{ + internal class NativeMethods + { + [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] + [return: MarshalAs(UnmanagedType.Bool)] + internal static extern bool DeleteObject([In] IntPtr hObject); + + [DllImport("ole32.dll")] + internal static extern int CoRegisterClassObject( + [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, + [MarshalAs(UnmanagedType.IUnknown)] object pUnk, + uint dwClsContext, + uint flags, + out uint lpdwRegister); + + [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)] + internal static extern int GetCurrentPackageFullName(ref int packageFullNameLength, StringBuilder packageFullName); + } +} + +#endif \ No newline at end of file diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Win32AppInfo.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Win32AppInfo.cs index 6dce097f717..081d793f4b8 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Win32AppInfo.cs +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/Desktop/Win32AppInfo.cs @@ -74,7 +74,7 @@ public static Win32AppInfo Get() { } - DeleteObject(nativeHBitmap); + NativeMethods.DeleteObject(nativeHBitmap); } } catch @@ -212,10 +212,6 @@ private static bool IsAlphaBitmap(Bitmap bmp, out BitmapData bmpData) return false; } - [DllImport("gdi32.dll", EntryPoint = "DeleteObject")] - [return: MarshalAs(UnmanagedType.Bool)] - private static extern bool DeleteObject([In] IntPtr hObject); - /// /// From https://stackoverflow.com/a/41622689/1454643 /// Generates Guid based on String. Key assumption for this algorithm is that name is unique (across where it it's being used) diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs index fd4f3929a23..cfeee70f5ca 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs @@ -55,7 +55,7 @@ public static event OnActivated OnActivated } catch (Exception ex) { - _initializeEx = ex; + _initializeEx = new InvalidOperationException("Failed to register notification activator", ex); } } @@ -104,7 +104,7 @@ internal static void OnActivatedInternal(string args, Internal.InternalNotificat private static string _win32Aumid; private static string _clsid; - private static Exception _initializeEx; + private static InvalidOperationException _initializeEx; static ToastNotificationManagerCompat() { @@ -115,7 +115,7 @@ static ToastNotificationManagerCompat() catch (Exception ex) { // We catch the exception so that things like subscribing to the event handler doesn't crash app - _initializeEx = ex; + _initializeEx = new InvalidOperationException("Failed initializing notifications", ex); } } @@ -239,7 +239,7 @@ private static void RegisterActivator(Type activatorType) // Big thanks to FrecherxDachs for figuring out the following code which works in .NET Core 3: https://github.com/FrecherxDachs/UwpNotificationNetCoreTest var uuid = activatorType.GUID; - CoRegisterClassObject( + NativeMethods.CoRegisterClassObject( uuid, new NotificationActivatorClassFactory(activatorType), CLSCTX_LOCAL_SERVER, @@ -319,14 +319,6 @@ public int LockServer(bool fLock) return S_OK; } } - - [DllImport("ole32.dll")] - private static extern int CoRegisterClassObject( - [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid, - [MarshalAs(UnmanagedType.IUnknown)] object pUnk, - uint dwClsContext, - uint flags, - out uint lpdwRegister); #endif /// From d40748849c05ed19534643f2cc18f517d295037f Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Thu, 7 Jan 2021 14:43:55 -0800 Subject: [PATCH 08/10] Support activating when process is elevated --- .../Compat/ToastNotificationManagerCompat.cs | 63 +++++++++++++++++-- 1 file changed, 57 insertions(+), 6 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs index cfeee70f5ca..f9dba5af387 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs @@ -250,12 +250,36 @@ private static void RegisterActivator(Type activatorType) private static void RegisterComServer(Type activatorType, string exePath) { // We register the EXE to start up when the notification is activated - string regString = string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}\\LocalServer32", activatorType.GUID); - var key = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(regString); + string regString = string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}", activatorType.GUID); + using (var key = Registry.CurrentUser.CreateSubKey(regString + "\\LocalServer32")) + { + // Include a flag so we know this was a toast activation and should wait for COM to process + // We also wrap EXE path in quotes for extra security + key.SetValue(null, '"' + exePath + '"' + " " + TOAST_ACTIVATED_LAUNCH_ARG); + } + + if (IsElevated) + { + //// For elevated apps, we need to ensure they'll activate in existing running process by adding + //// some values in local machine + using (var key = Registry.LocalMachine.CreateSubKey(regString)) + { + // Same as above, except also including AppId to link to our AppId entry below + using (var localServer32 = key.CreateSubKey("LocalServer32")) + { + localServer32.SetValue(null, '"' + exePath + '"' + " " + TOAST_ACTIVATED_LAUNCH_ARG); + } - // Include a flag so we know this was a toast activation and should wait for COM to process - // We also wrap EXE path in quotes for extra security - key.SetValue(null, '"' + exePath + '"' + " " + TOAST_ACTIVATED_LAUNCH_ARG); + key.SetValue("AppId", "{" + activatorType.GUID + "}"); + } + + // This tells COM to match any client, so Action Center will activate our elevated process. + // More info: https://docs.microsoft.com/windows/win32/com/runas + using (var key = Registry.LocalMachine.CreateSubKey(string.Format("SOFTWARE\\Classes\\AppID\\{{{0}}}", activatorType.GUID))) + { + key.SetValue("RunAs", "Interactive User"); + } + } } /// @@ -319,6 +343,14 @@ public int LockServer(bool fLock) return S_OK; } } + + private static bool IsElevated + { + get + { + return new System.Security.Principal.WindowsPrincipal(System.Security.Principal.WindowsIdentity.GetCurrent()).IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator); + } + } #endif /// @@ -440,7 +472,26 @@ public static void Uninstall() { if (_clsid != null) { - Registry.CurrentUser.DeleteSubKey(string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}\\LocalServer32", _clsid)); + try + { + Registry.CurrentUser.DeleteSubKeyTree(string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}", _clsid)); + } + catch { } + + if (IsElevated) + { + try + { + Registry.LocalMachine.DeleteSubKeyTree(string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}", _clsid)); + } + catch { } + + try + { + Registry.LocalMachine.DeleteSubKeyTree(string.Format("SOFTWARE\\Classes\\AppID\\{{{0}}}", _clsid)); + } + catch { } + } } } catch From c23b5df6dddd78e76474b3e69d68360ac5038ec7 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Tue, 12 Jan 2021 10:29:42 -0800 Subject: [PATCH 09/10] Fix warnings --- .../Toasts/Compat/ToastNotificationManagerCompat.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs index f9dba5af387..be6917046b8 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs +++ b/Microsoft.Toolkit.Uwp.Notifications/Toasts/Compat/ToastNotificationManagerCompat.cs @@ -476,7 +476,9 @@ public static void Uninstall() { Registry.CurrentUser.DeleteSubKeyTree(string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}", _clsid)); } - catch { } + catch + { + } if (IsElevated) { @@ -484,13 +486,17 @@ public static void Uninstall() { Registry.LocalMachine.DeleteSubKeyTree(string.Format("SOFTWARE\\Classes\\CLSID\\{{{0}}}", _clsid)); } - catch { } + catch + { + } try { Registry.LocalMachine.DeleteSubKeyTree(string.Format("SOFTWARE\\Classes\\AppID\\{{{0}}}", _clsid)); } - catch { } + catch + { + } } } } From d1518bd94d0c5e790cfe97c269776f7583baf5a0 Mon Sep 17 00:00:00 2001 From: Andrew Leader Date: Fri, 22 Jan 2021 09:54:11 -0800 Subject: [PATCH 10/10] Nuspec updates --- .../Microsoft.Toolkit.Uwp.Notifications.csproj | 1 - .../Microsoft.Toolkit.Uwp.Notifications.nuspec | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj index 7df3c50fda3..c980f7c4205 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.csproj @@ -3,7 +3,6 @@ netstandard1.4;uap10.0.19041;net461;netcoreapp3.1;net5.0;net5.0-windows10.0.17763.0;native $(DefineConstants);NETFX_CORE - Windows Community Toolkit Notifications true Microsoft.Toolkit.Uwp.Notifications.nuspec diff --git a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec index 363c6d3fe16..7778144f3cb 100644 --- a/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec +++ b/Microsoft.Toolkit.Uwp.Notifications/Microsoft.Toolkit.Uwp.Notifications.nuspec @@ -4,13 +4,12 @@ Microsoft.Toolkit.Uwp.Notifications $version$ Windows Community Toolkit Notifications - Microsoft.Toolkit - + Microsoft.Toolkit,dotnetfoundation true https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/license.md https://github.com/windows-toolkit/WindowsCommunityToolkit https://raw.githubusercontent.com/windows-toolkit/WindowsCommunityToolkit/master/build/nuget.png - The official way to send toast notifications on Windows 10 via code rather than XML, with the help of IntelliSense. Supported by all C# app types, including WPF, UWP, WinForms, and Console, even without packaging your app as MSIX. Also supported by C++ UWP apps. + The official way to send toast notifications on Windows 10 via code rather than XML, with the help of IntelliSense. Supports all C# app types, including WPF, UWP, WinForms, and Console, even without packaging your app as MSIX. Also supports C++ UWP apps. Additionally, generate notification payloads from your ASP.NET web server to send as push notifications, or generate notification payloads from class libraries.