No listeners found for event notificationReceived

I am using the pushNotifications module along with firebase to send push notifications to Android. Firebase integration is working and I am able to register the device and retrieve the token. I am also able to trigger a test message with the token, however, I get the following issue.

When I trigger a push notification, Android Studio logcat indicates the following:

Notifying listeners for event notificationReceived
No listeners found for event notificationReceived

Based on the following (from the documentation), the event listener is called “pushNotificationReceived” and I have the following code:

PushNotifications.addListener(
          "pushNotificationReceived",
          (notification) => {
            alert("Push received: " + JSON.stringify(notification));
          }
        );

Given that the above indicates that there must be a listerened called notificationReceived, I have also added the following listener:

PushNotifications.addListener("notificationReceived", (notification) => {
        alert("Push action performed: " + JSON.stringify(notification));
      });

From the console I can see that the listeners are registered:

To native (Capacitor plugin): callbackId: 96304817, pluginId: PushNotifications, methodName: addListener
callback: 96304817, pluginId: PushNotifications, methodName: addListener, methodData: {"eventName":"notificationReceived"}
To native (Capacitor plugin): callbackId: 96304820, pluginId: PushNotifications, methodName: addListener
callback: 96304820, pluginId: PushNotifications, methodName: addListener, methodData: {"eventName":"pushNotificationReceived"}

I am not sure what else to do here as it appears at face value that the event listener for both pushNotificationReceived and notificationReceived is created, but not implimented.

The listener to register is definitely pushNotificationReceived and not notificationReceived.

Please show some more of your code. Where are you registering the listener?

As an update, the notifications do come through if the app is in idle / closed state (not foreground). If the app is open then notifications does not come through. It appears that this is by design and some articles indicate that if I want push notifications to come through I need to create another listener / custom notification event handler.

Is this correct? Happy to share code if that will help?

Are you on Capacitor 6? If so, then you just need to set presentationOptions in your Capacitor config to show when the app is in the foreground. This was actually fixed in Push Notifications v5.1.0 for Android. Previous to that, you had to create a local notification within the pushNotificationReceived listener if you wanted to show a push notification properly when the app was in the foreground.

Thank you, I am using 6, I have the following set in my capacitor.config.js:

{
  "appId": "app.example.net",
  "appName": "App Name",
  "webDir": "www",
  "plugins": {
    "PushNotifications": {
      "presentationOptions": ["badge", "sound", "alert"]
    }
  }
}

If the app is open, logcat reports:
No listeners found for event notificationReceived

If the app is closed / background, I get the push notification.

Where are you registering pushNotificationReceived?

Can you share your list of plugins?

Are you using @capacitor-firebase/messaging by any chance?

Yes I installed this, I wasnt able to get the firebase integration working without this, it indicated that firebase was not initialized correctly. Installed it afterwards and firebase worked.

d----- 2024/09/10 09:59 @capacitor
d----- 2024/09/10 17:02 @capacitor-firebase
d----- 2024/09/10 17:02 @firebase

I initlized this as part of my mainlayout in vue. I have added some conditional to ensure I dont re-register it during page loads:

      if (!this.isPushNotificationReceivedListenerAdded) {
        PushNotifications.addListener(
          "pushNotificationReceived",
          (notification) => {
            alert("Push received: " + JSON.stringify(notification));
          }
        );
        this.isPushNotificationReceivedListenerAdded = true;
      }

Yeah, the problem is that plugin intercepting the notifications before @capacitor/push-notifications do, use that plugin listener instead of the @capacitor/push-notifications one.

1 Like