Skip to content

Commit 3f12ead

Browse files
loneursidEric Langlois
andauthored
Sample: App Notifications C++ WinUI Unpackaged (#177)
* Initial empty sample * Naming the sample to better express intent and differenciate from other samples * Removing unused scenario and properly naming the initial scenario * show toast working with hand made package * Moved initialization to windowsmain * re-activating bootstrap * Adding windows 10+ compat * Applying learnings from asklar * Update the latest experimental package * Updating with latest experiments * Updating to latest SDK * Set message according to design doc * wip * Trying independent scenarios * Easier to get routing right with an extra scenario * Retrieving input text from toast * Moving init code to Onlaunch * getting activation when not running * Moving dialog boxes to their respective scenarios * Adding launch debug support * Adding a packaged project for testing only * fixing icon issue * wip * Updating to preview1 rc1 * Code cleanup * Couldn't find a better place to call Unregister() * List app notification activation arguments * Let's hack it for now * Upon receiving a toast, navigate to the originator's page * Route activation args to the right scenario * Adding avatar image to toasts * co-locationg code by scenarios * Grouping duplicated code into helper functions * good enough * Removing old prototypes not intended to ship * Sample depends on bug fixes in latest WInAppSDK package * Adding link to ref docs * better linking with the onecore libs * Refactor to support detecting background activation early on * Background experiment * Updating to WAS preview2 * improve manage life time of registration * A listview is better suited at storing notifications and keeping some form of history * Visual improvments * Adding toast user input to listview * code cleanup * Co-locating notification management code to same class * Only util functions left are a better for for the App class * generalize notification routing * Creating readme * file was a mistake * Retrieving the activation args should really happen in onLaunch * Removing dead code * Use the actual action name instead of a made up one * De-duplicating code * Notes * Ensure all files have a copyright message * Hack not needed anymore as the SDK takes care of things now * removing dead code * Updating doc * Updating SDK to Preview3 * Scenarios descriptions were flipped * PR Feedback * Code cleanup * Doc update * Doc update * Group Notification code into same folder * handle when user click background of a toast * Bring app to foreground when a toast has been received * Add launch arguments Co-authored-by: Eric Langlois <[email protected]>
1 parent df6d56a commit 3f12ead

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1968
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32210.238
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "CppUnpackagedAppNotifications", "CppUnpackagedAppNotifications\CppUnpackagedAppNotifications.vcxproj", "{BA5623DE-515A-4D7E-9110-38D1641F5FE0}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|arm64 = Debug|arm64
11+
Debug|x64 = Debug|x64
12+
Debug|x86 = Debug|x86
13+
Release|arm64 = Release|arm64
14+
Release|x64 = Release|x64
15+
Release|x86 = Release|x86
16+
EndGlobalSection
17+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|arm64.ActiveCfg = Debug|arm64
19+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|arm64.Build.0 = Debug|arm64
20+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|arm64.Deploy.0 = Debug|arm64
21+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|x64.ActiveCfg = Debug|x64
22+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|x64.Build.0 = Debug|x64
23+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|x64.Deploy.0 = Debug|x64
24+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|x86.ActiveCfg = Debug|Win32
25+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|x86.Build.0 = Debug|Win32
26+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Debug|x86.Deploy.0 = Debug|Win32
27+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|arm64.ActiveCfg = Release|arm64
28+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|arm64.Build.0 = Release|arm64
29+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|arm64.Deploy.0 = Release|arm64
30+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|x64.ActiveCfg = Release|x64
31+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|x64.Build.0 = Release|x64
32+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|x64.Deploy.0 = Release|x64
33+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|x86.ActiveCfg = Release|Win32
34+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|x86.Build.0 = Release|Win32
35+
{BA5623DE-515A-4D7E-9110-38D1641F5FE0}.Release|x86.Deploy.0 = Release|Win32
36+
EndGlobalSection
37+
GlobalSection(SolutionProperties) = preSolution
38+
HideSolutionNode = FALSE
39+
EndGlobalSection
40+
GlobalSection(ExtensibilityGlobals) = postSolution
41+
SolutionGuid = {5E99866A-9228-40FB-AC77-C637CA706017}
42+
EndGlobalSection
43+
EndGlobal
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<!-- Copyright (c) Microsoft Corporation.
2+
Licensed under the MIT License. -->
3+
<Application
4+
x:Class="CppUnpackagedAppNotifications.App"
5+
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
6+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
7+
xmlns:local="using:CppUnpackagedAppNotifications">
8+
9+
<Application.Resources>
10+
<!-- Application-specific resources -->
11+
<ResourceDictionary>
12+
<ResourceDictionary.MergedDictionaries>
13+
<!--
14+
Styles that define common aspects of the platform look and feel
15+
Required by Visual Studio project and item templates
16+
-->
17+
<ResourceDictionary Source="ms-appx:///Styles.xaml"/>
18+
</ResourceDictionary.MergedDictionaries>
19+
</ResourceDictionary>
20+
</Application.Resources>
21+
</Application>
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#include "pch.h"
5+
#include "App.xaml.h"
6+
#include "MainWindow.xaml.h"
7+
#include "Notifications\NotificationManager.h"
8+
#include <wil/result.h>
9+
#include <Microsoft.UI.Xaml.Window.h>
10+
#include <winrt/Microsoft.Windows.AppLifecycle.h>
11+
#include <winrt/Microsoft.Windows.AppNotifications.h>
12+
13+
namespace winrt
14+
{
15+
using namespace Windows::Foundation;
16+
using namespace Microsoft::UI::Xaml;
17+
using namespace winrt::Microsoft::Windows::AppLifecycle;
18+
using namespace winrt::Microsoft::Windows::AppNotifications;
19+
}
20+
21+
// NotificationManager is responsible for registering and unregistering the Sample for App Notifications as well as
22+
// dispatching actionned notifications to the appropriate scenario.
23+
// Registration will happen when Init() is called and Unregistration will happen when this
24+
// instance variable goes out of scope, i.e.: when the App is terminated.
25+
static NotificationManager g_notificationManager;
26+
27+
namespace winrt::CppUnpackagedAppNotifications::implementation
28+
{
29+
static App* app{ nullptr };
30+
31+
App::App()
32+
{
33+
InitializeComponent();
34+
app = this;
35+
36+
#if defined _DEBUG && !defined DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
37+
UnhandledException([](winrt::IInspectable const&, winrt::UnhandledExceptionEventArgs const& e)
38+
{
39+
if (IsDebuggerPresent())
40+
{
41+
auto errorMessage = e.Message();
42+
__debugbreak();
43+
}
44+
});
45+
#endif
46+
}
47+
48+
void App::ToForeground()
49+
{
50+
assert(app != nullptr);
51+
52+
HWND hwnd;
53+
auto WindowNative{ app->window.as<IWindowNative>() };
54+
if (WindowNative != nullptr && WindowNative->get_WindowHandle(&hwnd) == S_OK)
55+
{
56+
SwitchToThisWindow(hwnd, TRUE);
57+
}
58+
}
59+
60+
std::wstring App::GetFullPathToExe()
61+
{
62+
TCHAR buffer[MAX_PATH] = { 0 };
63+
GetModuleFileName(NULL, buffer, MAX_PATH);
64+
std::wstring::size_type pos = std::wstring(buffer).find_last_of(L"\\/");
65+
return std::wstring(buffer).substr(0, pos);
66+
}
67+
68+
std::wstring App::GetFullPathToAsset(std::wstring const& assetName)
69+
{
70+
return GetFullPathToExe() + L"\\Assets\\" + assetName;
71+
}
72+
73+
void App::OnLaunched(winrt::Microsoft::UI::Xaml::LaunchActivatedEventArgs const& /*args*/)
74+
{
75+
window = make<MainWindow>();
76+
77+
g_notificationManager.Init();
78+
79+
// NOTE: AppInstance is ambiguous between
80+
// Microsoft.Windows.AppLifecycle.AppInstance and
81+
// Windows.ApplicationModel.AppInstance
82+
auto currentInstance{ winrt::AppInstance::GetCurrent() };
83+
if (currentInstance)
84+
{
85+
// AppInstance.GetActivatedEventArgs will report the correct ActivationKind,
86+
// even in WinUI's OnLaunched.
87+
winrt::AppActivationArguments activationArgs{ currentInstance.GetActivatedEventArgs() };
88+
if (activationArgs)
89+
{
90+
winrt::ExtendedActivationKind extendedKind{ activationArgs.Kind() };
91+
if (extendedKind == winrt::Microsoft::Windows::AppLifecycle::ExtendedActivationKind::AppNotification)
92+
{
93+
winrt::AppNotificationActivatedEventArgs notificationActivatedEventArgs{ activationArgs.Data().as<winrt::AppNotificationActivatedEventArgs>() };
94+
g_notificationManager.ProcessLaunchActivationArgs(notificationActivatedEventArgs);
95+
}
96+
}
97+
}
98+
99+
window.Activate();
100+
}
101+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
#pragma once
5+
#include "App.xaml.g.h"
6+
#include "pch.h"
7+
8+
namespace winrt::CppUnpackagedAppNotifications::implementation
9+
{
10+
struct App : AppT<App>
11+
{
12+
App();
13+
14+
static std::wstring GetFullPathToExe();
15+
static std::wstring GetFullPathToAsset(std::wstring const& assetName);
16+
static void ToForeground();
17+
18+
void OnLaunched(Microsoft::UI::Xaml::LaunchActivatedEventArgs const&);
19+
20+
private:
21+
Microsoft::UI::Xaml::Window window{ nullptr };
22+
};
23+
}
76.3 KB
Loading
10.6 KB
Loading
2.73 KB
Loading
10.9 KB
Loading
3.06 KB
Loading

0 commit comments

Comments
 (0)