当应用程序处于后台时,如何在Ionic 3中单击推送通知时打开特定页面?

时间:2017-10-26 19:31:50

标签: ionic3 phonegap-pushplugin

我已经集成了PhoneGap推送通知插件。我能够在Android中接收通知,并且当应用程序位于前台时我能够打开特定页面。当用户在后台时点击通知但我无法找到任何解决方案时,我想打开一个特定页面。我甚至试过了下面的链接。

https://medium.com/@ankushaggarwal/push-notifications-in-ionic-2-658461108c59

这是我的代码

initPushNotification() {
    if (!this.platform.is('cordova')) {
      console.warn('Push notifications not initialized. Cordova is not available - Run in physical device');
      return;
    }
    const options: PushOptions = {
      android: {
        senderID: 'YOUR_SENDER_ID'
      },
      ios: {
        alert: 'true',
        badge: false,
        sound: 'true'
      },
      windows: {}
    };
    const pushObject: PushObject = this.push.init(options);

    pushObject.on('registration').subscribe((data: any) => {
      console.log('device token -> ' + data.registrationId);
      //TODO - send device token to server
    });

    pushObject.on('notification').subscribe((data: any) => {
      console.log('message -> ' + data.message);
      //if user using app and push notification comes
      if (data.additionalData.foreground) {
        // if application open, show popup
        let confirmAlert = this.alertCtrl.create({
          title: 'New Notification',
          message: data.message,
          buttons: [{
            text: 'Ignore',
            role: 'cancel'
          }, {
            text: 'View',
            handler: () => {
              //TODO: Your logic here
              this.nav.push(DetailsPage, { message: data.message });
            }
          }]
        });
        confirmAlert.present();
      } else {
        //if user NOT using app and push notification comes
        //TODO: Your logic on click of push notification directly
        this.nav.push(DetailsPage, { message: data.message });
        console.log('Push notification clicked');
      }
    });

    pushObject.on('error').subscribe(error => console.error('Error with Push plugin' + error));
  }

2 个答案:

答案 0 :(得分:1)

注入App;

import {App } from "ionic-angular";
constructor(private app: App){...}

然后;

var nav = this.app.getActiveNav();
nav.push(DetailsPage, { message: data.message });

答案 1 :(得分:0)

使用以下内容: -

this.nav.push(DetailsPage, {'message': data.message});

并在Detailspage.ts构造函数中使用Navparams获取此消息: -

message:any;
this.message = this.navParams.get('message');

现在,您可以使用{{message}}

在detailspage.html中显示此消息