Skip to content

Feature: Show notification only when app crashes #10873

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 29, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 32 additions & 31 deletions src/Files.App/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -337,13 +337,13 @@ public static void SaveSessionTabs() // Enumerates through all tabs and gets the
}

// Occurs when an exception is not handled on the UI thread.
private static void OnUnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) => AppUnhandledException(e.Exception);
private static void OnUnhandledException(object sender, Microsoft.UI.Xaml.UnhandledExceptionEventArgs e) => AppUnhandledException(e.Exception, true);

// Occurs when an exception is not handled on a background thread.
// ie. A task is fired and forgotten Task.Run(() => {...})
private static void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e) => AppUnhandledException(e.Exception);
private static void OnUnobservedException(object sender, UnobservedTaskExceptionEventArgs e) => AppUnhandledException(e.Exception, false);

private static void AppUnhandledException(Exception ex)
private static void AppUnhandledException(Exception ex, bool shouldShowNotification)
{
StringBuilder formattedException = new StringBuilder() { Capacity = 200 };

Expand Down Expand Up @@ -385,49 +385,50 @@ private static void AppUnhandledException(Exception ex)

SaveSessionTabs();
Logger.UnhandledError(ex, ex.Message);
if (ShowErrorNotification)

if (!ShowErrorNotification || !shouldShowNotification)
return;

var toastContent = new ToastContent()
{
var toastContent = new ToastContent()
Visual = new ToastVisual()
{
Visual = new ToastVisual()
BindingGeneric = new ToastBindingGeneric()
{
BindingGeneric = new ToastBindingGeneric()
Children =
{
Children =
new AdaptiveText()
{
new AdaptiveText()
{
Text = "ExceptionNotificationHeader".GetLocalizedResource()
},
new AdaptiveText()
{
Text = "ExceptionNotificationBody".GetLocalizedResource()
}
Text = "ExceptionNotificationHeader".GetLocalizedResource()
},
AppLogoOverride = new ToastGenericAppLogo()
new AdaptiveText()
{
Source = "ms-appx:///Assets/error.png"
Text = "ExceptionNotificationBody".GetLocalizedResource()
}
},
AppLogoOverride = new ToastGenericAppLogo()
{
Source = "ms-appx:///Assets/error.png"
}
},
Actions = new ToastActionsCustom()
}
},
Actions = new ToastActionsCustom()
{
Buttons =
{
Buttons =
new ToastButton("ExceptionNotificationReportButton".GetLocalizedResource(), "report")
{
new ToastButton("ExceptionNotificationReportButton".GetLocalizedResource(), "report")
{
ActivationType = ToastActivationType.Foreground
}
ActivationType = ToastActivationType.Foreground
}
}
};
}
};

// Create the toast notification
var toastNotif = new ToastNotification(toastContent.GetXml());
// Create the toast notification
var toastNotif = new ToastNotification(toastContent.GetXml());

// And send the notification
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
}
// And send the notification
ToastNotificationManager.CreateToastNotifier().Show(toastNotif);
}

public static void CloseApp()
Expand Down