无法调用Ionic 4 FCM onNotification

时间:2019-11-21 13:02:27

标签: cordova ionic-framework firebase-cloud-messaging

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

我可以在控制台中看到FCMPlugin.js: is createdFCMPlugin Ready OK,再往后几行,我可以看到console.log,我添加了该文件以确认正在调用this.platform.ready(在这里我正在fcm.onNotification内订阅app.component.ts

值得注意的是,当应用关闭或暂停时,我会收到通知,只是当应用处于前台时,当收到通知以提醒用户以及点击通知时,我无法捕获无法读取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

1 个答案:

答案 0 :(得分: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观察值仅在应用程序处于前台时才会触发。当它关闭或暂停时,您会收到通知并点击它,则不会触发onNotification可观察对象。 :(