dpa99c / cordova-plugin-firebasex

Cordova plugin for Google Firebase
MIT License
570 stars 457 forks source link

Push Notifications not received when app is in background #851

Open hal-berto opened 5 months ago

hal-berto commented 5 months ago

Bug report

CHECKLIST

Current behavior:

Notification messages are received only when app is in foreground. If the app is in background or is closed, notifications are no more received.

Expected behavior:

Notifications should be received even if the app is in background or is closed

Environment information

Runtime issue

Related code:

In app.component.ts following code is executed in function ngOnInit():

this.platform.ready().then(() => {

      this.firebase.grantPermission().then(hasPermission => {
        console.log(":::::HAS PERMISSION: ", hasPermission);
      }).catch(e => {
        console.log(":::::HAS PERMISSION ERROR: ", e);
      });

      this.firebase.getToken().then(token => {
        this.sendToken(token!);
      }).catch(e => {
        // Error
      });

      this.firebase.onMessageReceived().subscribe(data => {
        console.log(":::::::RECEIVED NOTIFICATION: "+JSON.stringify(data));
        let tipoNotifica = data['tipo'];
        let dettaglioId = data['eventoid'];
        let titolo = data['title'];
        let testo = data['testo'];

        if(data.tap){
          this.navigazionePaginaDettaglio(dettaglioId, tipoNotifica);
        }else{
          alert("Nuova notifica ricevuta.\nTipo: " + tipoNotifica + "\nTitolo: " + titolo);
          this.navigazionePaginaDettaglio(dettaglioId, tipoNotifica);
        }

      });

    });

The code in this.firebase.grantPermission() is ignored, no permission is requested and no logs are written in console (but it should be correct, at least if I correctly understood what's written in the documentation about plugin usage under Android).

I also tried to add following configuration in config.xml file:

With this configuration when application is executed on recent phones (android 13+) a message appears asking to give permission to receive notifications, but nothing changes. Even with permission granted, no notification is received when app is in background.

Other information:

This issue seems to be present in recent versions of cordova platform. In apps developed with older versions of cordova (for example cordova 6.3.0) notifications are received.

xJosee commented 5 months ago

In my case the push notification only arrives when I am in the background, when I am inside the app it does not arrive.

Does anyone know if it is a configuration issue that I am missing?

hal-berto commented 5 months ago

Strange thing is I tried with: -Test project provided with this same plugin -A clean cordova project with only firebasex plugin installed -A clean capacitor project with only the push plugin (in this case it's not the cordova-plugin-firebasex, but a similar one)

In all these cases I experienced same issue: notifications are not received when the app is closed... Isn't it a way to receive notifications on newer phones?

dpa99c commented 5 months ago

Here is a screen recording showing the test project built with the latest plugin version (v16.3.0) and cordova-android@12 running on a Google Pixel 5a. It shows the app receiving push notifications while in the foreground, background and not running (task killed).

@hal-berto I notice your test device is an Asus. As outlined in https://github.com/dpa99c/cordova-plugin-firebasex/issues/235, some non-Google OEMs (including Asus) have issues receiving push notifications from FCM when the app is not running (vs running in the background). The proposed solution was to integrate the Autostarter library into this plugin. However it appears this library is no longer maintained (no update for 2 years) so maybe an alternative needs to be found which is maintained before making any integration with this plugin.