ionic-1和Phonegap-plugin-push我得到“PushNotification未定义”

时间:2017-09-05 12:12:11

标签: cordova firebase ionic-framework phonegap-plugins firebase-cloud-messaging

我已经疯了, 我试图像这样使用phonegap-plugin-push

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

                // initialize
                $cordovaPushV5.initialize(options).then(function (data) {
                    alert(data);
                    console.log(data);
                    // start listening for new notifications
                    $cordovaPushV5.onNotification();
                    // start listening for errors
                    $cordovaPushV5.onError();

                    // register to get registrationId
                    $cordovaPushV5.register().then(function (registrationId) {
                        alert(registrationId);
                        console.log(registrationId);
                        $localStorage.token = registrationId;
                    })
                });

我收到错误:

Uncaught ReferenceError: PushNotification is not defined
    at Object.initialize (ng-cordova.js:6378)
    at app.js:69

使用“远程设备”我甚至看不到phonegap-plugin-push enter image description here

我尝试更改并使用cordova-plugin-firebase,但我不会使用令牌, 我尝试使用window.FirebasePlugin.onTokenRefreshwindow.FirebasePlugin.getToken 我真的尝试了一切,但没有什么对我有用。

我的插件:

cordova-inappbrowser 1.0.6 "InAppBrowser"
cordova-plugin-camera 2.4.1 "Camera"
cordova-plugin-compat 1.1.0 "Compat"
cordova-plugin-console 1.0.5 "Console"
cordova-plugin-device 1.1.4 "Device"
cordova-plugin-file 4.3.3 "File"
cordova-plugin-file-transfer 1.6.3 "File Transfer"
cordova-plugin-firebase 0.1.24 "Google Firebase Plugin"
cordova-plugin-geolocation 2.4.3 "Geolocation"
cordova-plugin-splashscreen 4.0.3 "Splashscreen"
cordova-plugin-statusbar 2.2.1 "StatusBar"
cordova-plugin-whitelist 1.3.2 "Whitelist"
ionic-plugin-keyboard 2.2.1 "Keyboard"
phonegap-plugin-push 2.0.0 "PushPlugin"

离子信息:

cli packages: (/usr/local/lib/node_modules)

    @ionic/cli-utils  : 1.9.2
    ionic (Ionic CLI) : 3.9.2

global packages:

    Cordova CLI : 7.0.1 
    Gulp CLI    : CLI version 3.9.1 Local version 3.9.1

local packages:

    Cordova Platforms : android 6.2.3 ios 4.4.0
    Ionic Framework   : ionic1 1.3.3

System:

    ios-sim : 6.0.0 
    Node    : v6.10.2
    npm     : 5.3.0 
    OS      : macOS Sierra
    Xcode   : Xcode 8.3.3 Build version 8E3004b 

我使用ionic package build android构建应用 有人为我解决了吗?请!

1 个答案:

答案 0 :(得分:2)

请在真实的移动设备中进行测试。插件不会在浏览器中加载。

尝试在platform.ready事件之后调用initialize方法。

angular.module('MainCtrl', ['ionic'])
.controller('PushCtrl', function($scope,$cordovaPushV5) {

  ionic.Platform.ready(function(){
    // will execute when device is ready, or immediately if the device is already ready.
    console.log('Platform ready!');

    // 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) {
      // save `registrationId` somewhere;
         console.log(registrationId);
     })
   });
  });

  if(ionic.Platform.device()){
      console.log("Push plugin loaded");
  }else{
   console.log("App is running in browser, push plugin will not load");
  }    
});
相关问题