Skip to content

5. Notification with Action

Elvin Thudugala edited this page Jul 28, 2025 · 6 revisions

Notification Actions Guide

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)

Table of Contents

  1. Overview
  2. Setting Up Actions
  3. Implementing in .NET MAUI
  4. Implementing in Xamarin.Forms
  5. Handling Action Responses
  6. Platform-Specific Considerations

Overview

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

Setting Up Actions

Action Categories

Actions are grouped into categories. Each category can contain multiple actions and is identified by a NotificationCategoryType.

Action Properties

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

.NET MAUI Implementation

1. Configure Actions in MauiProgram.cs

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();
    }
}

2. Create a Notification with Actions

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);

Xamarin.Forms Implementation

1. Register Action Categories

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
            }
        }
    }
});

Handling Action Responses

1. Subscribe to Action Events

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
    }
}

Platform-Specific Considerations

Android

  • 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

iOS

  • 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

Best Practices

  1. Keep action titles short and clear
  2. Use meaningful action IDs
  3. Handle all action responses appropriately
  4. Consider platform-specific behaviors
  5. Test actions in different app states (foreground/background)

Visual Examples

Android Notification Actions

Android Notification Action

iOS Notification Actions

iOS Notification Action

Clone this wiki locally