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

£可爱£侵袭症+ 提交于 2021-02-07 18:15:56

问题


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);
     };
   });

回答1:


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:

  • github rep

  • Firebase Cloud Messaging HTTP Protocol

those two links have everything you need

good luck.



来源:https://stackoverflow.com/questions/45956891/fcm-onnotification-not-getting-called-on-clicking-the-notification-when-the-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!