thudugala / Plugin.LocalNotification

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

Geofence notifications do not execute on Android (works perfectly on iOS) #335

Open ClauderGunpowder opened 1 year ago

ClauderGunpowder commented 1 year ago

Describe the bug Cannot get Geofence notifications to execute on Android.

Notice:

I suspected that for my Android version I needed to add a channel group and channel, but when I do I get this error message at startup (see below) so therefore these two lines is commented out in the code sample:

JNINativeWrapper.g.cs : NotificationChannelGroup doesn't exist

To Reproduce Steps to reproduce the behavior: MauiProgram

using Microsoft.Extensions.Logging; using Plugin.LocalNotification; using Plugin.LocalNotification.AndroidOption; using Plugin.LocalNotification.iOSOption;

namespace PowerFences;

public static class MauiProgram { public static MauiApp CreateMauiApp() { NotificationChannelGroupRequest notificationChannelGroupRequest = new Plugin.LocalNotification.AndroidOption.NotificationChannelGroupRequest() { Group = "PowerFences", Name = "PowerFences" }; NotificationChannelRequest notificationChannelRequest = new Plugin.LocalNotification.AndroidOption.NotificationChannelRequest() { Name = "PowerFences", Description = "Channel for Android notifications", Group = "PowerFences", ShowBadge = true, EnableSound = true };

    var builder = MauiApp.CreateBuilder();
    builder
        .UseMauiApp<App>()
        .UseLocalNotification(config =>
        {
            config.AddiOS(iOS =>
            {
                iOS.SetPermission(new iOSNotificationPermission
                {
                    LocationAuthorization = iOSLocationAuthorization.Always
                });
            });
            config.AddAndroid(android =>
            {
                //android.AddChannelGroup(notificationChannelGroupRequest);
                //android.AddChannel(notificationChannelRequest);
            });
        })
        .ConfigureFonts(fonts =>
        {
            fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular");
            fonts.AddFont("OpenSans-Semibold.ttf", "OpenSansSemibold");
        });

if DEBUG

    builder.Logging.AddDebug();

endif

    return builder.Build();
}

}

The NotificationRequest w/Geofence:

public void addGeofenceNotificationHandler(int notificationId, NotificationRequestGeofence.GeofenceNotifyOn direction) { var notification = new NotificationRequest { NotificationId = notificationId, Title = "PowerFence", Subtitle = direction == NotificationRequestGeofence.GeofenceNotifyOn.OnEntry ? "Entry" : "Exit", Description = $"Elme Alle", BadgeNumber = 2, Silent = false, iOS = new Plugin.LocalNotification.iOSOption.iOSOptions { PlayForegroundSound = true }, Android = new Plugin.LocalNotification.AndroidOption.AndroidOptions { LaunchAppWhenTapped = true, ChannelId = "PowerFences" }, Group = "PowerFences", CategoryType = NotificationCategoryType.Reminder, ReturningData = direction == NotificationRequestGeofence.GeofenceNotifyOn.OnEntry ? "Entry" : "Exit", Geofence = new NotificationRequestGeofence { Center = new NotificationRequestGeofence.Position { Latitude = _savedLocation.Latitude, Longitude = _savedLocation.Longitude }, RadiusInMeters = 1000, NotifyOn = direction, iOS = new Plugin.LocalNotification.iOSOption.iOSGeofenceOptions { Repeats = true }, Android = new Plugin.LocalNotification.AndroidOption.AndroidGeofenceOptions { ExpirationDurationInMilliseconds = -1, LoiteringDelayMilliseconds = 1000, ResponsivenessMilliseconds = 0 } } };

    LocalNotificationCenter.Current.Show(notification);
}

Expected behavior I expect a notification to appear whenever I enter or exit a defined geofence area.

Platform (please complete the following information):

Smartphone (please complete the following information):

Additional context Using Visual Studio 2022 for Mac .NET 7, MAUI

Required permissions set for Android: AcceptBackgroundLocation, Access CoarseLocation, AccessFineLocation, AccessNetworkState, Internet, LocationHardware, PostNotifications. info.plist properties set for iOS: Privacy - Location Always and When In Use Usage Description (true), Privacy - Reminders Usage Description (true), Required background modes.

DeerSteak commented 1 year ago

I'm not convinced it's a problem with the plugin as much as it is a problem with Android. I went through the trouble of creating my own BroadcastReceiver in Xamarin.Android and registering my geofences manually, then using the plugin to send notifications, and the behavior didn't really change.

What fixed it for me was doing everything I could to get more timely location updates. For example, after adding activity tracking, then every time I get into or out of the car, an activity monitoring service fires up and gets location, and that tells the device it's in a geofence. Now notifications deliver like they should. Or at least closer to what I expect.

Android background location was brutally bad. It's improved a lot with Android 13. Combining it with other services like activity tracking helps a ton.

claus-lysholm commented 1 year ago

That's very interesting because I have experienced that if I later fire a method getting my current location, a couple of apparently stacked up but invisible notifications suddenly shows up as visible notifications on the phone.

DeerSteak commented 1 year ago

Exactly, because the phone finally got the location and delivered the scheduled notifications.

claus-lysholm commented 1 year ago

But still, I'm getting doubts about using this for my current project. Have praised that everything worked on iPhone (14 v/iOS 16). Then earlier today taking it on the road on my iPhone Pro 11, and no notifications at all. Okay, back to my desktop, creating and testing with an iPhone Pro 11 emulator and Nothing! Testing again on iPhone 14 and Bingo, that works. Can't have it working on only a small number of devices though, so ... (Permissions match on the iPhone 11 and iPhone 14 emulators)

claus-lysholm commented 1 year ago

@thudugala Notification w/Geofence setup: Do you need to create that in a foreground service or do the plugin manage that itself? For iOS testing, I just test with code in a .maui MainPage.xaml.cs sample project. Expecting Android to behave the same way.