Anybody tried the Toast yet?

I am trying to use the native Toast, but I always got “toast error: undefined”

Here is the code:

Toast.show('Hello there!', 'long', 'center').subscribe(
  a => {
    console.log('toast success: '+ a)
  }, 
  b => {
    alert('toast error: ' + b)
  }
);

I am using BB10, but I know the “cordova-plugin-x-toast” should work with that OS. Any idea? Do you think it is because the plugin is not loaded properly? Do I need to add a “this._platform.ready().then”?

1 Like

Yes, you have to do something like:

    showToast(message, position) {
            this.platform.ready().then(() => {
                window.plugins.toast.show(message, "short", position);
            });
        }

Works fine with blank project…

It gave me the same result… :frowning:

Have you tried with a blank project by first creating it with ionic start ToastTest --v2 --ts ?

@icarus_31 Have you tried using ionic-native?

npm install ionic-native
import {Toast} from 'ionic-native';

class{

   showToast(){ 
    Toast.show('hello world', 'short', 'top');
   }
}

http://ionicframework.com/docs/v2/native/Toast/

Yes, I did use the ionic-native.

Just got an old Android phone. I will try to run on that device (Samsung S4).

By the way, any advice how to setup an Ionic 2 project to run on an Android device and emulator?

I installed the SDK, put my phone on developer mode, I tried ionic emulate android and ionic run android. It built, installed and launched on the emulator, but got this error:

image

I thought the ‘run’ will start the app on the phone, but maybe it is doing something else?

Thanks in advance

[UPDATE]: when sending the question about the issue, I got it work on the phone! The simulator still has the issue, but that’s ok. I prefer to work on a real device! :slight_smile:

@mhartington
Thanks Mike. But it is not solved my problem with incorrect plugin installation.
Maybe there is way to get some additional info from ionic-native to find the cause?
Or maybe I should reinstall the plugin by removing it with cordova plugin remove ... and then reinstall it with ionic plugin add cordova-plugin-x-toast ?
What would you recommend to do in such a case?
I really don’t want to refactoring the whole working project to make toasts work.
And earning some basic “plugin debugging” skills will be very nice.

To run on the Android emulator you use the following command:

ionic emulate android -l -c

-l will give you livereload
-c will give console log to the terminal

I’m also finding Toasts don’t work when using ionic-native, but if I use them directly they work with one issue, on an actual device I get the Cordova icon at the top left corner of the toast and don’t know how to get rid of it.

Thanks @SDP1966.

I just tried on an Android phone and this code below is working!

file.ts

  import {Toast} from 'ionic-native';

  ...

  showToast() {
    Toast.show("This is my toast", 'short', 'top').subscribe(
      toast => {
        console.log('Success', toast);
      },
      error => {
        console.log('Error', error);
      },
      () => {
        console.log('Completed');
      }
    );
2 Likes

Interesting, so the ionic-native version only works with the .subscribe tacked on.

Does anybody know why I’m getting the Cordova icon on my Toasts? It only appears on my Android device, not in the emulator. Better yet, does anybody know how to get rid of it?

image

1 Like

What do you mean by “.subscribe tacked on”?

Also, you are testing on a iphone or android?

I mean using the following results in no Toast shown:

Toast.show('hello', 'short', 'bottom')

But if I then subscribe to it all goes as expected.

And I’m testing on an Android phone at the moment, also in the emulator. The icon only appears on the device.

1 Like

+1 here.
Only works with .subscribe, testing also on device android 6.
@SDP1966 I not getting this cordova icon on my toast.

Finally found out what’s going on, it’s a ‘feature’ of Cyanogenmod ROM. So no longer need to worry about it, typical users will never see it, and those that have Cyanogen installed, nothing we can do about it.

Apparently the idea behind it is because it’s a platform feature any app could be causing the Toast, it’s so the user knows which app is responsible.

nice catch about subscribe to get show up …

thanks

1 Like

.subscribe is still required here, suddenly… my app worked OK for a long time.
I don’t know what causes it!