Skip to content

Location Notifications

Elvin (Tharindu) Thudugala edited this page Jan 15, 2023 · 7 revisions

From Version 10.0.1 and above

You can set up Geofence to trigger a notification.

By default notification trigged on Entry.

var notification = new NotificationRequest
{
    NotificationId = notificationId,
    Title = "Test",
    Description = $"Test Description",
    Geofence =
    {
        Center =
        {
            Latitude = -37.003578276665095,
            Longitude = 174.78484574983338
        },
        RadiusInMeters = 100                
    }
};
LocalNotificationCenter.Current.Show(notification);

To trigged on both Entry and Exit

var notification = new NotificationRequest
{
    NotificationId = notificationId,
    Title = "Test",
    Description = $"Test Description",
    Geofence =
    {
        Center =
        {
            Latitude = -37.003578276665095,
            Longitude = 174.78484574983338
        },
        RadiusInMeters = 100,
        GeofenceNotifyOn = NotificationRequestGeofence.GeofenceNotifyOn.OnEntry | NotificationRequestGeofence.GeofenceNotifyOn.OnExit                
    }
};
LocalNotificationCenter.Current.Show(notification);

To repeat

var notification = new NotificationRequest
{
    NotificationId = notificationId,
    Title = "Test",
    Description = $"Test Description",
    Geofence =
    {
        Center =
        {
            Latitude = -37.003578276665095,
            Longitude = 174.78484574983338
        },
        RadiusInMeters = 100,
        iOS = 
        {
           Repeats = true
        },
        Android =
        {
            ExpirationDurationInMilliseconds = -1
        },         
    }
};
LocalNotificationCenter.Current.Show(notification);

Setup

iOS

https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services

Important You must add the required keys to your app’s Info.plist file. If a required key isn’t present, authorization requests fail immediately.

.Net MAUI

public static class MauiProgram
{
	public static MauiApp CreateMauiApp()
	{
		var builder = MauiApp.CreateBuilder();
		builder
			.UseMauiApp<App>()
			...
			.UseLocalNotification(config =>
			{
                		config.SetPermission(new NotificationPermission
                               {
                                   IOS =
                                   {
                                       LocationAuthorization = iOSLocationAuthorization.WhenInUse
                                   }
                               })
            		});

        	return builder.Build();
	}
}

Xamarin.Forms

public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{	
	public override bool FinishedLaunching(UIApplication app, NSDictionary options)
	{
		global::Xamarin.Forms.Forms.Init();

		LocalNotificationCenter.RequestLocationPermission(iOSLocationAuthorization.WhenInUse);

		LoadApplication(new App());

		return base.FinishedLaunching(app, options);
	}
}