Ionic 4 FCM onNotification 未被调用

Ionic 4 FCM onNotification not being called

我正在使用 cordova-plugin-fcm-with-dependecy-updated 接收推送通知,但 this.fcm.onNotification 可观察对象未被执行。我可以在 Xcode 控制台中看到正在接收消息,并且单步执行代码我可以看到 -(void) notifyOfMessage:(NSData *)payload 是从 FCMPlugin.m 中调用的,但我无法确定为什么 this.fcm.onNotification 未被调用。

我可以在控制台中看到 FCMPlugin.js: is createdFCMPlugin Ready OK,再往下几行我可以看到 console.log 我添加以确认正在调用 this.platform.ready (这是我从 app.component.ts 中订阅 fcm.onNotification 的地方)

值得注意的是,当应用程序关闭或暂停时,我会收到通知,只是当应用程序处于前台时,我无法捕获收到通知以提醒用户的时间,以及点击通知我无法读取 wasTapped 属性 将用户重定向到特定页面(因为未触发 onNotification)。

有人知道问题的原因吗?

我正在使用:

@Ionic/angular: ^4.11.4
cordova-android: ^8.1.0
cordova-ios: ^5.0.1
cordova-plugin-fcm-with-dependecy-updated: ^4.1.0
FCM_CORE_VERSION: 16.0.9
FCM_VERSION: 18.0.0,
GRADLE_TOOLS_VERSION: 3.5.0,
GOOGLE_SERVICES_VERSION: 4.2.0

我已经找到了满足我当前需求的可怕解决方法,但并没有完全解决手头的问题。

对于iOS,而不是将下面的代码放在app.component.ts constructor中:

this.platform.ready().then(() => {
  this.fcm.onNotification().subscribe(data => { 
    // Do stuff here
  });
});

我把下面的代码放在this.initializeApp()中:

async this.initializeApp() {

  try { 

    let isReady = await this.platform.ready();

    if (isReady) {

      if (this.platform.is('ios') === true) {

        this.fcm.onNotification().subscribe(data => { 
          // Do stuff
        });

      }

    }

  } catch (err) {

    // Deal with error

  }

  // Do other app init stuff

}

为什么 await 而不是 this.platform.ready().then() 对我来说毫无意义,但无论出于何种原因,上述内容都适用于 iOS.

为了让 onNotification 为 Android 工作,我需要将以下内容放入 app.component.ts constructor:

if (this.platform.is('android') === true) {

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

    setTimeout(() => {

      this.fcm.onNotification().subscribe(data => { 
        // Do stuff here
      });

    }, 100);

  });

}

同样,我不知道为什么会这样。但在这个问题上花了几天时间,这就是我想出的一个可行的解决方案。

不过上面的 Android 代码存在问题。 onNotification observable 只会在应用程序位于前台时触发。当它关闭或暂停时,您会收到通知并点击它,onNotification observable 将不会被触发。 :(