[Solved] LocalNotifications.on("click") not triggering, when is app is opened after `full close`


#1

Hi,
I am making use of Ionic Native’s LocalNotifications to have reminders in app.
While App is opened,

(1) when I press Home button the app goes to background. In this case when I click notification from notification bar, app is brought to Foreground (resumed in same screen where it’s closed), the on("click") event is triggered.
(2) When I press Back button the app is removed from Memory, I guess. In this case, when I click notification from notification bar, app is opened via Splash (the screen where it’s closed was not resumed), and the on("click") event is not triggered.

Tried the same with on("trigger"), same happens.

I tried enabling Background Service, by installing cordova-plugin-background-mode and calling BackgroundMode.enable(). It didn’t help too.

I want the event trigger to happen, no matter how the app is opened.

Any pointers will be highly appreciated.
Thanks.


#2

Solved:
My code was in a Components which doesn’t load at start.
When I moved the code to app/app.component.ts, it worked.


#3

Hi,
i’m using local notifications native plugin on my ionic 3 project, but when i click on notification and my app is closed the click event is not triggered.
It works when app is in background or foreground.

I use local notifications inside a provider and my on click code is inside its constructor but when app is closed is not work.
I’ve tried too write code inside platform ready in app/app.component.ts but not work.
This is my code:

app/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
  ) {
......
}
}

my provider

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';

/*
  Generated class for the BeaconServiceProvider provider.

  See https://angular.io/guide/dependency-injection for more info on providers
  and Angular DI.
*/
@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;
    console.log("QUI");
    this.localNotifications.on("click", function(notification, state){

      console.log("QUI2");

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

      let nav = thiz_app.getActiveNav();
      console.log("NAV", nav);

      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 });
                }
              }
          });

      }

    });

  }

Any ideas why not work?
Thanks