【问题标题】:$ionicPlatform.ready() not firing, Android$ionicPlatform.ready() 没有触发,Android
【发布时间】:2016-09-03 00:34:37
【问题描述】:

我正在尝试让推送通知在完全基于单个 WebView 的 Ionic Android 应用上运行,通过 cordova-plugin-inappbrowser 提供服务。

$ionicPlatform.ready() 函数永远不会调用,因此永远不会创建 Ionic.Push 对象(没有记录设备令牌)。我可以在调用 $ionicPlatform.ready() 之前在 run 函数内部运行 console.log,但不能在来自 $ionicPlatform.ready() 的回调内部运行。

我是否遗漏了一些明显的东西?

app.js

var websiteURL = 'http://example.com';

angular.module('abc', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {
    if(window.cordova && window.cordova.plugins.Keyboard) {
      // Hide the accessory bar by default (remove this to show the accessory bar above the keyboard
      // for form inputs)
      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      //stops the viewport from snapping when text inputs are focused. Ionic handles this internally for
      // a much nicer keyboard experience.
      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
    // bootstrap for push notifications, set debug to true to use Ionic's push notification platform, not Google's/Apple's
    var push = new Ionic.Push({
      "debug": true
    });

    // register the apps token so that the ionic platform can communicate with it
    push.register(function(token) {
      console.log("Device token:",token.token);
    });
  });
})

.controller("AppController", function($scope) {
  $scope.openWebsite = function()
  {
   // open up the peoplemovers site in the in app browser window
   window.open(websiteURL,'_self'); 
  };
})

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- IF using Sass (run gulp sass first), then uncomment below and remove the CSS includes above
    <link href="css/ionic.app.css" rel="stylesheet">
    -->

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>
<script src="lib/ionic-platform-web-client/dist/ionic.io.bundle.min.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <!-- Cordova is bootstrapped by ionic-platform-web-client, uncomment this if you remove ionic-platform-web-client... -->
<!-- <script src="cordova.js"></script> -->

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="abc">

    <ion-pane>
      <ion-content ng-controller="AppController" ng-init="openWebsite()" padding="true">
      </ion-content>
    </ion-pane>
  </body>
</html>

编辑

我已经重新安装了 Ionic、Cordova、node、npm、Android Studio 和 Android SDK。

window.open 调用的 URL 有一个文件选择按钮,当从 Android 的“图库”选项卡中选择文件时,该按钮会导致应用程序崩溃,这是由于某些 Android 权限问题造成的。论坛帖子建议添加cordova-plugin-camera 插件以添加必要的权限,这确实解决了问题。

但是,现在没有拨打$ionicPlatform.ready 电话。即使我运行ionic plugin remove cordova-plugin-camera,我似乎也无法再让$ionicPlatform.ready 打电话了。

我没有收到任何错误消息,应用程序加载正常。这种行为在 LG Android 6.0 手机、Android 模拟器 (SDK 23) 和 Genymotion 模拟器上的测试中是一致的。

除了相机插件,还有更简单的方法从手机获取文件权限吗?如果没有,是否有我需要让$ionicPlatform.ready 函数触发的设置或配置行?

这是新的 app.js:

var websiteURL = 'http://example.com';

angular.module('lmgapp', ['ionic'])

.run(function($ionicPlatform) {
  $ionicPlatform.ready(function() {

    console.log('firing');

    if(window.cordova && window.cordova.plugins.Keyboard) {

      cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);

      cordova.plugins.Keyboard.disableScroll(true);
    }
    if(window.StatusBar) {
      StatusBar.styleDefault();
    }
  });
})


.controller("AppController", function($scope) {
  $scope.openWebsite = function()
  {
    console.log('in the controller');
   // open up the site in the in app browser window
   window.open(websiteURL,'_self'); 
  };
});

这是重新安装后的新 index.html:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">
    <title></title>

    <link href="lib/ionic/css/ionic.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">

    <!-- ionic/angularjs js -->
    <script src="lib/ionic/js/ionic.bundle.js"></script>

    <!-- cordova script (this will be a 404 during development) -->
    <script src="cordova.js"></script>

    <!-- your app's js -->
    <script src="js/app.js"></script>
  </head>
  <body ng-app="lmgapp">

    <ion-pane>
      <ion-content ng-controller="AppController" ng-init="openWebsite()">
      </ion-content>
    </ion-pane>
  </body>
</html>

【问题讨论】:

    标签: javascript android angularjs cordova ionic-framework


    【解决方案1】:

    $ionicPlatform.ready 没有触发,因为您没有cordova。它被注释掉了。

    【讨论】:

    • + ionic-platform-web-client 已弃用。它被 Ionic Cloud 客户端取代。请参阅迁移:docs.ionic.io/migration.html
    • ionicPlatform.ready 无论你有没有cordova,都应该被调用。您是否收到任何错误消息?
    • @Ladmerc 我没有收到任何错误消息 - 应用程序仍将运行。
    • @e666 我一定使用的是旧版本的 Ionic。 cordova.js 在应用程序的创建中被注释掉了。一旦我用新版本重新安装了所有必要的软件,就会默认添加cordova.js。我用这些更改更新了我的问题。
    • @Alex,我更新到了新版本的 Cordova,cordova.js 现在被添加为默认值。不幸的是,我仍然有一些类似的行为。我发现了一些关于这种情况的新信息,并将其添加为我的问题的编辑。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-19
    • 1970-01-01
    相关资源
    最近更新 更多