Ionic2 check what page you're currently on

How do you check what page you’re currently on from an event listener in app.ts?

I have a messaging app, and it receives notifications, if a publisher sends a message to a subscriber. When it receives that message (on an event), it pops up a message. If the user is on the chats.ts page, I don’t want it to show the pop up. The thing is the event listener is on app.ts.

So how from the event listener in app.ts can I determine what page the user is on?

I thought I could maybe use this.viewCtrl.name, but the View controller is not available in app.ts.

app.ts

      push.on('registration', (data1) => {
        this.events.subscribe('messages:notify', (data) => {
          this.notificationService.push('null', data[0].topic, data[0].message, data[0].title);
        });
      });

      push.on('notification', (data) => {
        //if user using app and push notification comes
        if (data.additionalData.foreground) {
          //if application open, show popup
          let confirmAlert = this.alertCtrl.create({
            title: data.title,
            message: data.message,
            buttons: [{
              text: 'Ignore',
              role: 'cancel'
            }, {
                text: 'View',
                handler: () => {
                  this.rootPage = ChatsPage;
                }
              }]
          });
          confirmAlert.present(confirmAlert);
        } else {
          this.rootPage = ChatsPage;
        }
      });

chats.ts

        `this.events.publish('messages:notify', data);`

Take a look at the NavController API (http://ionicframework.com/docs/v2/api/components/nav/NavController/) It might have what you are looking for.

Thanks Chris. But is far as I know, the Nav Controller is not accessible from app.ts. I have tried to access it, but get errors. Unless you know how to access it in app.ts?

@richardmarais Inject a Nav ViewChild instance into your app.ts instead.

import { Component, ViewChild } from '@angular/core';
import { ionicBootstrap, Platform, Nav } from 'ionic-angular';

class App {
    @ViewChild(Nav) nav: Nav;
}

Thank you. That works

[quote=“richardmarais, post:5, topic:64329, full:true”]Thank you. That works
[/quote]
No problem! Glad to hear that.

Could you please flag your post as having been answered so that others with a similar problem that is searching the forums will know that this thread has a (potential) solution. Thanks and good luck with your project, @richardmarais!