You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ionic真机运行出现plugin_Not_Installed异常,请求技术支援

Ionic真机运行出现plugin_not_installed异常解决方法

在真机运行Ionic应用时,触发plugin_not_installed异常,涉及OneSignal插件的使用。以下是环境信息、代码详情及解决方案:

环境信息

Ionic

Ionic CLI                     : 5.4.16
Ionic Framework               : @ionic/angular 6.2.2
@angular-devkit/build-angular : 14.1.2
@angular-devkit/schematics    : 14.1.2
@angular/cli                  : 14.1.2
@ionic/angular-toolkit        : 6.1.0

Cordova

Cordova CLI       : 11.0.0
Cordova Platforms : android 10.1.2
Cordova Plugins   : cordova-plugin-ionic-keyboard 2.2.0, cordova-plugin-ionic-webview 5.0.0 (and 5 other plugins)

工具与系统

cordova-res : 0.15.4
native-run  : not installed
Android SDK Tools : 26.1.1
NodeJS            : v18.7.0
npm               : 8.6.0
OS                : Windows 10

package.json 内容

{
  "name": "testapp",
  "version": "0.0.1",
  "author": "Ionic Framework",
  "homepage": "https://ionicframework.com/",
  "scripts": {
    "ng": "ng",
    "start": "ng serve",
    "build": "ng build",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "ng e2e"
  },
  "private": true,
  "dependencies": {
    "@angular/common": "^14.0.0",
    "@angular/core": "^14.1.2",
    "@angular/forms": "^14.0.0",
    "@angular/platform-browser": "^14.0.0",
    "@angular/platform-browser-dynamic": "^14.0.0",
    "@angular/router": "^14.0.0",
    "@awesome-cordova-plugins/onesignal": "^5.44.0",
    "@ionic-native/onesignal": "^5.36.0",
    "@ionic-native/pdf-generator": "^5.36.0",
    "@ionic-native/splash-screen": "^5.36.0",
    "@ionic-native/status-bar": "^5.36.0",
    "@ionic/angular": "^6.1.9",
    "@ionic/cordova-builders": "^7.0.0",
    "@nguniversal/express-engine": "^14.0.3",
    "onesignal-cordova-plugin": "^2.11.4",
    "rxjs": "~6.6.0",
    "tslib": "^2.2.0",
    "zone.js": "~0.11.4"
  },
  "devDependencies": {
    "@angular-devkit/build-angular": "^14.1.2",
    "@angular-eslint/builder": "~13.0.1",
    "@angular-eslint/eslint-plugin": "~13.0.1",
    "@angular-eslint/eslint-plugin-template": "~13.0.1",
    "@angular-eslint/template-parser": "~13.0.1",
    "@angular/cli": "^14.0.0",
    "@angular/compiler": "^14.0.0",
    "@angular/compiler-cli": "^14.0.0",
    "@angular/language-service": "^14.0.0",
    "@ionic/angular-toolkit": "^6.0.0",
    "@types/jasmine": "~3.6.0",
    "@types/jasminewd2": "~2.0.3",
    "@types/node": "^12.11.1",
    "@typescript-eslint/eslint-plugin": "5.3.0",
    "@typescript-eslint/parser": "5.3.0",
    "cordova-android": "^10.1.2",
    "cordova-pdf-generator": "^2.1.1",
    "cordova-plugin-device": "2.0.2",
    "cordova-plugin-ionic-keyboard": "^2.0.5",
    "cordova-plugin-ionic-webview": "^5.0.0",
    "cordova-plugin-splashscreen": "^5.0.2",
    "cordova-plugin-statusbar": "^2.4.3",
    "eslint": "^7.6.0",
    "eslint-plugin-import": "2.22.1",
    "eslint-plugin-jsdoc": "30.7.6",
    "eslint-plugin-prefer-arrow": "1.2.2",
    "jasmine-core": "~3.8.0",
    "jasmine-spec-reporter": "~5.0.0",
    "karma": "~6.3.2",
    "karma-chrome-launcher": "~3.1.0",
    "karma-coverage": "~2.0.3",
    "karma-coverage-istanbul-reporter": "~3.0.2",
    "karma-jasmine": "~4.0.0",
    "karma-jasmine-html-reporter": "^1.5.0",
    "protractor": "~7.0.0",
    "ts-node": "~8.3.0",
    "typescript": "~4.7.3"
  },
  "description": "An Ionic project",
  "cordova": {
    "plugins": {
      "cordova-pdf-generator": {},
      "cordova-plugin-statusbar": {},
      "cordova-plugin-device": {},
      "cordova-plugin-splashscreen": {},
      "cordova-plugin-ionic-webview": {},
      "cordova-plugin-ionic-keyboard": {},
      "onesignal-cordova-plugin": {}
    },
    "platforms": [
      "android"
    ]
  }
}

app.component.ts 代码

import { Component } from '@angular/core';
import { OneSignal } from '@ionic-native/onesignal/ngx';
import { Platform, AlertController } from '@ionic/angular';
import { SplashScreen } from '@ionic-native/splash-screen/ngx';
import { StatusBar } from '@ionic-native/status-bar/ngx';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.scss'],
})
export class AppComponent {
  constructor(private platform: Platform, private splashScreen: SplashScreen, private statusBar: StatusBar, private oneSignal: OneSignal, private alertCtrl: AlertController) {
    this.initializeApp();
  }

  initializeApp() {
    this.platform.ready().then(() => {
      this.statusBar.styleDefault();
      this.splashScreen.hide();
      setTimeout(() => { this.splashScreen.hide(); }, 1000);
      if (this.platform.is('cordova')) {
        this.setupPush();
      }
    });
  }

  setupPush() {
    this.oneSignal.startInit("APPID", "GOOGLEPROJECTID");

    this.oneSignal.inFocusDisplaying(this.oneSignal.OSInFocusDisplayOption.None);
 
    // Notifcation was received in general
    this.oneSignal.handleNotificationReceived().subscribe(data => {
      let msg = data.payload.body;
      let title = data.payload.title;
      let additionalData = data.payload.additionalData;
      this.showAlert(title, msg, additionalData.task);
    });
 
    // Notification was really clicked/opened
    this.oneSignal.handleNotificationOpened().subscribe(data => {
      // Just a note that the data is a different place here!
      let additionalData = data.notification.payload.additionalData;
 
      this.showAlert('Notification opened', 'You already read this before', additionalData.task);
    });
 
    this.oneSignal.endInit();

    this.oneSignal.getIds().then(identity => {
      alert('inside');
      this.showAlert('pushToken : ', identity.pushToken, 'Ok');
      alert(identity.pushToken + ' it is PUSHTOKEN'); 
      alert(identity.userId + 'it is USERID');
    }).catch((e) => {
      alert('in exception!')
      alert(e);
    });
  }

  async showAlert(title, msg, task) {
    const alert = await this.alertCtrl.create({
      header: title,
      subHeader: msg,
      buttons: [
        {
          text: `Action: ${task}`,
          handler: () => {`enter code here`
            
          }
        }
      ]
    })
    alert.present();
  }
}

解决方案

1. 清理重复的OneSignal依赖

package.json中同时存在@awesome-cordova-plugins/onesignal和@ionic-native/onesignal,二者为同一插件的不同命名空间版本(@ionic-native已迁移至@awesome-cordova-plugins),会引发冲突:

  • 卸载旧依赖:
    npm uninstall @ionic-native/onesignal
    
  • 更新导入语句:将app.component.ts中的导入改为
    import { OneSignal } from '@awesome-cordova-plugins/onesignal/ngx';
    

2. 重新安装Cordova插件并同步平台

插件可能未正确安装到Android平台:

  • 移除Android平台:
    ionic cordova platform remove android
    
  • 重新添加平台:
    ionic cordova platform add android@10.1.2
    
  • 重新安装所有插件:
    ionic cordova plugin add onesignal-cordova-plugin@2.11.4 cordova-pdf-generator@2.1.1 cordova-plugin-device@2.0.2 cordova-plugin-statusbar@2.4.3 cordova-plugin-splashscreen@5.0.2 cordova-plugin-ionic-webview@5.0.0 cordova-plugin-ionic-keyboard@2.0.5
    

3. 加固插件初始化检查

确保OneSignal调用仅在Cordova环境就绪后执行,添加前置检查:

setupPush() {
  if (!window.cordova || !window.plugins || !window.plugins.OneSignal) {
    console.error('OneSignal plugin not available');
    return;
  }
  // 原初始化代码...
}

4. 清理构建缓存

  • 清理Ionic缓存:
    ionic cache clear
    
  • 删除项目根目录下的platforms/android/app/build文件夹,重新构建:
    ionic cordova build android --prod
    

5. 安装native-run工具

环境中native-run未安装,可能影响真机部署稳定性:

npm install -g native-run

内容的提问来源于stack exchange,提问作者Srinivasan M

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 13:24:29