【问题标题】:ionic 2 app not opening on click push notification when app is in background当应用程序处于后台时,离子 2 应用程序未在单击推送通知上打开
【发布时间】:2022-01-12 23:12:57
【问题描述】:

我有一个问题,如果用户打开应用程序,然后按下主页按钮或切换到另一个应用程序,当推送通知到达时,用户点击了该通知,我的应用程序没有打开。我在 ionic 中使用这个插件 -> https://ionicframework.com/docs/native/push/

似乎当应用程序处于后台时,单击推送通知时应用程序没有打开。如何使用该插件解决该问题。

这是我的 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');
    });
  }
}

当应用在后台时,当用户点击推送通知时,我该如何处理或触发this.navCtrl.push?请帮我解决这个问题。快3天了,我找不到任何解决方案。

【问题讨论】:

标签: ionic-framework phonegap-pushplugin


【解决方案1】:

我将 PushHandlerActivity.kt 更改为单击通知时会打开应用程序。我添加了 forceMainActivityReload(false) 以在消息“Don't Want Main Activity”之后强制启动应用程序。

插件版本:

@havesource/cordova-plugin-push@3.0.0

文件:

./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

发件人:

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

收件人:

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

所以应用程序最终在关闭时打开。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    相关资源
    最近更新 更多