Capacitor: phonegap-plugin-push event on notification recieved not being fired

Created on 20 Oct 2018  路  8Comments  路  Source: ionic-team/capacitor

Trying to get the phonegap-plugin-push working with Capacitor and have some problems.

For starters the project wont build if I first dont in the build.gradle-file change the firebase-messaging version to 17.1.0 instead of 11.6.2. I get the following error with 11.6.2:

The library com.google.android.gms:play-services-basement is being requested by various other libraries at [[15.0.1,15.0.1]], but resolves to 11.6.2. Disable the plugin and check your dependencies tree using ./gradlew :app:dependencies.

Using 17.1.0 the project is built and the app launches.

let push = PushNotification.init({'android': {}})
    push.on('registration', (token) => {
      console.log('Token: ' + token)
      if (token) {
        Device.getInfo().then((result) => {
          _this.registerDeviceForPushNotifications(result.platform, token)
        })
      }
    })

    push.on('notification', (data) => {
      console.log('Notification: ' + JSON.stringify(data))
    })

The registration-event is fired after init has been called. When sending a test-notification via my Azure Notification Hub the notification-event isn't fired. The following two lines shows in the logcat though:

V/Capacitor/Plugin/PushNotifications: Notifying listeners for event pushNotificationReceived
D/Capacitor/Plugin/PushNotifications: No listeners found for event pushNotificationReceived

I'm not using Capacitors PushNotifications-plugin in any way.

Any ideas what's going on? Starting to hate everything regarding Push Notifications...

Most helpful comment

Yeah, it's probably that, I don't think it can work now that Capacitor has it's own class for FirebaseMessagingService

You can try removing this

<service android:name="com.getcapacitor.CapacitorFirebaseMessagingService" android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

All 8 comments

One of my coworkers had the same issue today and we decided to go with the capacitor plugin instead, which works for us. I think that the error might happen somewhere in between Capacitor and the Phonegap plugin... maybe it's related to how Capacitor emits the event vs how Cordova would do it? Because pushNotificationReceived is the name of the event that Capacitor emits through its native plugin system, but the Phonegap plugin waits for an event named notification.

Maybe you could actually try doing push.on('pushNotificationReceived', console.log) instead of notification?

@simonhaenisch

Thank you for your reply. I actually started implementing Push Notifications using Capacitors own plugin but left it because it's not finished yet. If I may ask, how does the Capacitor plugin in its current state work for you? How do you show the recieved notification in the Notification Center for example? Or update the icon badge etc...?

Maybe you could actually try doing push.on('pushNotificationReceived', console.log) instead of notification?

Thanx for the suggestion. I will try that

I have tried push.on('pushNotificationReceived', console.log) but that doesn't work either

I have the same problem when using phonegap-plugin-push. On the other hand, I also try to use PushNotifications-plugin offered by capacitor, the pushNotificationReceived listener is trigered when a push notification is send via the Firebase console, but the PushNotification of listenerFunc always reponse the empty data, part of the code is shown below,

  PushNotifications.addListener('registration', (PushNotificationToken) => {
    console.log('token ' + PushNotificationToken.value);
    PushNotifications.addListener('pushNotificationReceived', (PushNotification) => { 
      console.log('notification ' + JSON.stringify(PushNotification));
    });
  });
  PushNotifications.register();

Does anyone know the idea?
Thanks

@solojuve1897 's original comment is spot on; I just would like to add that Capacitor complains about there not being any subscribers to the 'onNotificationReceived' event (which is normal since we are using phonegap-plugin-push here instead), but at least this testifies that the payload message is correctly received on the device. If you were to use Capacitors' API to subscribe to the 'onNotificationReceived' event (along side your phonagap-plugin-push setup) then you would get to handle the payload.

The question is, why is the 'notification' event from the phonegap plugin not called?

@jcesarmobile told me that phonegap-plugin-push probably stopped working when they added Capacitor's push plugin. The capacitor plugin would now be receiving the events instead of the phonegap plugin.

I guess this will be a 'wontfix' then

Yeah, it's probably that, I don't think it can work now that Capacitor has it's own class for FirebaseMessagingService

You can try removing this

<service android:name="com.getcapacitor.CapacitorFirebaseMessagingService" android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Yeah, it's probably that, I don't think it can work now that Capacitor has it's own class for FirebaseMessagingService

You can try removing this

<service android:name="com.getcapacitor.CapacitorFirebaseMessagingService" android:stopWithTask="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

You saved my life. Thank you

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ebk46 picture ebk46  路  3Comments

nicobytes picture nicobytes  路  3Comments

json-derulo picture json-derulo  路  3Comments

daniel-lucas-silva picture daniel-lucas-silva  路  3Comments

bogdbo picture bogdbo  路  3Comments