Ionic cordovaPushV5 initialize error: Can't find variable: PushNotification

Ionic cordovaPushV5 initialize error: Can't find variable: PushNotification

我快气死了。 尝试实现最后一个 cordova 推送通知存储库 (cordovaPushV5),但是当它查看控制台时显示 ReferenceError: Can't find variable: PushNotification。 但奇怪的是,如果我从控制台调用 PushNotification,它就会存在。

这是一些代码

$(document).ready(function() {


    // notification

    var options = {
      android: {
        senderID: "THE_NUMBER"
      },
      ios: {
        alert: "true",
        badge: "true",
        sound: "true"
      },
      windows: {}
    };

    // initialize
    $cordovaPushV5.initialize(options).then(function() {
      // start listening for new notifications
      $cordovaPushV5.onNotification();
      // start listening for errors
      $cordovaPushV5.onError();

      // register to get registrationId
      $cordovaPushV5.register().then(function(registrationId) {
        console.log('registrationId: ',registrationId);
      })
    });

    // triggered every time notification received
    $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data){
      console.log(data);
    });

    // triggered every time error occurs
    $rootScope.$on('$cordovaPushV5:errorOcurred', function(event, e){
      console.log('event: ', event);
      console.log('error: ', e);
    });


  });

下面是一些系统信息

$ ionic info

Your system information:

Cordova CLI: 6.4.0 
Ionic CLI Version: 2.1.14
Ionic App Lib Version: 2.1.7
ios-deploy version: 1.9.0 
ios-sim version: 5.0.8 
OS: macOS Sierra
Node Version: v5.9.1
Xcode version: Xcode 8.1 Build version 8B62



$ ionic platform ls
WARN: ionic.project has been renamed to ionic.config.json, please rename it.
Installed platforms:
  android 6.0.0
  ios 4.3.1
  wp8 (deprecated)
Available platforms: 
  amazon-fireos ~3.6.3 (deprecated)
  blackberry10 ~3.8.0
  browser ~4.1.0
  firefoxos ~3.6.3
  osx ~4.0.1
  webos ~3.7.0


$ ionic plugin ls
WARN: ionic.project has been renamed to ionic.config.json, please rename it.
cordova-plugin-console 1.0.4 "Console"
cordova-plugin-device 1.1.3 "Device"
cordova-plugin-nativeaudio 3.0.6 "Cordova Native Audio"
cordova-plugin-splashscreen 2.1.0 "Splashscreen"
cordova-plugin-statusbar 1.0.1 "StatusBar"
cordova-plugin-whitelist 1.2.0 "Whitelist"
cordova-plugin-x-socialsharing 5.1.3 "SocialSharing"
ionic-plugin-keyboard 2.2.1 "Keyboard"
phonegap-plugin-push 1.9.1 "PushPlugin"

提前致谢


编辑

我已经在插件初始化之前设置了 10 秒的超时并且它可以工作,但不应该这样工作,有什么想法吗?

您是否尝试过将初始化调用包装在 ionic.Platform.ready 函数中?您正在准备 jQuery 文档,但您需要确保移动设备插件也已准备就绪。

示例:

ionic.Platform.ready(function()
{
    // do stuff
});

此答案可能对其他类似问题有用。


我遇到了完全相同的问题。固定它。

我的问题是:我使用的是旧的 Angular 版本,即。 1.4.5 所以它生成了错误的旧版本依赖项。它基本上是在我将 GCM 升级到 FCM 并将插件升级到 'phonegap-plugin-push:^2.2.0'.

时发生的

解决方案是: 为了解决这个问题,一旦我使用 运行 'ionic cordova build android' 命令,然后我明确地手动更改了其他依赖项(如下所示)和重新构建它,一切正常。

即。 (我的应用程序)-build.gradle

    buildscript {
    repositories {
        jcenter()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:2.2.1'
        classpath 'com.google.gms:google-services:4.1.0'
    }
}

// apply plugin: 'com.google.gms.google-services'
// class must be used instead of id(string) to be able to apply plugin from non-root gradle file
ext.postBuildExtras = {
    apply plugin: com.google.gms.googleservices.GoogleServicesPlugin
}

和"project.properties"文件

target=android-27
android.library.reference.1=CordovaLib
android.library.reference.2=app
cordova.system.library.1=com.google.android.gms:play-services-analytics:16.0.8
cordova.gradle.include.1=cordova-support-google-services/myApp-build.gradle
cordova.gradle.include.2=phonegap-plugin-multidex/myApp-multidex.gradle
cordova.system.library.2=com.android.support:support-v13:27.+
cordova.system.library.3=me.leolin:ShortcutBadger:1.1.17@aar
cordova.system.library.4=com.google.firebase:firebase-messaging:17.0.0