【问题标题】:PushNotification is undefined (ng-cordova)PushNotification 未定义(ng-cordova)
【发布时间】:2016-11-30 17:18:12
【问题描述】:

在我的 ionic 应用程序中,在浏览器中运行应用程序时出现以下错误

ng-cordova.js:6378 Uncaught ReferenceError: PushNotification is not defined 

在我的控制台中,以及当我为 android 构建应用程序并在我的手机中运行时,它在那里也不起作用,即没有弹出窗口 注册 ID:alert(data.registrationId);

app.run(function($ionicPlatform, $cordovaPushV5) {
  $ionicPlatform.ready(function() {

      // For Push Notification

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

      // 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(data) {
          // `data.registrationId` save it somewhere;
          alert(data.registrationId);
        })
      });

      // triggered every time notification received
      $rootScope.$on('$cordovaPushV5:notificationReceived', function(event, data){
        alert(data.title + ' - '+ data.message);
        // data.message,
        // data.title,
        // data.count,
        // data.sound,
        // data.image,
        // data.additionalData
      });

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

  });

})

【问题讨论】:

    标签: ionic-framework cordova-plugins ngcordova


    【解决方案1】:

    如果您查看 ngCordova 页面,会说“在浏览器中开发时,Cordova 插件不起作用”。这就是它无法在浏览器中运行的原因。

    在此处查看链接:http://ngcordova.com/docs/common-issues/

    在初始化插件之前,您应该检查应用程序是否在设备上运行(而不是在浏览器中)并且离子平台已准备好。然后,如果您正确地点击链接,您就可以开始了。

    链接:https://github.com/phonegap/phonegap-plugin-push

    我的个人建议是你不必使用 ng-cordova,因为新的 phonegap-push 插件已经发布。你不需要使用任何其他东西。您基本上可以在不使用 ng-cordova 的情况下执行以下操作。

    确保检查 ionic 平台是否已准备好并且该平台不是浏览器。

    var pushConfig = {
        android: {
            senderID: "blabla"
        },
        ios: {
            alert: true,
            badge: true,
            sound: true
        }
    };
    
    var push = window.PushNotification.init(pushConfig);
    
    push.on('registration', function(data) {
        var token = data.registrationId
        console.log('OK: register notfy ', token);
    });
    

    详细信息在这里:https://github.com/phonegap/phonegap-plugin-push

    【讨论】:

    • 我做的和你说的完全一样,但是当我在安卓手机上运行应用程序时,我没有得到 redistrationId。
    • 您遇到什么错误?你能提供更多细节吗? @gudboisgn
    • 我已经在我的 $rootScope.regId 中分配了 data.registrationId ,当我使用 {{ regId }} 将其打印到视图时。它只是空的。 @orhankutlu
    • 你可以先试试 console.log(data.registrationId) 吗?
    • 在浏览器中我得到:PushNotification 未定义,Android 手机如何查看 console.log() 数据? @orhankutlu
    猜你喜欢
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-13
    • 1970-01-01
    • 2021-06-17
    • 1970-01-01
    相关资源
    最近更新 更多