PushNotification 未定义 (ng-cordova)

PushNotification is undefined (ng-cordova)

在我的离子应用程序中,当 运行在浏览器中打开应用程序时,出现以下错误

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

在我的控制台中,以及当我在我的 phone 中为 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
      });  

  });

})

如果您查看 ngCordova 页面,它是 "Cordova plugins do not work while developing in your browser"。这就是它在浏览器中不起作用的原因。

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

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

link: 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