Plugin.LocalNotification
Plugin.LocalNotification copied to clipboard
Notification Receiving not working on iOS when in foreground
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
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 please try version 10.1.6
@thudugala struggling with this issue, 10.1.6 did seem to resolve the issue unfortunately. Others maybe able to also confirm this.
@LeoJHarris Are you having the issue in Xamarin or .NET Maui ?
@LeoJHarris Are having the issue in Xamarin or .NET Maui ?
@thudugala issue is in Xamarin Forms. Using latest of this library.
@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 still not working iOS. LocalNotificationCenter.SetUserNotificationCenterDelegate();
was already being included. Side note, I can confirm the notification permission is granted.
Hey @LeoJHarris can you attach a sample project?
https://github.com/thudugala/Plugin.LocalNotification/assets/4112014/5e45e2d5-14ed-4757-9115-db4df9f54110
@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):
@LeoJHarris
Make sure to call LocalNotificationCenter.SetUserNotificationCenterDelegate();
https://github.com/thudugala/Plugin.LocalNotification/wiki/2.-Usage-10.0.0-Xamarin.Forms
@thudugala
Have now added LocalNotificationCenter.SetUserNotificationCenterDelegate();
In AppDelegate class and still not receiving any notification on iOS, see attached sample
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
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 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.
Will look into it as soon as possible next week.
@LeoJHarris for Xamarin Forms, you must register this plugin to the DI by yourself. It's only registered automatically in .Net MAUI
@thudugala I am already doing the DI like this:
_ = containerRegistry.RegisterInstance(LocalNotificationCenter.Current);
,@LeoJHarris can attach a sample project?
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 is permission granted to show notifications?
Can you attach a sample project?
@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.