Route mail/calendar toasts to their respective app entries (#821)
This commit is contained in:
@@ -136,10 +136,8 @@ public partial class App : WinoApplication,
|
|||||||
{
|
{
|
||||||
base.OnLaunched(args);
|
base.OnLaunched(args);
|
||||||
|
|
||||||
if (ShouldRegisterAppNotifications(args))
|
// Always register notification callbacks for both app entries (Mail and Calendar).
|
||||||
{
|
TryRegisterAppNotifications();
|
||||||
TryRegisterAppNotifications();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize required services regardless of launch activation type.
|
// Initialize required services regardless of launch activation type.
|
||||||
// All activation scenarios require these services to be ready.
|
// All activation scenarios require these services to be ready.
|
||||||
@@ -190,28 +188,6 @@ public partial class App : WinoApplication,
|
|||||||
private async void AppNotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args)
|
private async void AppNotificationInvoked(AppNotificationManager sender, AppNotificationActivatedEventArgs args)
|
||||||
=> await HandleToastActivationAsync(args);
|
=> await HandleToastActivationAsync(args);
|
||||||
|
|
||||||
private bool ShouldRegisterAppNotifications(Microsoft.UI.Xaml.LaunchActivatedEventArgs? args)
|
|
||||||
{
|
|
||||||
var activationArgs = AppInstance.GetCurrent().GetActivatedEventArgs();
|
|
||||||
|
|
||||||
// Always allow registration when activated from a toast.
|
|
||||||
if (activationArgs.Kind == ExtendedActivationKind.AppNotification)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
var launchMode = AppModeActivationResolver.Resolve(args?.Arguments,
|
|
||||||
GetCurrentLaunchTileId(),
|
|
||||||
Environment.CommandLine,
|
|
||||||
_preferencesService?.DefaultApplicationMode ?? WinoApplicationMode.Mail);
|
|
||||||
bool shouldRegister = launchMode == WinoApplicationMode.Mail;
|
|
||||||
|
|
||||||
if (!shouldRegister)
|
|
||||||
{
|
|
||||||
LogActivation("Skipping app notification registration for non-mail launch mode.");
|
|
||||||
}
|
|
||||||
|
|
||||||
return shouldRegister;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void TryRegisterAppNotifications()
|
private void TryRegisterAppNotifications()
|
||||||
{
|
{
|
||||||
var notificationManager = AppNotificationManager.Default;
|
var notificationManager = AppNotificationManager.Default;
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ using System.Threading.Tasks;
|
|||||||
using CommunityToolkit.Mvvm.Messaging;
|
using CommunityToolkit.Mvvm.Messaging;
|
||||||
using Microsoft.Toolkit.Uwp.Notifications;
|
using Microsoft.Toolkit.Uwp.Notifications;
|
||||||
using Serilog;
|
using Serilog;
|
||||||
|
using Windows.ApplicationModel;
|
||||||
using Windows.Data.Xml.Dom;
|
using Windows.Data.Xml.Dom;
|
||||||
using Windows.UI.Notifications;
|
using Windows.UI.Notifications;
|
||||||
using Wino.Core.Domain.Entities.Calendar;
|
using Wino.Core.Domain.Entities.Calendar;
|
||||||
@@ -19,6 +20,9 @@ namespace Wino.Mail.WinUI.Services;
|
|||||||
|
|
||||||
public class NotificationBuilder : INotificationBuilder
|
public class NotificationBuilder : INotificationBuilder
|
||||||
{
|
{
|
||||||
|
private const string MailApplicationId = "App";
|
||||||
|
private const string CalendarApplicationId = "CalendarApp";
|
||||||
|
|
||||||
private readonly IAccountService _accountService;
|
private readonly IAccountService _accountService;
|
||||||
private readonly IFolderService _folderService;
|
private readonly IFolderService _folderService;
|
||||||
private readonly IMailService _mailService;
|
private readonly IMailService _mailService;
|
||||||
@@ -93,7 +97,7 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
Src = new Uri("ms-winsoundevent:Notification.Mail")
|
Src = new Uri("ms-winsoundevent:Notification.Mail")
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Show();
|
ShowToast(builder, ToastTargetApp.Mail);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
@@ -148,7 +152,7 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Use UniqueId as tag to allow removal
|
// Use UniqueId as tag to allow removal
|
||||||
builder.Show(toast => toast.Tag = mailItem.UniqueId.ToString());
|
ShowToast(builder, ToastTargetApp.Mail, mailItem.UniqueId.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
private ToastButton GetDismissButton()
|
private ToastButton GetDismissButton()
|
||||||
@@ -234,7 +238,7 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
ToastNotificationManager.History.Remove(mailUniqueId.ToString());
|
ToastNotificationManager.History.Remove(mailUniqueId.ToString(), null, GetAppUserModelId(ToastTargetApp.Mail));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
@@ -255,7 +259,7 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
builder.AddArgument(Constants.ToastMailAccountIdKey, account.Id.ToString());
|
builder.AddArgument(Constants.ToastMailAccountIdKey, account.Id.ToString());
|
||||||
builder.AddArgument(Constants.ToastModeKey, Constants.ToastModeMail);
|
builder.AddArgument(Constants.ToastModeKey, Constants.ToastModeMail);
|
||||||
builder.AddButton(new ToastButton().SetContent(Translator.Buttons_FixAccount));
|
builder.AddButton(new ToastButton().SetContent(Translator.Buttons_FixAccount));
|
||||||
builder.Show();
|
ShowToast(builder, ToastTargetApp.Mail);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CreateWebView2RuntimeMissingNotification()
|
public void CreateWebView2RuntimeMissingNotification()
|
||||||
@@ -268,7 +272,7 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
|
|
||||||
builder.AddButton(GetDismissButton());
|
builder.AddButton(GetDismissButton());
|
||||||
builder.AddArgument(Constants.ToastModeKey, Constants.ToastModeMail);
|
builder.AddArgument(Constants.ToastModeKey, Constants.ToastModeMail);
|
||||||
builder.Show();
|
ShowToast(builder, ToastTargetApp.Mail);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task CreateCalendarReminderNotificationAsync(CalendarItem calendarItem, long reminderDurationInSeconds)
|
public Task CreateCalendarReminderNotificationAsync(CalendarItem calendarItem, long reminderDurationInSeconds)
|
||||||
@@ -301,8 +305,34 @@ public class NotificationBuilder : INotificationBuilder
|
|||||||
});
|
});
|
||||||
|
|
||||||
var tag = $"calendar-reminder-{calendarItem.Id:N}-{reminderDurationInSeconds}";
|
var tag = $"calendar-reminder-{calendarItem.Id:N}-{reminderDurationInSeconds}";
|
||||||
builder.Show(toast => toast.Tag = tag);
|
ShowToast(builder, ToastTargetApp.Calendar, tag);
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void ShowToast(ToastContentBuilder builder, ToastTargetApp targetApp, string? tag = null)
|
||||||
|
{
|
||||||
|
var toastNotification = new ToastNotification(builder.GetToastContent().GetXml());
|
||||||
|
|
||||||
|
if (!string.IsNullOrWhiteSpace(tag))
|
||||||
|
{
|
||||||
|
toastNotification.Tag = tag;
|
||||||
|
}
|
||||||
|
|
||||||
|
var appUserModelId = GetAppUserModelId(targetApp);
|
||||||
|
var notifier = ToastNotificationManager.CreateToastNotifier(appUserModelId);
|
||||||
|
notifier.Show(toastNotification);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static string GetAppUserModelId(ToastTargetApp targetApp)
|
||||||
|
{
|
||||||
|
var appId = targetApp == ToastTargetApp.Mail ? MailApplicationId : CalendarApplicationId;
|
||||||
|
return $"{Package.Current.Id.FamilyName}!{appId}";
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum ToastTargetApp
|
||||||
|
{
|
||||||
|
Mail,
|
||||||
|
Calendar
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user