thudugala / Plugin.LocalNotification

The local notification plugin provides a way to show local notifications from .Net MAUI and Xamarin Forms apps .
MIT License
419 stars 68 forks source link

Location based notifications #221

Closed mphill closed 2 years ago

mphill commented 2 years ago

Is your feature request related to a problem? Please describe. As a mobile developer I'd like to have location based notifications, for instance a reminder when I arrive at work or home.

Describe the solution you'd like I would like to create a PR to add this. iOS supports this out of the code, and Android supports this as well with geofencing. I would create a new method WithLocation that would take a long and lat as well as radius for the the parameter.

Describe alternatives you've considered N/A

Additional context Please let me know if you think this is a good fit for the library and I will start to work on it. Thanks

mos379 commented 2 years ago

@mphill sounds like a cool idea, but couldn't your app automatically trigger a notification if you are already surveilling the location?

mphill commented 2 years ago

@mos379 If the app is terminated and/or backgrounded this is not really possible to do practically speaking. This feature is offered by the OS so there is no need to constantly poll the location or force some background mode to keep checking.

This morning I added support for iOS and updated the sample project. I was able to have it trigger when I left my house and returned. It’s just a different trigger type (on iOS) that hasn’t been added to this project yet. https://developer.apple.com/documentation/usernotifications/unlocationnotificationtrigger

I’d imagine the heuristics the OS employs offers significant power savings as well.

thudugala commented 2 years ago

@mphill If no one has strong objection. I'm OK.

mos379 commented 2 years ago

I like it a lot!

On Sun, 31 Oct 2021, 19:25 Elvin (Tharindu) Thudugala < @.***> wrote:

@mphill https://github.com/mphill If no one has strong objection. I'm OK.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/thudugala/Plugin.LocalNotification/issues/221#issuecomment-955769561, or unsubscribe https://github.com/notifications/unsubscribe-auth/AKYEC3ZXUA6LE3QBEFLIIRTUJWCYVANCNFSM5HCH5QPQ . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

mphill commented 2 years ago

I have a branch out with the sample app updated. There is still more work to do to fully integrate this will the existing notification repository - the geofence notifications are working though. The geofencing will work even if the app is killed on Android. Please provide any feedback on design. I will keep working on cleaning up the code and improving integration.

thudugala commented 2 years ago

@mphill are you ready to do a PR ? it look ok to me

mphill commented 2 years ago

I will continue work and get PR out soon. Android still needed work.

thudugala commented 2 years ago

@mphill @mos379 please try 10.0.0-preview02

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);
mos379 commented 2 years ago

wohoo this is so cool! now I just need to find time to try it out

thudugala commented 2 years ago

@mos379 @mphill please try version 10.0.0

mphill commented 2 years ago

@thudugala I’ll start testing. Thanks.

mphill commented 2 years ago

It's working well, there is one issue that that will improve the feature: https://github.com/thudugala/Plugin.LocalNotification/blob/558c7e2e8b6aeaec17d6fec0411c9ea24ec242c7/Source/Plugin.LocalNotification/Platforms/iOS/NotificationServiceImpl.cs#L180

trigger = UNLocationNotificationTrigger.CreateTrigger(regin, false);

All that is needed is to add a bool Repeats to the request and use that for the 2nd parameter. This way the location notification isn't removed after triggering and can be repeated.

Does the android implementation remove after firing?

thudugala commented 2 years ago

@mphill Try version 10.0.1

in Android is ExpirationDurationInMilliseconds == -1, location notification is not removed

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);
mphill commented 2 years ago

This is working well on iOS. I have not tested on Android.

mos379 commented 2 years ago

quick question on the permissions. Now that we have the new Location Permissions, how can we trigger the request for these in iOS and Android?

mphill commented 2 years ago

I used Xamarin Essentials to manage this:

await Permissions.RequestAsync<Permissions.LocationWhenInUse>()

Also see:

https://developer.apple.com/documentation/usernotifications/unlocationnotificationtrigger

thudugala commented 2 years ago

@mos379 @mphill https://github.com/thudugala/Plugin.LocalNotification/wiki/Location-Notifications