Skip to content

Commit 41de124

Browse files
committed
feat(notifications): make notifications clickable to bring app to foreground
1 parent ac74485 commit 41de124

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/DevCLT.WindowsApp/App.xaml.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using DevCLT.Infrastructure.Data;
66
using DevCLT.WindowsApp.Services;
77
using DevCLT.WindowsApp.ViewModels;
8+
using Microsoft.Toolkit.Uwp.Notifications;
89

910
namespace DevCLT.WindowsApp;
1011

@@ -44,6 +45,27 @@ protected override async void OnStartup(StartupEventArgs e)
4445
var window = new MainWindow();
4546
window.Initialize(mainVM);
4647
window.Show();
48+
49+
// Handle Notification Clicks
50+
ToastNotificationManagerCompat.OnActivated += toastArgs =>
51+
{
52+
// Runs on a background thread
53+
Application.Current.Dispatcher.Invoke(delegate
54+
{
55+
var win = Application.Current.MainWindow;
56+
if (win != null)
57+
{
58+
if (win.WindowState == WindowState.Minimized)
59+
win.WindowState = WindowState.Normal;
60+
61+
win.Show();
62+
win.Activate();
63+
win.Topmost = true; // Hack to bring to front
64+
win.Topmost = false;
65+
win.Focus();
66+
}
67+
});
68+
};
4769
}
4870
catch (Exception ex)
4971
{

src/DevCLT.WindowsApp/Services/WindowsNotifier.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public void NotifyBreakEnded()
1010
try
1111
{
1212
new ToastContentBuilder()
13+
.AddArgument("action", "viewApp")
1314
.AddText("Pausa concluída")
1415
.AddText("Clique para retomar o trabalho")
1516
.Show();
@@ -25,6 +26,7 @@ public void NotifyWorkCompleted()
2526
try
2627
{
2728
new ToastContentBuilder()
29+
.AddArgument("action", "viewApp")
2830
.AddText("Jornada concluída! 🎉")
2931
.AddText("Você completou suas horas de trabalho.")
3032
.Show();
@@ -37,6 +39,7 @@ public void NotifyOvertime(TimeSpan elapsed)
3739
try
3840
{
3941
new ToastContentBuilder()
42+
.AddArgument("action", "viewApp")
4043
.AddText("Hora extra em andamento")
4144
.AddText($"Já são {elapsed:hh\\:mm\\:ss} de hora extra.")
4245
.Show();

0 commit comments

Comments
 (0)