question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't get notification response for push notifications on iOS

See original GitHub issue

We are using the LocalNotification plugin for local notifications in our app, and it works great! We are also using Firebase push notifications, and now we are implementing support for performing different actions in the app when the user taps different push notifications.

To achieve this, we have implemented DidReceiveNotificationResponse() in our AppDelegate and use data in response.Notification.Request.Content.UserInfo to know which notification type the user has tapped. The problem is that our implementation of DidReceiveNotificationResponse() never gets called when the LocalNotification plugin is enabled. I suspect the reason is that the plugin also implements this method.

We have also tried to use the NotificationTapped event from the LocalNotification plugin. This event does get called when the user taps the push notification, however, the Request member of the NotificationEventArgs parameter is null, so there is no way for us to know which push notification that was tapped.

Is there any way we can get our implementation of DidReceiveNotificationResponse() to be called when using the plugin? Or would it be possible to update the plugin to pass the Content or UserInfo data from the push notification in the NotificationTapped event? Or do you have any other suggestions on how to solve this?

Note: This is only a problem for us on iOS. On Android, we get the data we need in MainActivity.OnNewIntent() when the user taps a push notification.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
AntAttakkcommented, Oct 7, 2022

@thudugala Sorry for the delay. I have now tested the latest version (v10.0.2) and it works well! I can get my push notifications and the local notifications are handled correctly by the plugin.

I don’t know why it didn’t work on 9.2.1-preview01, but it may very well have been an error in my code.

Thanks for your support and your patience 😃

2reactions
AntAttakkcommented, Feb 25, 2022

I have managed to implement a workaround that allows my implementation of DidReceiveNotificationResponse() to get called and still use the LocalNotification plugin as intended. Maybe it can be useful for someone else that uses both push and local notifications.

Step 1: Initialize the LocalNotification plugin before assigning my own UNUserNotificationCenter delegate. Step 2: Save a reference to the plugin’s UNUserNotificationCenter delegate. Step 3: Assign my own UNUserNotificationCenter delegate. Step 4: In my implementation of WillPresentNotification() and DidReceiveNotificationResponse(), I handle my push notifications and also forward the call to the plugin’s delegate I stored earlier, so it can handle local notifications.

Code snippets from my AppDelegate:

private IUNUserNotificationCenterDelegate? localNotificationDelegate;

private void InitNotifications()
{
    // Initialize the LocalNotification plugin before we initialize our own UNUserNotificationCenter
    // delegate, to avoid that the plugin overwrites our delegate.
    var ignore = Plugin.LocalNotification.NotificationCenter.Current;

    // Then store the plugin's delegate so we can forward UNUserNotificationCenter calls to it. 
    localNotificationDelegate = UNUserNotificationCenter.Current.Delegate; 
    
    // Assign this class (AppDelegate) as the actual UNUserNotificationCenter delegate
    UNUserNotificationCenter.Current.Delegate = this;
}
// This method is called if a notification (push or local) is about to be displayed
// when the app is in the foreground.
[Export("userNotificationCenter:willPresentNotification:withCompletionHandler:")]
public void WillPresentNotification(UNUserNotificationCenter center, UNNotification notification,
    Action<UNNotificationPresentationOptions> completionHandler)
{
    // Forward call to LocalNotification plugin,
    // but don't let it call the completionHandler because we want to call it ourselves.
    localNotificationDelegate?.WillPresentNotification(center, notification,
        (UNNotificationPresentationOptions _) => { });
    
    // Tell the system to display the notification
    completionHandler(UNNotificationPresentationOptions.Alert);
}
// This method is called when the user taps a notification (push or local).
[Export("userNotificationCenter:didReceiveNotificationResponse:withCompletionHandler:")]
public void DidReceiveNotificationResponse(UNUserNotificationCenter center, UNNotificationResponse response, Action completionHandler)
{
    // Forward call to LocalNotification plugin,
    // but don't let it call the completionHandler because we want to call it ourselves.
    localNotificationDelegate?.DidReceiveNotificationResponse(center, response, () => { });
    
    // Code for handling push notification goes here
    
    completionHandler();
}

@thudugala, any thoughts? Maybe a future version of the plugin could accept a UNUserNotificationCenter delegate parameter somewhere. I could then pass my AppDelegate to the plugin and it could forward any calls it can’t handle to me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Notifications on my iPhone are not working
No longer receive notifications on apps that I set up to receive them ... Since notifications stopped working I decided to update to...
Read more >
Fix iPhone Notifications Not Working after iOS 16/17 Update
If you've turned on this feature, there will be a moon icon in the status bar. And you will find notification sound doesn't...
Read more >
Troubleshooting Duo Push notification issues on iOS devices
Information · 1. Notifications not enabled · 2. Issues with Apple Push Notification Service (APNs) · 3. Issues caused by Wi-Fi Assist and...
Read more >
How to Enable Push Notifications on iOS Devices
Tap on the Settings icon from your Home Screen. Click on the Settings icon · Scroll to find and tap on Mail. Click...
Read more >
Setting up iOS Push Notifications
Step 1: Enable push notifications in Xcode · Step 2: Enable push notifications in the Apple Developer Portal · Step 3: Create and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found