【问题标题】:fcm.onNotification() not getting called on clicking the notification when the app is in background当应用程序处于后台时,单击通知时未调用 fcm.onNotification()
【发布时间】:2018-02-07 23:02:32
【问题描述】:

我已经安装了cordova-plugin-fcm,除了一件小事,一切正常。当应用程序处于后台/关闭状态并且从 Firebase 发送推送通知时,设备中会弹出通知。在从托盘单击该通知时,我的应用程序开始运行,但控件未进入 fcm.onNotification()。

我在 app.component.ts 中的代码是这样的

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

【问题讨论】:

    标签: cordova push-notification ionic2 cordova-plugin-fcm


    【解决方案1】:

    通知应该有 "click_action":"FCM_PLUGIN_ACTIVITY" 用于触发 onNotification(),因此如果您从 firebase 控制台发送通知,使用 http reqquest 发送通知,关注 Firebase Cloud Messaging HTTP Protocol documentation 形成更多见解,我将无法正常工作推荐Postman做,也是个chrome插件。

    您的代码应如下所示:

    {
      "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"
    }
    

    参考:

    这两个链接有你需要的一切

    祝你好运。

    【讨论】:

    • 太棒了!是工作。只需添加click_action="FCM_PLUGIN_ACTIVITY",然后调用函数fcm.onNotification()。谢谢
    • @JoeSleiman 您收到任何错误吗?您是否尝试过上述方法?,您是如何发送通知的?
    • 是的,我已经做了和上面一样的事情,没有任何改变......而且没有错误......我正在尝试通过后端通知
    • 所以要清楚,您正在收到通知,但它没有进入 fcm.onNotification()。或者您根本没有收到通知?
    • 是的,我收到了通知,但 fcm.onNotification 没有触发,我也有本地通知插件,但我删除了它,但仍然无法正常工作!您是否建议我删除平台然后安装它,否则可能会损坏其他东西
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 1970-01-01
    • 2017-11-06
    相关资源
    最近更新 更多