thudugala/Plugin.LocalNotification

Exception on iOS: System.ArgumentNullException: Value cannot be null. (Parameter 'implementationInstance')

Closed this issue · 3 comments

Describe the bug
Version: 11.1.1
On iOS the local notification is not working. Following is the exception.

System.ArgumentNullException: Value cannot be null. (Parameter 'implementationInstance')
   at System.ThrowHelper.Throw(String paramName)
   at System.ThrowHelper.ThrowIfNull(Object argument, String paramName)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionServiceExtensions.AddSingleton[INotificationService](IServiceCollection services, INotificationService implementationInstance)
   at Plugin.LocalNotification.LocalNotificationExtensions.UseLocalNotification(MauiAppBuilder builder, Action`1 configureDelegate)
   at TestProject.MauiProgram.CreateMauiApp() in /Volumes/Dev/TestProject/MauiProgram.cs:line 35
   at TestProject.AppDelegate.CreateMauiApp() in /Volumes/Dev/TestProject/Platforms/iOS/AppDelegate.cs:line 11
   at Microsoft.Maui.MauiUIApplicationDelegate.WillFinishLaunching(UIApplication application, NSDictionary launchOptions)
   at UIKit.UIApplication.UIApplicationMain(Int32 argc, String[] argv, IntPtr principalClassName, IntPtr delegateClassName) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 58
   at UIKit.UIApplication.Main(String[] args, Type principalClass, Type delegateClass) in /Users/builder/azdo/_work/1/s/xamarin-macios/src/UIKit/UIApplication.cs:line 94
   at TestProject.Program.Main(String[] args) in /Volumes/Dev/TestProject/Platforms/iOS/Program.cs:line 13

To Reproduce
Steps to reproduce the behavior:
Add "Plugin.LocalNotification" and follow
the documentation.

Expected behavior
The application should not crash.

Screenshots
If applicable, add screenshots to help explain your problem.

Platform (please complete the following information):

  • OS: MacOS
  • Version .net-8

Smartphone (please complete the following information):

  • Device: iPhone 7 and iPhone 15 Pro Max
  • OS: iOS 15.6, iOS 17.0

Additional context
The project was using nuget version 10.1.8 and was working fine.

Edit: I just tried version 11.1.0, That is also working fine.

@Ahmadpashaquarterpi can you please attach a sample project?

I'm also experiencing this issue. I've dug through the code for a bit and am still unsure why this is happening but it appears the Lazy<INotificationServce> in LocalNotificationCenter.cs isn't being initialized correctly. The problem is here:

public partial class LocalNotificationCenter
    {
        private static readonly Lazy<INotificationService> implementation = new(CreateNotificationService, LazyThreadSafetyMode.PublicationOnly);

        ...

        private static INotificationService CreateNotificationService()
        {
#if ANDROID || IOS || WINDOWS
            return new NotificationServiceImpl();
#else
            return null;
#endif
        }

        ...

        public static INotificationService Current => implementation.Value;

        ...


It could just be a nasty .net 8.0/iOS bug but I'll keep digging to, at the very least, try to find a workaround.

I was able to solve this issue by adding the fetch and remote-notification UIBackgroundModes to my Info.plist file. Hope this helps.

<key>UIBackgroundModes</key>
<array>
    <string>fetch</string>
    <string>remote-notification</string>
</array>