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

Firebase integration disables CurrentNotificationActionTapped trigger #451

Open Bindslev opened 9 months ago

Bindslev commented 9 months ago

Describe the bug Most likely a configuration issue, but when I use local notifications together with push notifications from firebase (this plugin https://github.com/TobiasBuchholz/Plugin.Firebase), the notifications I get from this plugin doesn't respond to clicks.

So, I actually do receive both notifications as I should, but my local notification contains action buttons, and when I have push notifications enabled, the CurrentNotificationActionTapped won't trigger when clicking the action buttons.

I have added this piece of code from the documentation regarding push notifications:

.AddiOS(iOS =>
            {
#if IOS
                iOS.SetCustomUserNotificationCenterDelegate(new CustomUserNotificationCenterDelegate());
#endif
            });

and my CustomUserNotificationCenterDelegate class looks identical to the one in the documentation.

In my dependency injection setup, I'm not sure whether to register this plugin, or firebase first, and they act very differently depending on what I register first;

If I register this plugin first, I'll get both local notifications and push notifications, but CurrentNotificationActionTapped is not triggered on the local notification when the action buttons are clicked. If I register the firebase plugin first, I'll only get local notifications and CurrentNotificationActionTapped is triggered just fine, but push notifications won't appear at all.

Any guidance would be much appreciated. Everything works just fine in isolation.

To Reproduce Steps to reproduce the behavior: Install firebase cloud messaging in an app with this plugin and add action buttons to the local notification. Now CurrentNotificationActionTapped won't trigger if clicked.

Expected behavior I would expect that the CurrentNotificationActionTapped is triggered AND that I can also retrieve push notifications. - Again, both of those work in isolation but not when both are enabled.

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

Platform (please complete the following information):

Smartphone (please complete the following information):

thudugala commented 9 months ago

@Bindslev Can you please attach a sample project?

borrmann commented 7 months ago

I actually have a similar, but kind of opposite problem (using apns, though). Previously I handled taps on my push notifications in AppDelegate

public override bool FinishedLaunching(UIApplication app, NSDictionary options)
{
    if (DeviceInstallationService.NotificationsSupported)
    {
        UNUserNotificationCenter.Current.RequestAuthorization(
                UNAuthorizationOptions.Alert |
                UNAuthorizationOptions.Badge |
                UNAuthorizationOptions.Sound,
                (approvalGranted, error) =>
                {
                    if (approvalGranted && error == null)
                        RegisterForRemoteNotifications();
                });
    }
    using (var userInfo = options?.ObjectForKey(UIApplication.LaunchOptionsRemoteNotificationKey) as NSDictionary) ProcessNotificationActions(userInfo);

    UNUserNotificationCenter.Current.Delegate = new UserNotificationCenterDelegate();

    return base.FinishedLaunching(app, options);
}
void ProcessNotificationActions(NSDictionary userInfo)
{
    if (userInfo == null)
        return;

    try
    {
        var actionValue = userInfo.ObjectForKey(new NSString("action")) as NSString;

        if (!string.IsNullOrWhiteSpace(actionValue?.Description))
            NotificationActionService.TriggerAction(actionValue.Description);
    }
    catch (Exception ex)
    {
        Debug.WriteLine(ex.Message);
    }
}

but this doesnt work anymore. I figured I now have to use the mentioned CustomUserNotificationCenterDelegate, but I don't know how to get my previously used var actionValue = userInfo.ObjectForKey(new NSString("action")) as NSString; as I have no clue about all those iOS objects. I suppose it is probably somewhere inside UNNotificationResponse response?

borrmann commented 7 months ago

Also I can confirm it is not a problem with DidReceiveNotificationResponse inside CustomUserNotificationCenterDelegate. It does get triggered when I tap a Push-Notification, but it doesnt seem to have the information of the tapped notification like I was able before. (OR - I dont know where to find it)

I tried this:

Console.WriteLine(response.Notification.ToString());

but what I get is this:

 <UNNotification: 0x283dd77b0; source: <<mypackagename>> date: 2023-11-14 16:16:31 +0000, request: <UNNotificationRequest: 0x283dd4ed0; identifier: 1BB89C3B-2390-4DAF-8760-2318B44EB3B7, content: <UNNotificationContent: 0x151604e70; title: (null), subtitle: (null), body: <redacted>, summaryArgument: , summaryArgumentCount: 0, categoryIdentifier: , launchImageName: , threadIdentifier: , attachments: (
2023-11-14 16:16:32.627 <<myNamespaceRemoved>>[12077:9575350] ), badge: (null), sound: (null), realert: 0, interruptionLevel: 1, relevanceScore: 0.00, filterCriteria: (null), screenCaptureProhibited: 0, trigger: <UNPushNotificationTrigger: 0x28b1ef540; contentAvailable: NO, mutableContent: NO>>, intents: (
)>

although the notification that was tapped did have a title and description. Again, I assume the data is probably somewhere but I have no clue about the iOS objects and am struggling to find the information I need.

Any help would be much appreciated!

borrmann commented 7 months ago

I figured it out with the help of this this that my data was hiding just here!

var actionValue = response.Notification.Request.Content.UserInfo.ObjectForKey(new NSString("action"));
if(!string.IsNullOrWhiteSpace(actionValue?.Description))
    MyPushNotificationHandler(actionValue.Description);