cordovapush-notificationionic2cordova-plugin-fcm

fcm.onNotification() not getting called on clicking the notification when the app is in background


I have installed cordova-plugin-fcm and everything works fine except one little thing. When the app is in background/closed and push notification is sent from firebase, notification pops up in the devices. On clicking that notification from the tray my application starts run, but the control is not entering into fcm.onNotification().

My code in app.component.ts looks like this

   fcm.onNotification().subscribe(data=>{
     if(data.wasTapped){
       console.log("Received in background");
       console.log(data);
     } else {
      console.log("Received in foreground");
      console.log(data);
     };
   });

Solution

  • the notification should have "click_action":"FCM_PLUGIN_ACTIVITY" for the onNotification() to be fired, so it would not work if you are sending it from firebase console, send the notification using http reqquest, follow Firebase Cloud Messaging HTTP Protocol documentation form more insights , and i recommend Postman to do it, it is also a chrome plugin.

    your code should be as follow:

    {
      "notification":{
        "title":"Notification title",
        "body":"Notification body",
        "sound":"default",
        "click_action":"FCM_PLUGIN_ACTIVITY", //this is needed so the onNotification() fires when notification is tapped
        "icon":"fcm_push_icon"
      },
      "data":{
        "param1":"value1",
        "param2":"value2"
      },
      "to":"/topics/topicExample"(or device token),
      "priority":"high"
    }
    

    references:

    those two links have everything you need

    good luck.