Ionic 3:本地通知“点击”事件APP CLOSED无效

时间:2017-12-06 17:35:05

标签: android ios ionic-framework ionic3

我在我的离子3项目(最新版本)上使用本地通知本机插件,但是当我点击通知并且我的应用程序关闭时,不会触发点击事件。 当app处于后台或前台时,它可以正常工作。

我在提供程序中使用本地通知,而我的on click代码在其构造函数中,但是当应用程序关闭时,它无效。 我已经尝试在app / app.component.ts中编写平台内的代码,但这种方法不起作用。 这是我的代码:

应用程序/ app.component.ts

export class MyApp {
  @ViewChild(Nav) nav: Nav;
  rootPage: any;

constructor(
public platform: Platform,
public menu: MenuController,
public statusBar: StatusBar,
public splashScreen: SplashScreen,
public BeaconServiceProvider: BeaconServiceProvider, /* my provider with local notifications*/
public RemoteServiceProvider: RemoteServiceProvider,
public translate: TranslateService,
public globalization: Globalization,
private oneSignal: OneSignal,
public ga: GoogleAnalytics
  ) {
......
}
}

我的提供者

import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Storage } from '@ionic/storage';
import { IBeacon } from '@ionic-native/ibeacon';
import { LocalNotifications } from '@ionic-native/local-notifications';
import { RemoteServiceProvider } from '../remote-service/remote-service';
import { StorageProvider } from '../storage/storage';
import { TranslateService } from '@ngx-translate/core';
import { AlertController } from 'ionic-angular';
import { NavController, App} from "ionic-angular/index";
import { GoogleAnalytics } from '@ionic-native/google-analytics';

@Injectable()
export class BeaconServiceProvider {

  remoteBeacons: Array<{major: string, minor: string}> = [];
  localBeacons: Array<{major: string, minor: string, date: any}> = [];
  bluetoothInit: boolean = false;
  private navCtrl: NavController;

  constructor(
    public http: HttpClient,
    public alertCtrl: AlertController,
    public storage: Storage,
    public ibeacon: IBeacon,
    public translate: TranslateService,
    public localNotifications: LocalNotifications,
    public RemoteServiceProvider: RemoteServiceProvider,
    public StorageProvider: StorageProvider,
    public ga: GoogleAnalytics,
    private app: App
  ) {
    console.log('Hello BeaconProvider Provider');

    this.localBeacons = this.StorageProvider.localBeacons;

    this.navCtrl = this.app.getActiveNav();

    let thiz_app = this.app;
    let remote = this.RemoteServiceProvider;
    this.localNotifications.on("click", function(notification, state){

      this.ga.trackEvent(
        "Notifiche Beacon (" + remote.version_code + ")",
        "Apertura notifica",
        "Click sulla notifica beacon " + " {" + notification.message + "}",
    1
     );

     let nav = thiz_app.getActiveNav();

     let data = JSON.parse(notification.data);
     let page_id = data.page_id;
     page_id = page_id.toString();

    switch(page_id)
    {
       case "" :
       case "0" :
          break;
    case "-1" : //Contact Page
        if (nav == null)
          nav.push('ContactPage');
        else if (nav.getActive().component.name != 'ContactPage')
          nav.push('ContactPage');
      break;
    default :

      remote.isPageListNew(page_id).subscribe(result => {
          let page = result.has_subpages ? 'List2Page' : 'ItemDetailsPage';

          if (nav == null)
            nav.push(page, { item: result });
          else
          {
            if( nav.getActive().component.name != page)
              nav.push(page, { item: result });
            else
            {
              let item_id = nav.getActive().data.item.id;
              if (item_id != result.id)
                nav.push(page, { item: result });
            }
          }
      });

  }

});

  }

为什么我的代码不起作用的任何想法? 感谢

0 个答案:

没有答案