0

I have a problem that if a user opens the app and then press the home button or switching to another apps, and when the push notification arrives, and the user clicked on that notification, my app doesn't open. I am using this plugin in ionic -> https://ionicframework.com/docs/native/push/

It seems that when app is in background, app doesn't open when he clicked the push notification. How can I resolve that problem using that plugin.

here's my app.component.ts

import { Component, enableProdMode } from '@angular/core';
import { Platform, NavController, App } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { Keyboard } from '@ionic-native/keyboard';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { Push, PushObject, PushOptions } from '@ionic-native/push';
import { GlobalProvider } from "./global.provider";
import { NotificationDetailsPage } from '../pages/notification-details/notification-details';

enableProdMode();

@Component({
  templateUrl: 'app.html'
})
export class MyApp {
  rootPage:any = HomePage;
  private navCtrl: any;

  constructor(
    private platform: Platform,
    private app:App,
    private statusBar: StatusBar,
    private splashScreen: SplashScreen,
    private keyboard: Keyboard,
    private globalVar: GlobalProvider,
    private push: Push
  ) {
    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.
      this.navCtrl = this.app.getActiveNav();
      this.statusBar.overlaysWebView(false);
      this.statusBar.backgroundColorByHexString('#000000');
      setTimeout(() => {
        this.splashScreen.hide();
      }, 500);
      this.keyboard.disableScroll(true);
      this.initPushNotification();
    });
  }
  initPushNotification() {
    const options: PushOptions = {
       android: {
          icon: 'small-icon',
          forceShow: true
       },
       ios: {
          alert: 'true',
          badge: true,
          sound: 'true'
       },
       windows: {}
    };
    const pushObject: PushObject = this.push.init(options);
    pushObject.on('notification').subscribe((notification: any) => {
      //Notification Display Section
      this.navCtrl.push(NotificationDetailsPage, {notifyId:notification.additionalData.id});
    });
    pushObject.on('registration').subscribe((registration: any) => {
      //Register Device ID
      let platformName;
      if (this.platform.is('android')) {
        platformName = 'android';
      } else {
        platformName = 'ios';
      }
      this.globalVar.saveDeviceId(registration.registrationId, platformName).subscribe( data => {});
    });
    pushObject.on('error').subscribe(error => {
      console.log('Can\'t send push notification');
    });
  }
}

How can I handle or trigger this.navCtrl.push when the user clicks on push notification when app is in background? Please help me to resolve this problem. It is almost 3 days and I can't find any solution.

Codeblooded Saiyan
  • 1,457
  • 4
  • 28
  • 54

1 Answers1

0

I changed the PushHandlerActivity.kt to it opens the app when click on notification. I added the forceMainActivityReload(false) to force start the app afetr message "Don't Want Main Activity".

Plugin version:

@havesource/cordova-plugin-push@3.0.0

File:

./platforms/android/app/src/main/java/com/adobe/phonegap/push/PushHandlerActivity.kt

./plugins/@havesource/cordova-plugin-push/src/android/com/adobe/phonegap/push/PushHandlerActivity.kt

./node_modules/@havesource/cordova-plugin-push/src/android/com/adobe/phonegap/push/PushHandlerActivity.kt

From:

if (!dismissed) {
    Log.d(TAG, "Is Push Plugin Active: ${PushPlugin.isActive}")

    if (!PushPlugin.isActive && foreground && inline) {
      Log.d(TAG, "Force Main Activity Reload: Start on Background = False")
      forceMainActivityReload(false)
    } else if (startOnBackground) {
      Log.d(TAG, "Force Main Activity Reload: Start on Background = True")
      forceMainActivityReload(true)
    } else {
      Log.d(TAG, "Don't Want Main Activity")      
    }
  }

To:

  if (!dismissed) {
    Log.d(TAG, "Is Push Plugin Active: ${PushPlugin.isActive}")

    if (!PushPlugin.isActive && foreground && inline) {
      Log.d(TAG, "Force Main Activity Reload: Start on Background = False")
      forceMainActivityReload(false)
    } else if (startOnBackground) {
      Log.d(TAG, "Force Main Activity Reload: Start on Background = True")
      forceMainActivityReload(true)
    } else {
      Log.d(TAG, "Don't Want Main Activity (force start)")
      forceMainActivityReload(false)
    }
  }

So the app finally opens when it's closed.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
lynx_74
  • 1,633
  • 18
  • 12