Reputation: 11
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
Upvotes: 0
Views: 712
Reputation: 8245
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.
<Package
...
xmlns:desktop="http://schemas.microsoft.com/appx/manifest/desktop/windows10"
xmlns:com="http://schemas.microsoft.com/appx/manifest/com/windows10"
IgnorableNamespaces="uap rescap com desktop">
...
<Applications>
<Application Id="App" Executable="$targetnametoken$.exe" EntryPoint="$targetentrypoint$">
<uap:VisualElements
...
</uap:VisualElements>
<Extensions>
<!-- Specify which CLSID to activate when toast clicked -->
<desktop:Extension Category="windows.toastNotificationActivation">
<desktop:ToastNotificationActivation ToastActivatorCLSID="YOUR_APP_ID_FROM_CSPROJ" />
</desktop:Extension>
<!--Register COM CLSID LocalServer32 registry key-->
<com:Extension Category="windows.comServer">
<com:ComServer>
<com:ExeServer Executable="YOUR_APP_NAME.exe" Arguments="-ToastActivated" DisplayName="Toast activator">
<com:Class Id="YOUR_APP_ID_FROM_CSPROJ" DisplayName="Toast activator"/>
</com:ExeServer>
</com:ComServer>
</com:Extension>
</Extensions>
</Application>
Remember to change the values for ToastActivatorCLSID="YOUR_APP_ID_FROM_CSPROJ"
, Executable="YOUR_APP_NAME.exe"
and Executable="YOUR_APP_NAME.exe"
based on your project.
Second, subscribe the OnActivated
by adding the following lines in App.xaml.cs placed under Platforms/Windows folder.
public partial class App : MauiWinUIApplication
{
public App()
{
this.InitializeComponent();
}
...
//add the following lines
protected override void OnLaunched(LaunchActivatedEventArgs args)
{
ToastNotificationManagerCompat.OnActivated += ToastNotificationManagerCompat_OnActivated;
base.OnLaunched(args);
}
void ToastNotificationManagerCompat_OnActivated(ToastNotificationActivatedEventArgsCompat e)
{
// Handle ToastNotificationEvent.
Console.WriteLine(e.ToString());
}
...
}
For more info, you may refer to App notifications from UWP to WinUI 3 migration, WinUI specifics.
Hope it helps!
Upvotes: 0