Ionic cordovaPushV5初始化错误:无法找到变量:PushNotification

时间:2016-12-11 16:21:43

标签: cordova push-notification ngcordova

我生气了。 尝试实现最后一个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秒并且它可以工作,但不应该这样工作,有什么想法吗?

2 个答案:

答案 0 :(得分:0)

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

示例:

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

答案 1 :(得分:0)

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


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

我的问题是:我使用的是较旧的Angular版本,即。 1.4.5,因此它生成了错误的较旧版本的依赖关系。基本上是在我将GCM升级到FCM并将插件升级到“ phonegap-plugin-push:^ 2.2.0”时发生的。

解决方案是:为了解决该问题,一旦我运行了“ ionic cordova build android”命令,我便手动地明确更改了其他依赖项(如下所示)并重新构建它,一切正常很好。

即。 (my-app)-build.gradle

App.scss

和“ project.properties”文件

    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
}