I'm working on a .NET MAUI Blazor app and have implemented a notification service using Microsoft.Toolkit.Uwp.Notifications with ToastContentBuilder. (Only for windows platfrom) I want to handle the click event on the notification so that when a user clicks on it, the MAUI Blazor application opens on a specific page (Blazor page) that is determined by the argument passed in the notification.
I followed an article on handling activation (user click on notification), Handling activation which suggested overriding the OnActivated() method in the App.xaml.cs file. However, when I attempt to override this method, I encounter an error stating that the method is not suitable for overriding.
Here's the error I'm getting:
method is not suitable to override
Has anyone successfully implemented a notification click event handler in a .NET MAUI Blazor app that opens a specific page? How can I properly override the OnActivated() method, or is there another approach I should take?
This is my NotficationService class
#if WINDOWS
using MAUIDemo.Services;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MAUIDemo.PlatformServices.Windows
{
public class NotificationService : INotificationService
{
public void ShowNotification(string title, string body)
{
new ToastContentBuilder()
.AddToastActivationInfo(null, ToastActivationType.Foreground)
.AddAppLogoOverride(new Uri("ms-appx:///Assets/dotnet_bot.png"))
.AddText(title, hintStyle: AdaptiveTextStyle.Header)
.AddText(body, hintStyle: AdaptiveTextStyle.Body)
.AddButton(new ToastButton()
.SetContent("See more details")
.AddArgument("action", "viewDetails"))
.AddButton(new ToastButton()
.SetContent("Remind me later")
.AddArgument("action", "remindLater"))
.Show();
}
}
}
#endif
Windows apps built using .NET MAUI use Windows UI3(WinUI 3) library to create native apps that target the Windows desktop. The steps for handling activation you use is for UWP.
In order to handle activation in .NET MAUI, you could make some changes.
First, Edit the Package.appxmanifest file placed under Platforms/Windows folder. You may refer to 2.Using external NuGet package in WinUI specifics.
Remember to change the values for
ToastActivatorCLSID="YOUR_APP_ID_FROM_CSPROJ",Executable="YOUR_APP_NAME.exe"andExecutable="YOUR_APP_NAME.exe"based on your project.Second, subscribe the
OnActivatedby adding the following lines in App.xaml.cs placed under Platforms/Windows folder.For more info, you may refer to App notifications from UWP to WinUI 3 migration, WinUI specifics.
Hope it helps!