当应用处于后台/关闭时,当通知被点击时,Ionic 3 FCM打开特定页面

时间:2018-04-17 04:13:39

标签: firebase ionic-framework firebase-cloud-messaging ionic-native cordova-plugin-fcm

当我的应用程序处于后台/关闭时,我可以获得推送通知,当我点击它并打开时,我可以在通知的有效负载中获取数据

"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
}

但有一个问题是我在后台/关闭时点按通知后无法打开特定页面。

以下是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');未被触发。我不知道为什么请帮助我!

1 个答案:

答案 0 :(得分:1)

所以问题已经解决了。

像@SurajRao建议的那样,我没有使用<input id="btn-Preview-Image" type="button" value="Preview"/> <div id="previewImage"></div> <a id="btn-Convert-Html2Image" href="#">Download</a> ,而是遵循了他的建议,并将我的代码更新为fcm.onNotification().subscribe(function(data) {,并且确实有效。

相关问题