title: Phonegap Firebase Push Notification not firing event listener when app opens, or when app is in background
body: <p>I'm using Phonegap and Firebase (fcm) to receive push notifications.
  I am using Android 8 to test.</p>
  
  <p>I want my device to receive a push notification if the app is running in the foreground (as a popup message in the app).
  If the app is closed or in the background I want the message to be shown as a notification, and when I click on the notification it opens the app in the foreground and calls the notification event function.</p>
  
  <p>Here is what happens so far:</p>
  
  <ul>
  <li><p>When the app is open a push notification comes through and is displayed in the app - CORRECT</p></li>
  <li><p>When the app is minimised a push notification appears in the top bar - CORRECT...but</p></li>
  <li><p>When I click the notification message it opens my app / puts it in foreground, but the "notification" event listener is not called - INCORRECT</p></li>
  </ul>
  
  <p>There are a lot of requests for help regarding this issue and I have gone through many of them but none of them offer a working solution for my app (most are between 2016 and 2018, not 2019).</p>
  
  <p>Primarily this needs to work for Android. Therefore I have tried removing the notification payload and only using the data payload. However this doesn't solve the problem.</p>
  
  <p>Here is my current payload:</p>
  
  <pre><code>$notification = array
  (
  'body'   =&gt; 'test',
  'title'     =&gt; 'test',
  "icon" =&gt;  'notification_icon',
  "content_available" =&gt; true,
  );
  
  $data = array
  (
  "title" =&gt; "test",
  "body" =&gt; "test",
  "icon" =&gt; "notification_icon",
  "content_available" =&gt; true,
  );
  
  $fields = array
  (
  'registration_ids'  =&gt; $registrationIds,
  'notification'      =&gt; $notification,
  'data'          =&gt; $data,
  "priority" =&gt; "high",
  "content_available" =&gt; true 
  );
  </code></pre>
  
  <p>As I understand it there are 3 ways to send a notification:</p>
  
  <p>1) <em>as "notification"</em>. This works in the foreground if the app is currently running, or in the background in the top bar if the app is closed or minimised (and the icon for the app has a little badge above it indicating a message) but when in the background, when the message is clicked on the app loads but the on notification function IS NOT called.</p>
  
  <p>2) <em>as "data"</em>. This ONLY seems to work in the foreground, with no top bar message and the icon for the app does not have a little badge above it. If the app is in the background there is no push notification at all.</p>
  
  <p>3) <em>as BOTH "notificaton" and "data"</em>. When in the background the app icon has a badge above it. When the top bar message is clicked the app is loaded but the on notification function IS NOT called.</p>
  
  <p>Also, even with the app in the background, if it receives a push notification and put the app in to the froeground no notification message is shown. It only works if the app is already in the foreground when the notification is received.</p>
  
  <p>Here are my event listeners:</p>
  
  <pre><code>var app = {
  initialize: function() {
      this.bindEvents();
  },
  bindEvents: function() {
      document.addEventListener('deviceready', this.onDeviceReady, false);
  },
  onDeviceReady: function() {
    console.log('Received Device Ready Event');
    app.setupPush();
  },
  setupPush: function() {
      var push = PushNotification.init({
        android: {
          senderID: 'XXXXXXXXXX',  
          iconColor: "#ccc",
          alert: true,
          badge: true,
          icon: 'notification_icon',
          sound: true,
          vibrate: true,
        },
        browser: {},
        ios: {
          sound: true,
          vibration: true,
          badge: true
        },
        windows: {}
      });
      console.log('after init');
  
      push.on('registration', function(data) {
           //[edit - script not displayed for this example, but it registers a token to enable push notifications to the device. tested and working]
      });
  
      push.on('error', function(e) {
        console.log('push error = ' + e.message);
      });
  
      push.on('notification', function(data) {
         window.alert(data.additionalData);              //Test line
  
        if(data.additionalData.foreground){
          window.alert(data.title+'\n'+data.message);    //Test line
        }
        if(data.additionalData.coldstart){
          window.alert("coldstart");                     //Test line
        }
        showPopup(data,"info");
  
      });
  
  
    }
  };
  
  //Using Sweet Alert to display messages
  
  function showPopup(data,type){
     Swal.fire({
       type: type,
       title: data.title,
       text: data.message,
       showConfirmButton: true,
       imageUrl: data.image,
       imageWidth: 400,
       imageHeight: 200,
      imageAlt: data.title,
      animation: true,
    });
  }
  </code></pre>
  
  <p>Does anybody have an answer to the above that works for Android 7 and 8?</p>