Skip to content

5. Notification with Action

Elvin (Tharindu) Thudugala edited this page Jul 5, 2024 · 5 revisions

From version 7.0.0 and above

Register Categories with actions

Setup Categories at the beginning of the app

.Net MAUI

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
                        ....
			.UseLocalNotification(config =>
			{
                        	config.AddCategory(new NotificationCategory(NotificationCategoryType.Status)
                        	{
                                	ActionList = new HashSet<NotificationAction>(new List<NotificationAction>()
                    			{
                        			new NotificationAction(100)
                        			{
                            				Title = "Hello",
                            				Android =
                            				{
                                				LaunchAppWhenTapped = true,
                                				IconName =
                                				{
                                    					ResourceName = "i2"
                                				}
                            				},
                                                        IOS =
                                                        {
                                                                Action = iOSActionType.Foreground
                                                        }
                        			},
                        			new NotificationAction(101)
                        			{
                            				Title = "Close",
                            				Android =
                            				{
                                				LaunchAppWhenTapped = false,
                                				IconName =
                                				{
                                    					ResourceName = "i3"
                                				}
                            				},
                                                        IOS =
                                                        {
                                                                Action = iOSActionType.Destructive
                                                        }
                        			}                                	
                    			})
                		});
			});
                return builder.Build();
	}
}

Xamarin.Forms

 LocalNotificationCenter.Current.RegisterCategoryList(new HashSet<NotificationCategory>(new List<NotificationCategory>()
            {
                new NotificationCategory(NotificationCategoryType.Status)
                {
                    ActionList = new HashSet<NotificationAction>( new List<NotificationAction>()
                    {
                        new NotificationAction(100)
                        {
                            Title = "Hello",
                            iOSAction = iOSActionType.None
                        },
                        new NotificationAction(101)
                        {
                            Title = "Close",
                            iOSAction = iOSActionType.None
                        }
                    })
                }
            }));

If Requesting Notification has CategoryType set to one of the Registered Category, the Notification will show the actions in that Category.

var request = new NotificationRequest
{
   NotificationId = 100,
   Title = "Test",
   Description = "Test Description",               
   CategoryType = NotificationCategoryType.Status,                
};          

How to action on Notification action tap

NotificationCenter.Current.NotificationActionTapped += Current_NotificationActionTapped;

private void Current_NotificationActionTapped(NotificationActionEventArgs e)
        {
            switch (e.ActionId)
            {
                case 100:
                    Device.BeginInvokeOnMainThread(() =>
                    {
                        DisplayAlert(e.Request.Title, "Hello Button was Tapped", "OK");
                    });
                    break;

                case 101:
                    NotificationCenter.Current.Cancel(e.Request.NotificationId);
                    break;
            }
        }

Android

Android Notification Action

iOS

iOS Notification Action