File tree Expand file tree Collapse file tree 9 files changed +14
-26
lines changed
Samples/Notifications/App/CppUnpackagedAppNotifications/CppUnpackagedAppNotifications Expand file tree Collapse file tree 9 files changed +14
-26
lines changed Original file line number Diff line number Diff line change @@ -26,6 +26,7 @@ This repository hosts samples for the [Windows App SDK](https://github.com/micro
26
26
27
27
#### Notifications
28
28
- [ 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.
29
30
30
31
#### Runtime Components
31
32
- [ Custom Controls] ( Samples/CustomControls ) : This sample shows how to author a Windows Runtime Component in C# with custom WinUI controls.
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ namespace winrt
19
19
}
20
20
21
21
// 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.
23
23
// Registration will happen when Init() is called and Unregistration will happen when this
24
24
// instance variable goes out of scope, i.e.: when the App is terminated.
25
25
static NotificationManager g_notificationManager;
@@ -50,8 +50,8 @@ namespace winrt::CppUnpackagedAppNotifications::implementation
50
50
assert (app != nullptr );
51
51
52
52
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)) )
55
55
{
56
56
SwitchToThisWindow (hwnd, TRUE );
57
57
}
Original file line number Diff line number Diff line change @@ -46,7 +46,6 @@ namespace winrt::CppUnpackagedAppNotifications::implementation
46
46
else
47
47
{
48
48
text.append (L" \t - Input received: " + notification.Input );
49
-
50
49
}
51
50
}
52
51
messages.InsertAt (0 , PropertyValue::CreateString (text));
Original file line number Diff line number Diff line change @@ -28,7 +28,7 @@ std::wstring Common::MakeScenarioIdToken(unsigned id)
28
28
return std::wstring (scenarioTag) + L" =" + std::to_wstring (id);
29
29
}
30
30
31
- std::optional <unsigned > Common::ExtractScenarioIdFromArgs (std::wstring const & args)
31
+ std::optional<unsigned > Common::ExtractScenarioIdFromArgs (std::wstring const & args)
32
32
{
33
33
auto scenarioId{ ExtractParamFromArgs (args, scenarioTag) };
34
34
Original file line number Diff line number Diff line change @@ -8,6 +8,6 @@ struct Common
8
8
static std::optional<std::wstring> ExtractParamFromArgs (std::wstring const & args, std::wstring const & paramName);
9
9
10
10
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 );
12
12
};
13
13
Original file line number Diff line number Diff line change @@ -41,7 +41,9 @@ void NotificationManager::Init()
41
41
{
42
42
auto notificationManager{ winrt::AppNotificationManager::Default () };
43
43
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.
45
47
const auto token{ notificationManager.NotificationInvoked ([&](const auto &, winrt::AppNotificationActivatedEventArgs const & notificationActivatedEventArgs)
46
48
{
47
49
NotifyUser::NotificationReceived ();
@@ -58,8 +60,6 @@ void NotificationManager::Init()
58
60
59
61
void NotificationManager::ProcessLaunchActivationArgs (winrt::AppNotificationActivatedEventArgs const & notificationActivatedEventArgs)
60
62
{
61
- assert (m_isRegistered);
62
-
63
63
DispatchNotification (notificationActivatedEventArgs);
64
64
NotifyUser::AppLaunchedFromNotification ();
65
65
}
Original file line number Diff line number Diff line change @@ -38,14 +38,9 @@ bool ToastWithAvatar::SendToast()
38
38
</toast>" };
39
39
40
40
auto toast{ winrt::AppNotification (xmlPayload) };
41
- toast.Priority (winrt::AppNotificationPriority::High);
42
41
winrt::AppNotificationManager::Default ().Show (toast);
43
- if (toast.Id () == 0 )
44
- {
45
- return false ;
46
- }
47
42
48
- return true ;
43
+ return toast. Id () != 0 ; // return true (indicating success) if the toast was sent (if it has an Id)
49
44
}
50
45
51
46
void ToastWithAvatar::NotificationReceived (winrt::Microsoft::Windows::AppNotifications::AppNotificationActivatedEventArgs const & notificationActivatedEventArgs)
@@ -56,5 +51,4 @@ void ToastWithAvatar::NotificationReceived(winrt::Microsoft::Windows::AppNotific
56
51
notification.Action = action.has_value () ? action.value () : L" " ;
57
52
winrt::MainPage::Current ().NotificationReceived (notification);
58
53
winrt::App::ToForeground ();
59
-
60
54
}
Original file line number Diff line number Diff line change @@ -44,21 +44,16 @@ bool ToastWithTextBox::SendToast()
44
44
</toast>" };
45
45
46
46
auto toast{ winrt::AppNotification (xmlPayload) };
47
- toast.Priority (winrt::AppNotificationPriority::High);
48
47
winrt::AppNotificationManager::Default ().Show (toast);
49
- if (toast.Id () == 0 )
50
- {
51
- return false ;
52
- }
53
48
54
- return true ;
49
+ return toast. Id () != 0 ; // return true (indicating success) if the toast was sent (if it has an Id)
55
50
}
56
51
57
52
void ToastWithTextBox::NotificationReceived (winrt::Microsoft::Windows::AppNotifications::AppNotificationActivatedEventArgs const & notificationActivatedEventArgs)
58
53
{
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
62
57
// the payload is routed internally
63
58
64
59
auto input{ notificationActivatedEventArgs.UserInput () };
Original file line number Diff line number Diff line change @@ -22,7 +22,6 @@ void NotifyUser::ToastSentSuccessfully()
22
22
void NotifyUser::AppLaunchedFromNotification ()
23
23
{
24
24
winrt::MainPage::Current ().NotifyUser (L" App launched from notifications" , winrt::InfoBarSeverity::Informational);
25
-
26
25
}
27
26
28
27
void NotifyUser::NotificationReceived ()
You can’t perform that action at this time.
0 commit comments