Ionic 在推送通知后未导航到指定的 URL

Ionic does not navigate to specified URL after push notification

我有一个 ionic 聊天应用程序,它从 firebase 云功能接收推送通知...您单击该图标并打开该应用程序。问题是在后台收到通知时没有导航到指定页面,只有在前台。

这是我的应用组件:

  initializeApp() {
    if (!firebase.apps.length) {
      firebase.initializeApp(firebaseConfig);
    }

    this.platform.ready().then(() => {
        const unsubscribe = firebase.auth().onAuthStateChanged( user => {
          if (!user) {
            this.router.navigateByUrl('login');
            unsubscribe();
          } else {
            this.router.navigateByUrl('tabs');
            unsubscribe();
          }
        });
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      this.getDeviceToken();
    });
  }

  getDeviceToken() {
    this.fcm.onNotification().subscribe(data => {
      if (data.wasTapped) {
        this.router.navigateByUrl('tabs/connections');

        console.log("Received in background", data.wasTapped);
      } else {
        console.log("Received in foreground", data.wasTapped);
        this.router.navigateByUrl('tabs/connections');
      }
    });

    this.fcm.onTokenRefresh().subscribe(token => {
      // Register your new token in your back-end if you want
      this.fcmService.saveTokenToFirestore(token, this.loggedInUser.uid);
    });
  }

如果应用程序打开并且在前台收到推送通知,它将导航到指定页面,但在关闭时不会。

我怀疑当我打开应用程序时出现的这些错误与它有什么关系......它们在推送通知之前就已经存在并且它们没有妨碍任何功能......只是认为值得一提:

Failed to load resource: the server responded with a status of 404 ()
polyfills.js:3040 Unhandled Promise rejection: Error Status 404: App not found ; Zone: <root> ; Task: Promise.then ; Value: Error: Error Status 404: App not found
    at IonicDeployImpl.<anonymous> (/plugins/cordova-plugin-ionic/dist/common.js:291)
    at step (/plugins/cordova-plugin-ionic/dist/common.js:37)
    at Object.next (/plugins/cordova-plugin-ionic/dist/common.js:18)
    at fulfilled (/plugins/cordova-plugin-ionic/dist/common.js:9)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (polyfills.js:2749)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (polyfills.js:2508)
    at polyfills.js:3247
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (polyfills.js:2781)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (polyfills.js:2553)
    at drainMicroTaskQueue (polyfills.js:2959) Error: Error Status 404: App not found
    at IonicDeployImpl.<anonymous> (http://localhost/plugins/cordova-plugin-ionic/dist/common.js:291:35)
    at step (http://localhost/plugins/cordova-plugin-ionic/dist/common.js:37:23)
    at Object.next (http://localhost/plugins/cordova-plugin-ionic/dist/common.js:18:53)
    at fulfilled (http://localhost/plugins/cordova-plugin-ionic/dist/common.js:9:58)
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invoke (http://localhost/polyfills.js:2749:26)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.run (http://localhost/polyfills.js:2508:43)
    at http://localhost/polyfills.js:3247:34
    at ZoneDelegate.push../node_modules/zone.js/dist/zone.js.ZoneDelegate.invokeTask (http://localhost/polyfills.js:2781:31)
    at Zone.push../node_modules/zone.js/dist/zone.js.Zone.runTask (http://localhost/polyfills.js:2553:47)
    at drainMicroTaskQueue (http://localhost/polyfills.js:2959:35)

否则,我不会在控制台中收到任何错误。此外,它不是控制台日志记录 "console.log("在后台接收”,data.wasTapped);”

它将console.log "console.log("前台接收", data.wasTapped);"

再次强调,这是带有 Firebase 的 Ionic 4...任何帮助将不胜感激。

我至少能够在控制台记录数据被窃听 = true。但是现在我收到了这个错误:

导航在Angular区外触发,你是不是忘了调用'ngZone.run()'?

终于明白了。我正在使用选项卡模块。所以为了让它工作,我不得不把 fcm.onNotification 放在主选项卡模块中。现在当应用程序处于后台时它可以工作

我遇到了同样的问题。 但后来我尝试使用 https://cordova-plugin-fcm.appspot.com/ 发送通知。 因此,在从给定 link 发送通知后,当我单击通知时,它会重定向到所需的页面。 所以我开始知道我的代码没有问题但是发送通知的代码有问题。 有关更多信息,请阅读本文 https://enappd.com/blog/implement-ionic-4-firebase-push/34/