【问题标题】:Ionic 5 Firebase notification - this.fcm.onNotification().subscribe() never firedIonic 5 Firebase 通知 - this.fcm.onNotification().subscribe() 从未触发
【发布时间】:2020-04-22 08:41:23
【问题描述】:

我正在尝试在我的 ionic 应用程序 (ionic 5) 中添加带有 firebase 的通知。我遵循本教程:https://www.positronx.io/ionic-firebase-fcm-push-notification-tutorial-with-example/

我收到令牌,当我从 firebase 控制台发送通知时,通知会显示在手机(模拟器)上,但控制台中不会显示任何内容来处理通知点击...

我将 FCM 添加到 app.modules.ts 的提供中,并在 app.component.ts 中添加了以下代码

import { FCM } from "@ionic-native/fcm/ngx";

...
constructor(
...
    private fcm: FCM
  ) {}

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

      this.statusBar.styleDefault();

      this.splashScreen.hide();
    this.fcm.getToken().then(token => {
      console.log(token);
    });
    this.fcm.onTokenRefresh().subscribe(token => {
      console.log(token);
    });

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

     if (token === null) {
      this.msgService.presentToast(
        "Impossible de configurer la reception des notifications"
      );
    }
    // Observer.hasTokenFCM.next(token);

    if (this.platform.is("ios") || this.platform.is("android")) {
      // this.saveToken(token);
    }
  });

并且已经安装了以下插件: 离子cordova插件添加cordova-plugin-fcm-with-dependecy-updated npm install @ionic-native/fcm

cordova 插件列表: cordova-plugin-fcm-with-dependecy-updated 4.4.0 “Cordova FCM 推送插件”

在我的 package.json 中:

依赖: "@ionic-native/fcm": "^5.22.0", "cordova-plugin-fcm-with-dependecy-updated": "^4.1.1",

“cordova”->“插件”: “cordova-plugin-fcm-with-dependecy-updated”:{ "FCM_CORE_VERSION": "16.0.8", "FCM_VERSION": "18.0.0", "GRADLE_TOOLS_VERSION": "2.3.+", “GOOGLE_SERVICES_VERSION”:“3.0.0” },

非常感谢您知道为什么“this.fcm.onNotification.subscribe”从未被触发...

【问题讨论】:

    标签: android firebase ionic-framework notifications firebase-cloud-messaging


    【解决方案1】:

    要解决此问题,请使用 cordova-plugin-fcm-with-dependecy-updated 插件而不是 @ionic-native/fcm/ngx,它们相互冲突。如果onNotification方法没有被触发并且你无法获取payload,尝试使用getInitialPushPayload

    ...
    const pushPayload = await this.fcm.getInitialPushPayload();
    
    if (pushPayload) {
      this.processPayload(pushPayload);
    }
    
    this.fcm.onNotification().subscribe(
      (payload: PushNotification) => this.processPayload(payload)
    );
    ...
    

    【讨论】:

    • 打扰一下,您已经知道如何在 iOS 上修复它了吗?谢谢!
    • @M.Mariscal 对我来说,它适用于 Android 和 iOS 平台,所以我认为您可以直接尝试,无需任何更改。
    【解决方案2】:

    显然我需要使用

    this.firebase.onMessageReceived().subscribe(notification => {
     if (notification["tap"]) {
    ...
    }
    ....
    });
    

    使用 firebase 插件来处理通知...但我不知道为什么。

    【讨论】:

    • 我也有类似的问题,请问您是如何解决的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 2018-09-20
    • 1970-01-01
    • 1970-01-01
    • 2018-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多