-
Notifications
You must be signed in to change notification settings - Fork 72
5. Notification with Action
Elvin Thudugala edited this page Jul 28, 2025
·
6 revisions
This guide explains how to add interactive actions to your local notifications, allowing users to respond directly from the notification. (Available from version 7.0.0 and above)
- Overview
- Setting Up Actions
- Implementing in .NET MAUI
- Implementing in Xamarin.Forms
- Handling Action Responses
- Platform-Specific Considerations
Notification actions allow users to interact with your notifications without opening the app. Common use cases include:
- Accepting/Rejecting invitations
- Marking tasks as complete
- Quick replies
- Snoozing reminders
Actions are grouped into categories. Each category can contain multiple actions and is identified by a NotificationCategoryType
.
Each action has these key properties:
-
Id
: Unique identifier for the action -
Title
: Display text for the action button -
LaunchAppWhenTapped
: Whether to open the app when action is tapped - Platform-specific settings for Android and iOS
public static class MauiProgram
{
public static MauiApp CreateMauiApp()
{
var builder = MauiApp.CreateBuilder();
builder
.UseMauiApp<App>()
.UseLocalNotification(config =>
{
// Define a Status category with multiple actions
config.AddCategory(new NotificationCategory(NotificationCategoryType.Status)
{
ActionList = new HashSet<NotificationAction>
{
// Positive action that launches the app
new NotificationAction(100)
{
Title = "Accept",
Android =
{
LaunchAppWhenTapped = true,
IconName = { ResourceName = "accept_icon" }
},
iOS = { Action = iOSActionType.Foreground }
},
// Destructive action that doesn't launch the app
new NotificationAction(101)
{
Title = "Decline",
Android =
{
LaunchAppWhenTapped = false,
IconName = { ResourceName = "decline_icon" }
},
iOS = { Action = iOSActionType.Destructive }
}
}
});
});
return builder.Build();
}
}
var notification = new NotificationRequest
{
NotificationId = 100,
Title = "Meeting Invitation",
Description = "Team meeting at 2 PM",
CategoryType = NotificationCategoryType.Status // This links the notification to the action category
};
await LocalNotificationCenter.Current.Show(notification);
LocalNotificationCenter.Current.RegisterCategoryList(new HashSet<NotificationCategory>
{
new NotificationCategory(NotificationCategoryType.Status)
{
ActionList = new HashSet<NotificationAction>
{
new NotificationAction(100)
{
Title = "Accept",
iOSAction = iOSActionType.Foreground
},
new NotificationAction(101)
{
Title = "Decline",
iOSAction = iOSActionType.Destructive
}
}
}
});
public partial class App : Application
{
public App()
{
InitializeComponent();
LocalNotificationCenter.Current.NotificationActionTapped += OnNotificationActionTapped;
MainPage = new MainPage();
}
private async void OnNotificationActionTapped(NotificationActionEventArgs e)
{
switch (e.ActionId)
{
case 100: // Accept action
await HandleAcceptAction(e.Request);
break;
case 101: // Decline action
await HandleDeclineAction(e.Request);
break;
}
}
private async Task HandleAcceptAction(NotificationRequest request)
{
await MainThread.InvokeOnMainThreadAsync(async () =>
{
await Shell.Current.DisplayAlert(
"Action Taken",
$"Accepted: {request.Title}",
"OK");
});
}
private async Task HandleDeclineAction(NotificationRequest request)
{
// Cancel the notification
await LocalNotificationCenter.Current.Cancel(request.NotificationId);
// Additional decline handling logic
}
}
- Action icons must be added to the Android project's drawable resources
- Actions can be configured to launch the app or handle in the background
- Consider using appropriate icon resources for actions
- Supports different action types:
-
Foreground
: Opens the app -
Destructive
: Red-colored action for destructive operations -
Background
: Handles action without opening app
-
- Actions appear in the expanded notification view
- Keep action titles short and clear
- Use meaningful action IDs
- Handle all action responses appropriately
- Consider platform-specific behaviors
- Test actions in different app states (foreground/background)