Ionic 3 FCM 在应用程序处于 background/closed 时点击通知时打开特定页面

Ionic 3 FCM open specific pages when notification tapped when app is in background/closed

我可以在我的应用程序打开时收到推送通知 background/closed 并且当我点击并打开它时我可以在通知的有效负载中获取数据

"notification": {
    "title": "Chat Messages",
    "body": `You have received chat messages from ${userName}`,
    "sound": "default",
    "click_action":"FCM_PLUGIN_ACTIVITY",
    "icon":"fcm_push_icon"
},
"data": {
    "user_id" : from_user_id,
    "user_name" : userName,
    "type" : notification_type
}

但是在background/closed.

中点击通知后出现无法打开特定页面的问题

这是我在 app.components.ts 中的代码:

import { Component, ViewChild } from '@angular/core';
import { Platform, NavController } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { FCM } from '@ionic-native/fcm';

import { LoginPage } from '../pages/login/login';
import { TestpagePage } from '../pages/testpage/testpage';

@Component({
   templateUrl: 'app.html'
})
export class MyApp {
@ViewChild('myNav') navCtrl: NavController
rootPage:any = LoginPage;

constructor(fcm: FCM, platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen) {
   platform.ready().then(() => {
  // Okay, so the platform is ready and our plugins are available.
  // Here you can do any higher level native things you might need.
  statusBar.styleDefault();
  splashScreen.hide();

  fcm.onNotification().subscribe(function(data) {
    if(data.wasTapped) {
      alert('HI');
      if(data.type == 'messages') {
        alert('HEY');
        this.navCtrl.setRoot(TestpagePage);
      }

    } else {
      alert('Untapped<br>' +
            'User ID: ' + data.user_id + 
            '<br>User Name: ' + data.user_name +
            '<br>Type: ' + data.type);
     }
   });
  });
 }
}

它将在 if(data.wasTapped) { 中执行 'HI' 和 'HEY' 警报,但似乎没有触发 this.navCtrl.setRoot('TestpagePage');。我不知道为什么请帮助我!

至此问题解决

就像@SurajRao 建议的那样,我没有使用 fcm.onNotification().subscribe(function(data) {,而是听从了他的建议并将我的代码更新为 fcm.onNotification().subscribe((data) => {,并且它有效。