Plugin.LocalNotification icon indicating copy to clipboard operation
Plugin.LocalNotification copied to clipboard

Notification Receiving not working on iOS when in foreground

Open gsk143 opened this issue 1 year ago • 21 comments

Describe the bug Notification Receiving not working on iOS

To Reproduce Use sample code in the repo and add LocalNotificationCenter.Current.NotificationReceived += NotificationRecived;

private void NotificationRecived(NotificationEventArgs e) { MainThread.BeginInvokeOnMainThread(() => Shell.Current.DisplayAlert(GlobalConstants.Notification.SyncNotificationTitle, GlobalConstants.Notification.SyncNotificationDesc, AppRes.Ok)); }

Expected behavior Should fire the event in the foreground and the user can do his logic

Platform:

  • OS: iOS
  • Version 16.2

gsk143 avatar Jun 06 '23 09:06 gsk143

Workaround: Add this to the class linked below public async override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler) { //Do your logic }

https://github.com/thudugala/Plugin.LocalNotification/issues/334#issuecomment-1483969397

gsk143 avatar Jun 07 '23 12:06 gsk143

@gsk143 please try version 10.1.6

thudugala avatar Jun 17 '23 00:06 thudugala

@thudugala struggling with this issue, 10.1.6 did seem to resolve the issue unfortunately. Others maybe able to also confirm this.

LeoJHarris avatar Jun 20 '23 03:06 LeoJHarris

@LeoJHarris Are you having the issue in Xamarin or .NET Maui ?

thudugala avatar Jun 25 '23 03:06 thudugala

@LeoJHarris Are having the issue in Xamarin or .NET Maui ?

@thudugala issue is in Xamarin Forms. Using latest of this library.

LeoJHarris avatar Jun 26 '23 00:06 LeoJHarris

@LeoJHarris Please try version 10.1.7

Make sure to call LocalNotificationCenter.SetUserNotificationCenterDelegate();

https://github.com/thudugala/Plugin.LocalNotification/wiki/Usage-10.0.0-Xamarin.Forms

thudugala avatar Jun 27 '23 12:06 thudugala

@thudugala still not working iOS. LocalNotificationCenter.SetUserNotificationCenterDelegate(); was already being included. Side note, I can confirm the notification permission is granted.

LeoJHarris avatar Jun 29 '23 02:06 LeoJHarris

Hey @LeoJHarris can you attach a sample project?

thudugala avatar Jun 29 '23 02:06 thudugala

https://github.com/thudugala/Plugin.LocalNotification/assets/4112014/5e45e2d5-14ed-4757-9115-db4df9f54110

thudugala avatar Jun 29 '23 13:06 thudugala

@thudugala I'm not too sure which NuGet version your using in that sample screen but I pulled your repo and ran the NuGet sample project, updated the NuGet package from 8.0.2 (notifications working) => 10.1.8 (notifications not working):

NuGet.zip

LeoJHarris avatar Jul 05 '23 06:07 LeoJHarris

@LeoJHarris

Make sure to call LocalNotificationCenter.SetUserNotificationCenterDelegate();

https://github.com/thudugala/Plugin.LocalNotification/wiki/2.-Usage-10.0.0-Xamarin.Forms

thudugala avatar Jul 06 '23 08:07 thudugala

@thudugala

Have now added LocalNotificationCenter.SetUserNotificationCenterDelegate(); In AppDelegate class and still not receiving any notification on iOS, see attached sample

NuGet.zip

Its still the same sample project that was updated from 8.0.2 (notifications working) => 10.1.8 (notifications not working and calls LocalNotificationCenter.SetUserNotificationCenterDelegate();)

LeoJHarris avatar Jul 06 '23 20:07 LeoJHarris

@LeoJHarris

Please read the doc fully

https://github.com/thudugala/Plugin.LocalNotification/wiki/2.-Usage-10.0.0-Xamarin.Forms

you need to request Permission

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

thudugala avatar Jul 07 '23 05:07 thudugala

@thudugala thanks for assisting, its working now if I call:

await LocalNotificationCenter.Current.Show(request).ConfigureAwait(true);

but using DI does not work which I had been doing previously:

_notificationService.Show(request).ConfigureAwait(true))

I registered the service like this:

containerRegistry.RegisterInstance(LocalNotificationCenter.Current);

Another comment here https://github.com/thudugala/Plugin.LocalNotification/issues/334#issuecomment-1426768866 pointed out using Dependency Injection was not working with this plugin. Is there a way to register and use with DI?

iOS seems not to be working with DI, android is okay.

LeoJHarris avatar Jul 09 '23 22:07 LeoJHarris

Will look into it as soon as possible next week.

thudugala avatar Jul 09 '23 22:07 thudugala

@LeoJHarris for Xamarin Forms, you must register this plugin to the DI by yourself. It's only registered automatically in .Net MAUI

thudugala avatar Jul 22 '23 01:07 thudugala

@thudugala I am already doing the DI like this:

_ = containerRegistry.RegisterInstance(LocalNotificationCenter.Current);

LeoJHarris avatar Jul 24 '23 04:07 LeoJHarris

,@LeoJHarris can attach a sample project?

thudugala avatar Jul 24 '23 05:07 thudugala

Hello, I have the same problem on 10.1.8 with xamarin.forms ios On simulator notification pops up when app is in foreground, but on real device doesn't. Is there a workaround for that? Thank you.

nikubesliu avatar Aug 02 '23 09:08 nikubesliu

@nikubesliu is permission granted to show notifications?

Can you attach a sample project?

thudugala avatar Aug 02 '23 22:08 thudugala

@thudugala Sorry I havnt been able to get reproduce this in a small sample application, seemed to occur in the production app we have but Ill need to double check things again when I have time.

LeoJHarris avatar Aug 16 '23 03:08 LeoJHarris