capacitorquasarlocalnotificationcapacitor-plugin

Handling click events on Capacitor Local Notifications when the app is closed


I'm encountering an issue within my app that utilizes Capacitor with Quasar. We're utilizing the @capacitor/local-notifications plugin to handle local notifications, and we have a method that gets triggered when the notification is clicked, using the following listener:

await LocalNotifications.addListener('localNotificationActionPerformed', handleLocalNotificationAction);

However, when the app is closed (not in the foreground), this listener obviously doesn't fire. We're in need of a way to execute a method upon notification click, such as opening the app at a specific URL or performing a certain action even if the app is closed.

We've tried some things, like:

Thanks in advance for your help!


Solution

  • After some tries I figured out how to solve my problem. Basically the listener was being called, but the method that I used to handle had some dependencies that were not loaded (in my case it was Vue Router).

    I just added a retry method that will wait the boot time and try again and it turned out to be working!

    I'll post my function, maybe it can help someone:

    const openAccordingToPlatform = async (params) => {
      if (Platform.is.capacitor) {
        pushWithRetry(params);
        return;
      }
    
      const url = router().resolve(params).href;
      openOnNewTab(url);
    };