Mr.Smith
Mr.Smith

Reputation: 190

ionic-1 and Phonegap-plugin-push I get "PushNotification is not defined"

I'm going crazy already, I tried to work with phonegap-plugin-push like that:

                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;
                    })
                });

and I get Error:

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

with “Remote devices” I do not even see phonegap-plugin-push enter image description here

I tried to change and to work with cordova-plugin-firebase but I don't gat Token, I tried to work with window.FirebasePlugin.onTokenRefresh and with window.FirebasePlugin.getToken I really tried everything but nothing works for me.

my plugins:

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"

ionic info:

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 

I build the app with ionic package build android Anyone have a solution for me? Please!

Upvotes: 1

Views: 888

Answers (1)

Anuj.T
Anuj.T

Reputation: 1628

Please test it in real mobile device.Plugin wont load in browser.

Try calling the initialize method after platform.ready event.

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");
  }    
});

Upvotes: 2

Related Questions