thudugala / Plugin.LocalNotification

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

iOS NotificationReceiving not fired when app is not in foreground #441

Open PauchardThomas opened 10 months ago

PauchardThomas commented 10 months ago

Describe the bug

Hello,

On iOS, NotificationReceiving is not fired when app is not in foreground...

I would like to show a local notification at 10:00 am each day except for the week-end (saturday/sunday).

The following code is not working, i'm still received the local notification on saturday / sunday when the app is closed or in the background :


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

                LocalNotificationCenter.Current.NotificationReceiving = (request) =>
                {
                    var isWeekend = DateTime.Now.DayOfWeek == DayOfWeek.Sunday || DateTime.Now.DayOfWeek == DayOfWeek.Saturday;
                    return Task.FromResult(new NotificationEventReceivingArgs
                    {
                        Handled =isWeekend,
                        Request = request
                    });
                };

                var notification = new NotificationRequest
                {
                    NotificationId = 100,
                    Title = "My Title",
                    Description = $"My description",
                    Schedule =
                    {
                        NotifyTime = DateTime.Now.AddSeconds(15), // Used for Scheduling local notification, if not specified notification will show immediately.
                        RepeatType = NotificationRepeat.Daily
                    }
                };

                await LocalNotificationCenter.Current.Show(notification);

Any idea please ?

Thanks

Platform (please complete the following information):

Smartphone (please complete the following information):

thudugala commented 10 months ago

@PauchardThomas So NotificationReceiving is only fired when the app is in the foreground?

PauchardThomas commented 10 months ago

@thudugala Exactly

thudugala commented 9 months ago

@PauchardThomas can you attach a sample project?

PauchardThomas commented 9 months ago

@thudugala https://github.com/PauchardThomas/PluginLocalNotification-Issue-441

  1. Launch the app
  2. Accept notifications
  3. You will receive the notification
  4. Close the app
  5. Go to settings, change the day
  6. Problem : You will receive the notification even on saturday and sunday
thudugala commented 9 months ago

@PauchardThomas The workaround for now is to schedule 5 notifications NotificationRepeat.Weekly. Make sure NotifyTime .DayOfWeek is set correctly.