Skip to content

Commit 547a928

Browse files
loneursidEric Langlois
andauthored
Addressing bunch of small nits in AppNotifications sample (#217)
* nits * Vaheeshta's words are more beautifuller than mine * Adding App Notifications to readme * nits from PR feedback Co-authored-by: Eric Langlois <[email protected]>
1 parent bbbdcf3 commit 547a928

File tree

9 files changed

+14
-26
lines changed

9 files changed

+14
-26
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ This repository hosts samples for the [Windows App SDK](https://github.com/micro
2626

2727
#### Notifications
2828
- [Push Notifications](Samples/Notifications/Push): This is a sample app that showcases Push Notifications.
29+
- [App Notifications](Samples/Notifications/App): This is a sample app that showcases App Notifications.
2930

3031
#### Runtime Components
3132
- [Custom Controls](Samples/CustomControls): This sample shows how to author a Windows Runtime Component in C# with custom WinUI controls.

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/App.xaml.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace winrt
1919
}
2020

2121
// NotificationManager is responsible for registering and unregistering the Sample for App Notifications as well as
22-
// dispatching actionned notifications to the appropriate scenario.
22+
// dispatching actioned notifications to the appropriate scenario.
2323
// Registration will happen when Init() is called and Unregistration will happen when this
2424
// instance variable goes out of scope, i.e.: when the App is terminated.
2525
static NotificationManager g_notificationManager;
@@ -50,8 +50,8 @@ namespace winrt::CppUnpackagedAppNotifications::implementation
5050
assert(app != nullptr);
5151

5252
HWND hwnd;
53-
auto WindowNative{ app->window.as<IWindowNative>() };
54-
if (WindowNative != nullptr && WindowNative->get_WindowHandle(&hwnd) == S_OK)
53+
auto windowNative{ app->window.as<IWindowNative>() };
54+
if (windowNative && SUCCEEDED(windowNative->get_WindowHandle(&hwnd)))
5555
{
5656
SwitchToThisWindow(hwnd, TRUE);
5757
}

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/MainPage.xaml.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,6 @@ namespace winrt::CppUnpackagedAppNotifications::implementation
4646
else
4747
{
4848
text.append(L"\t- Input received: " + notification.Input);
49-
5049
}
5150
}
5251
messages.InsertAt(0, PropertyValue::CreateString(text));

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/Notifications/Common.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ std::wstring Common::MakeScenarioIdToken(unsigned id)
2828
return std::wstring(scenarioTag) + L"=" + std::to_wstring(id);
2929
}
3030

31-
std::optional <unsigned> Common::ExtractScenarioIdFromArgs(std::wstring const& args)
31+
std::optional<unsigned> Common::ExtractScenarioIdFromArgs(std::wstring const& args)
3232
{
3333
auto scenarioId{ ExtractParamFromArgs(args, scenarioTag) };
3434

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/Notifications/Common.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ struct Common
88
static std::optional<std::wstring> ExtractParamFromArgs(std::wstring const& args, std::wstring const& paramName);
99

1010
static std::wstring MakeScenarioIdToken(unsigned Id);
11-
static std::optional <unsigned> ExtractScenarioIdFromArgs(std::wstring const& string);
11+
static std::optional<unsigned> ExtractScenarioIdFromArgs(std::wstring const& args);
1212
};
1313

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/Notifications/NotificationManager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@ void NotificationManager::Init()
4141
{
4242
auto notificationManager{ winrt::AppNotificationManager::Default() };
4343

44-
// Always setup the notification hanlder before registering your App, otherwise notifications may get lost.
44+
// To ensure all Notification handling happens in this process instance, register for
45+
// NotificationInvoked before calling Register(). Without this a new process will
46+
// be launched to handle the notification.
4547
const auto token{ notificationManager.NotificationInvoked([&](const auto&, winrt::AppNotificationActivatedEventArgs const& notificationActivatedEventArgs)
4648
{
4749
NotifyUser::NotificationReceived();
@@ -58,8 +60,6 @@ void NotificationManager::Init()
5860

5961
void NotificationManager::ProcessLaunchActivationArgs(winrt::AppNotificationActivatedEventArgs const& notificationActivatedEventArgs)
6062
{
61-
assert(m_isRegistered);
62-
6363
DispatchNotification(notificationActivatedEventArgs);
6464
NotifyUser::AppLaunchedFromNotification();
6565
}

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/Notifications/ToastWithAvatar.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,9 @@ bool ToastWithAvatar::SendToast()
3838
</toast>" };
3939

4040
auto toast{ winrt::AppNotification(xmlPayload) };
41-
toast.Priority(winrt::AppNotificationPriority::High);
4241
winrt::AppNotificationManager::Default().Show(toast);
43-
if (toast.Id() == 0)
44-
{
45-
return false;
46-
}
4742

48-
return true;
43+
return toast.Id() != 0; // return true (indicating success) if the toast was sent (if it has an Id)
4944
}
5045

5146
void ToastWithAvatar::NotificationReceived(winrt::Microsoft::Windows::AppNotifications::AppNotificationActivatedEventArgs const& notificationActivatedEventArgs)
@@ -56,5 +51,4 @@ void ToastWithAvatar::NotificationReceived(winrt::Microsoft::Windows::AppNotific
5651
notification.Action = action.has_value() ? action.value() : L"";
5752
winrt::MainPage::Current().NotificationReceived(notification);
5853
winrt::App::ToForeground();
59-
6054
}

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/Notifications/ToastWithTextBox.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,16 @@ bool ToastWithTextBox::SendToast()
4444
</toast>" };
4545

4646
auto toast{ winrt::AppNotification(xmlPayload) };
47-
toast.Priority(winrt::AppNotificationPriority::High);
4847
winrt::AppNotificationManager::Default().Show(toast);
49-
if (toast.Id() == 0)
50-
{
51-
return false;
52-
}
5348

54-
return true;
49+
return toast.Id() != 0; // return true (indicating success) if the toast was sent (if it has an Id)
5550
}
5651

5752
void ToastWithTextBox::NotificationReceived(winrt::Microsoft::Windows::AppNotifications::AppNotificationActivatedEventArgs const& notificationActivatedEventArgs)
5853
{
59-
// In a real life scenario, this type of action would usually be processed in the background. Your App would process the payload in
60-
// the brackground (sending the payload back to your App Server) without ever showing the App's UI.
61-
// This is no not something that can easily be demonstrated in a sample such as this one, as we need to show the UI to demonstrate how
54+
// In a real-life scenario, this type of action would usually be processed in the background. Your App would process the payload in
55+
// the background (sending the payload back to your App Server) without ever showing the App's UI.
56+
// This is not something that can easily be demonstrated in a sample such as this one, as we need to show the UI to demonstrate how
6257
// the payload is routed internally
6358

6459
auto input{ notificationActivatedEventArgs.UserInput() };

Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications/NotifyUser.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ void NotifyUser::ToastSentSuccessfully()
2222
void NotifyUser::AppLaunchedFromNotification()
2323
{
2424
winrt::MainPage::Current().NotifyUser(L"App launched from notifications", winrt::InfoBarSeverity::Informational);
25-
2625
}
2726

2827
void NotifyUser::NotificationReceived()

0 commit comments

Comments
 (0)