【问题标题】:Ionic - push notifications - 'pushNotification' of undefined离子 - 推送通知 - 未定义的“pushNotification”
【发布时间】:2016-05-27 07:54:49
【问题描述】:

我想创建带有通知的 Ionic 应用程序。 我安装了 PushPlugin,这是我的控制器:

.controller('MainCtrl', function($scope,$ionicPlatform,$cordovaPush) {
  $ionicPlatform.ready(function () {
    var androidConfig = {
      "senderID": "ID"
    };

      $cordovaPush.register(androidConfig).then(function(result) {
        // Success
        console.log(result);
      }, function(err) {
        // Error
      });
  })
});

通过 ionic serve 运行后出现错误:

ng-cordova.js:6180 未捕获类型错误:无法读取属性 未定义的'pushNotification'

我找到了一些解决方案,所以我修改了我的代码:

index.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="css/ionic.app.css" rel="stylesheet">

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

    <script src="lib/ngCordova/dist/ng-cordova.js"></script>

    <script src="cordova.js"></script>
    <!-- copy from plugin www folder -->
    <script src="js/libs/PushNotification.js"></script>

    <script src="js/app.js"></script>
    <script src="js/controllers.js"></script>
</head>

和我的控制器:

.controller('MainCtrl', function($scope,$ionicPlatform,$cordovaPush) {
  $ionicPlatform.ready(function () {
    var androidConfig = {
      "senderID": "ID"
    };

      if (window.plugins && window.plugins.pushNotification){
          console.log("plugins"); //it displays
          $cordovaPush.register(androidConfig).then(function(result) {
            // Success
            console.log(result);
          }, function(err) {
            // Error
          });
        }
  })
});

现在,我有:

未捕获的引用错误:cordova 未定义

我很困惑,如何运行它。

插件列表:

com.phonegap.plugins.PushPlugin 2.5.0 "PushPlugin"
cordova-plugin-console 1.0.3 "Console"
cordova-plugin-device 1.1.2 "Device"
cordova-plugin-splashscreen 3.2.2 "Splashscreen"
cordova-plugin-statusbar 2.1.3 "StatusBar"
cordova-plugin-whitelist 1.2.2 "Whitelist"
ionic-plugin-keyboard 2.2.0 "Keyboard"

【问题讨论】:

标签: cordova ionic-framework push-notification


【解决方案1】:

确保以下几点:

1.. 通过运行以下命令行添加 ionic-platform-web-client 和 push-plugin:

ionic add ionic-platform-web-client
ionic plugin add phonegap-plugin-push --variable SENDER_ID="GCM_PROJECT_NUMBER"

2.. 通过运行以下命令行为您的应用分配一个 ID:

ionic io init

3.. 将 debug 设置为 true 以便能够在浏览器中进行测试

ionic config set dev_push true

4.. 在 run() 方法中设置你的推送注册

angular.module('app', ['ionic','ionic.service.core' ])
.run(function($ionicPlatform) {

    $ionicPlatform.ready(function() {
      var push = new Ionic.Push({
        "debug": true
      });

      push.register(function(token) {
        console.log("My Device token:",token.token);        
        push.saveToken(token, {'ignore_user': true}) ;  // persist the token in the Ionic Platform
      });
    }); 

})

测试您的推送,如果它在浏览器中有效,则禁用调试并开始在您的设备上进行测试。

ionic config set dev_push false

完整指南http://docs.ionic.io/docs/push-quick-start

【讨论】:

  • 我阅读了这个文档,但它只是正确的解决方案吗?使用另一个cordova插件怎么样?
  • 我不确定我是否理解你的问题,你能澄清更多吗?
  • 是的,它工作正常,你只需要一步一步来,首先检查什么 console.log("My Device token:",token.token);显示,如果它正在返回一个令牌。
  • 我部署在设备上,我可以接收消息。但我在顶部栏没有通知:/
  • 如果您能够在应用程序仅打开时在设备上接收消息,则您已经完成了一半,下一步是按照完整的设置指南设置 android 或 ios 推送服务和您必须将 dev_push 设置为 false,如上一步所示:docs.ionic.io/docs/push-full-setup
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多