Skip to content

Location Notifications

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

From Version 10.0.1 and above

You can set up Geofence to trigger a notification.

In iOS, make sure to specify LocationAuthorization type

if (await LocalNotificationCenter.Current.AreNotificationsEnabled() == false)
{
    await LocalNotificationCenter.Current.RequestNotificationPermission(new NotificationPermission
                               {
                                   IOS =
                                   {
                                       LocationAuthorization = iOSLocationAuthorization.WhenInUse
                                   }
                               });
}

By default, notification triggered 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,
        NotifyOn = 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

Follow platform-specific setup from

https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/device/geolocation?view=net-maui-7.0&tabs=android https://learn.microsoft.com/en-us/dotnet/maui/platform-integration/device/geolocation?view=net-maui-7.0&tabs=ios

iOS

https://developer.apple.com/documentation/corelocation/requesting_authorization_for_location_services https://developer.apple.com/documentation/bundleresources/privacy_manifest_files/describing_data_use_in_privacy_manifests

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