34

I am trying to use the cordova GooglePlus plugin in a new ionic 2 project (latest ionic2 version) but I always run into errors regarding cordova. The plugin is properly installed and shows up in the plugin folder.

One approach I tried is this:

import { GooglePlus } from "ionic-native";

and then

GooglePlus.login().then(...)

The login method executes but always throws an error saying "cordova_not_available"

I want to test the app with ionic serve on my windows system first before deploying it to my android phone. How can I make cordova available in the localhost server? From searching I understand that cordova.js is generated and always included in the deploy package for the device.

Another approach I tried is using

window.plugins.googleplus.login(...)

But this approach does not go through the typescript compiler who does not know anything about a plugins property on the windows object.

How can I fix this?

hholtij
  • 2,906
  • 6
  • 27
  • 42

5 Answers5

68

If you want the plugin to work for the browser you should add platform browser and run it:

ionic cordova platform add browser

and run it:

ionic cordova run browser

instead of ionic serve.

Ajay Gupta
  • 2,867
  • 23
  • 28
  • @JasonKrs make sure browser platform added, I mean to say check it out there is a browser folder create under platform folder??? or try to remove and add it again. – Ajay Gupta Apr 27 '18 at 11:11
  • Yeah it was not added... I commented the answer before trying what you said...my bad... – Jason Krs Apr 27 '18 at 12:44
  • 1
    if someone is facing issues, after following this, please do make sure you hit 'npm install' after first command here. and then run browser. – Deepak Yadav Aug 08 '18 at 09:24
  • @AjayGupta - Is it possible to use --livereload using browser? – Mohan Nov 23 '18 at 08:57
15

This error usually occurs when you're running the app in chrome using ionic serve which is normal as in the browser cordova native components are not there but also occur on emulator and devices when an ionic native plugin that you're using was nod added, even if you have added the ionic plugin for it.

For instance if you are using native Toast

then you need to add proper ionic dependencies:

ionic plugin add cordova-plugin-x-toast --save

but you also need to add cordova dependencies:

cordova plugin add cordova-plugin-x-toast --save

If you forget to add the later cordova plugin you'll get an error like:

Runtime Error Uncaught(in promise): cordova_not_available

Which can be tricky to find the cause.

Once you have added ionic and cordova dependencies you should be able to use it.

Make sure you import it:

import { Toast } from 'ionic-native';

inject Platform in constructor:

constructor(public navCtrl: NavController, private platform: Platform) {...

then use the native item:

this.platform.ready().then(() =>
      Toast.show("Successfull", '5000', 'center')
        .subscribe(
        toast => {
          console.log(toast);
        }
      ));
groo
  • 4,213
  • 6
  • 45
  • 69
  • I noticed that the error: Runtime Error Uncaught(in promise): cordova_not_available still shows some times while running the android emulator on linux .. not sure yet what causes it as it's not consistent, sometimes it happens others it works. – groo Dec 01 '16 at 19:36
  • Still getting same error even after all this installation, and I noticed that the CLI observed that the plugin has been installed already on running cordova plugin add cordova-plugin-x-toast --save – kolexinfos Dec 11 '16 at 10:52
  • @kolexinfos - it only works when running with ionic run android without the -lc option, when using ionic serve or ionic run android -lc yes the error still there as the cordova plugins are not available in browser. What I do with android is to sue ionic run android and then to be able to see logs/console I tail logcat logs with: adb logcat |grep -i "console" so I can still follow the logs. – groo Dec 12 '16 at 07:58
  • I know Cordova is not available in browser what I am saying is even running on device it throws Cordova_not_unavailable I can see this using Google chrome's remote device console and the toast does not show either – kolexinfos Dec 12 '16 at 08:03
  • @kolexinfos - when you mention that you're using Google chrome's remote device console then it will not work.. you're probably using livereload and in this case the cordova plugins are not available. Please try running only on device emulator and DON'T connect the chrome remote device console.. To see the logs use adb logcat (if android): adb logcat |grep -i "console" – groo Dec 12 '16 at 09:10
7

Using ionic serve disables all the cordova plugins, because it is not running on a device.

Rather use ionic cordova run android This will start an android emulator that should allow all the cordova plugins to function

I have also come across the second approach, but the syntax then has to be windows['plugins'].googleplus.login(...)

Luuk Wuijster
  • 6,678
  • 8
  • 30
  • 58
DeepFreez
  • 91
  • 4
2

Sometimes using ionic cordova run browser is not the best option, since it takes a long time for it to compile your changes.

In my case, what was causing the issue was the FCM plugin. It cannot run on the browser, if I use ionic serve. Since my code was inside app.component.ts, it was easy for me to get around the problem. I simply used the following line:

  if (platform.is('cordova')) 
     {  this.fcmx.onTokenRefresh().subscribe(token => { 
     this.pushNoti.storeNewToken(token); }); }

Using platform.is('cordova'), you can prevent the code causing trouble to run on the browser.

Roddy P. Carbonell
  • 858
  • 1
  • 11
  • 16
0

Simply Run after attaching android device

ionic cordova run android

make sure you correct google map key