thudugala / Plugin.LocalNotification

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

`NotificationReceiving` not called when implementing UNUserNotificationCenterDelegate #445

Closed LeoJHarris closed 1 year ago

LeoJHarris commented 1 year ago

Might be more a configuration question but we have remote notification support so we are overriding the WillPresentNotification and DidReceiveNotificationResponse similar to what you see below (code stripped out):

public class UserNotificationCenterDelegate : UNUserNotificationCenterDelegate
    {
        #region Constructors

        public UserNotificationCenterDelegate()
        {
        }

        #endregion Constructors

        #region Override Methods

        public override void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
        {
            ...
        }

        public override void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification, Action<UNNotificationPresentationOptions> completionHandler)
        {
            ...
        }

        #endregion Override Methods
    }

And in AppDelegate we setup our UserNotificationCenterDelegate and set accordingly:

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
        {
            global::Xamarin.Forms.Forms.Init();

            LocalNotificationCenter.SetUserNotificationCenterDelegate();
            UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

            LoadApplication(new App(new iOSInitializer()));
            return base.FinishedLaunching(app, options);
        }

The problem the NotificationReceiving event is not being called:

below prevents NotificationReceiving from being called

UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

We need access to the overridden methods in the UserNotificationCenterDelegate to handle remote notifications in the foreground which is working fine but now the NotificationReceiving has stopped firing

LeoJHarris commented 1 year ago

See sample attached:

NuGet.zip

thudugala commented 1 year ago

@LeoJHarris when you call SetUserNotificationCenterDelegate pass in UserNotificationCenterDelegate

look at on implementing custom CustomUserNotificationCenterDelegate Sample/Direct Maui/LocalNotification.Sample/Platforms/iOS/CustomUserNotificationCenterDelegate.cs

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    global::Xamarin.Forms.Forms.Init();

    // if you want to handle push notifications
    LocalNotificationCenter.SetCustomUserNotificationCenterDelegate(new CustomUserNotificationCenterDelegate());

    LoadApplication(new App());

    return base.FinishedLaunching(app, options);
}

https://github.com/thudugala/Plugin.LocalNotification/wiki/%5BiOS%5D-Working-with-push-notifications