Skip to content

1. Usage 10.0.0 .Net MAUI

Elvin (Tharindu) Thudugala edited this page Aug 14, 2024 · 8 revisions

Setup

Usage

Android-specific setup

Add the assembly-based permission:

Open the Platforms/Android/MainApplication.cs file and add the following assembly attributes after using directives:

// Minimum permissions 
[assembly: UsesPermission(Manifest.Permission.Vibrate)]
[assembly: UsesPermission("android.permission.POST_NOTIFICATIONS")]

// Required so that the plugin can schedule
[assembly: UsesPermission(Manifest.Permission.WakeLock)]

//Required so that the plugin can reschedule notifications upon a reboot
[assembly: UsesPermission(Manifest.Permission.ReceiveBootCompleted)]


//Optional (only for Calendar and alarm clock apps)
//If the app requires scheduling notifications with exact timings (aka exact alarms), there are two options since Android 14 brought about behavioural changes

// Users will not be prompted to grant permission, however as per the official Android documentation on the USE_EXACT_ALARM permission
// (refer to (https://developer.android.com/about/versions/14/changes/schedule-exact-alarms#calendar-alarm-clock) and (https://developer.android.com/reference/android/Manifest.permission#USE_EXACT_ALARM)),
// this requires the app to target Android 13 (API level 33) or higher and could be subject to approval and auditing by the app store(s) used to publish theapp
[assembly: UsesPermission("android.permission.USE_EXACT_ALARM")]

// user can grant the permission via the app
[assembly: UsesPermission("android.permission.SCHEDULE_EXACT_ALARM")]

Or Update the Android Manifest:

Open the Platforms/Android/AndroidManifest.xml file and add the following in the manifest node:

<uses-permission android:name="android.permission.WAKE_LOCK" />

<!--Required so that the plugin can reschedule notifications upon a reboot-->
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

Optional (only for Calendar and alarm clock apps)
<uses-permission android:name="android.permission.USE_EXACT_ALARM" />
<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM" android:maxSdkVersion="32" />

iOS

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>remote-notification</string>
</array>

Setup

public static MauiApp CreateMauiApp()
{
	var builder = MauiApp.CreateBuilder();
	builder
		.UseMauiApp<App>()
		.........
		.UseLocalNotification();
			
	return builder.Build();
}

Show local notification

if (await LocalNotificationCenter.Current.AreNotificationsEnabled() == false)
{
    await LocalNotificationCenter.Current.RequestNotificationPermission();
}

var notification = new NotificationRequest
{
    NotificationId = 100,
    Title = "Test",
    Description = "Test Description",
    ReturningData = "Dummy data", // Returning data when tapped on notification.
    Schedule = 
    {
        NotifyTime = DateTime.Now.AddSeconds(30) // This is Used for Scheduling local notifications; if not specified, the notification will show immediately.
    }
};
await LocalNotificationCenter.Current.Show(notification);

Using MVVM

You can use INotificationService without LocalNotificationCenter.Current as it is dependency injected

Receive local notification tap event

public partial class App : Application
{
	public App()
	{
		InitializeComponent();

		// Local Notification tap event listener
		LocalNotificationCenter.Current.NotificationActionTapped += OnNotificationActionTapped;

		MainPage = new MainPage();
	}
	
	private void OnNotificationActionTapped(NotificationActionEventArgs e)
    	{
           if (e.IsDismissed)
           {
               // your code goes here
               return;
           }
	   if (e.IsTapped)
           {
               // your code goes here
               return;
           }
           // if Notification Action are setup
           switch (e.ActionId)
           {
               // your code goes here
           }
	}
}