Ankur
Ankur

Reputation: 3209

ionic serve - Runtime Error : Push plugin not found (IONIC 2)

I am using push notification in ionic 2 app with below code.

import { Push, PushToken } from '@ionic/cloud-angular';
   @Component({...})
   export MyPage {     
    constructor(public platform: Platform, public menu: MenuController, public push: Push){
     this.initializeApp();
    }
    initializeApp() {
      this.platform.ready().then(() => {
       if (this.push) {
        this.push.register().then((t: PushToken) => {
          return this.push.saveToken(t);
        }).then((t: PushToken) => {
          console.log('Token saved:', t.token);
          window.localStorage.setItem("deviceToken", t.token);
        });

        this.push.rx.notification()
          .subscribe((msg) => {
            alert(msg.title + ': ' + msg.text);
            console.log('notification msg', msg);
          });
        }
      }
    }
}

when i run on device it works fine. but i do ionic serve it gives below error because of injection of Push in constructor

error_handler.js:53 TypeError: platform.toLowerCase is not a function
    at Insights.normalizeDevicePlatform (http://localhost:8100/build/main.js:70460:25)
    at Insights.markActive (http://localhost:8100/build/main.js:70450:33)
    at Insights.checkActivity (http://localhost:8100/build/main.js:70439:22)
    at http://localhost:8100/build/main.js:70415:27
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9723)
    at Object.onInvokeTask (http://localhost:8100/build/main.js:41825:37)
    at t.invokeTask (http://localhost:8100/build/polyfills.js:3:9659)
    at e.runTask (http://localhost:8100/build/polyfills.js:3:7083)
    at invoke (http://localhost:8100/build/polyfills.js:3:10836)
    at e.args.(anonymous function) (http://localhost:8100/build/polyfills.js:2:30123)
ErrorHandler.handleError @ error_handler.js:53

Any help is appriciated

Upvotes: 0

Views: 1163

Answers (2)

Tau Martin
Tau Martin

Reputation: 581

Check if you have the line "@ionic/cloud-angular":"^ 0.9.0" in the package.json file, inside dependencies.

If so, switch it to ^0.9.1, save the file and run npm install in the project root folder. This should update @ionic/cloud-angular to 0.9.1 and its @ionic/cloud dependency to 0.15.1.

Then run ionic serve and it should work.

Upvotes: 2

misha130
misha130

Reputation: 5746

You should check if cordova object exists before executing cordova operations.

It doesn't exist on serve since the browser is not a mobile device

initializeApp() {
      this.platform.ready().then(() => {
       if(!(<any>window).cordova) return;
       ...the native code you want to execute...
      });
    }

Upvotes: 1

Related Questions