Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

angular - Using a third-party cordova plugin in Ionic 2 with TypeScript

In my Ionic 2 app (TypeScript), where I use plugins, for example the Camera plugin from ionic-native which works fine. Now I want to use BackgroundMode plugin: https://github.com/katzer/cordova-plugin-background-mode. I read the README, I did the installation as described.

Under Usage it says that the plugin can be used like this:

cordova.plugins.backgroundMode.enable();

In my IDE (Atom), when I type that, it says it can't find cordova.

I googled a lot about cordova plugins and Ionic 2 and in some cases they use navigator.somePlugin.someFunction() (the window.navigator object if I understand correctly) but that also doesn't work for me. I did a console.log in my app and chrome device inspector shows this:

JSON.stringify(window.navigator, null, 2)
{
  "app": {},
  "camera": {
    "DestinationType": {
      "DATA_URL": 0,
      "FILE_URI": 1,
      "NATIVE_URI": 2
    },
    "EncodingType": {
      "JPEG": 0,
      "PNG": 1
    },
    "MediaType": {
      "PICTURE": 0,
      "VIDEO": 1,
      "ALLMEDIA": 2
    },
    "PictureSourceType": {
      "PHOTOLIBRARY": 0,
      "CAMERA": 1,
      "SAVEDPHOTOALBUM": 2
    },
    "PopoverArrowDirection": {
      "ARROW_UP": 1,
      "ARROW_DOWN": 2,
      "ARROW_LEFT": 4,
      "ARROW_RIGHT": 8,
      "ARROW_ANY": 15
    },
    "Direction": {
      "BACK": 0,
      "FRONT": 1
    }
  },
  "splashscreen": {}
}

My question is:

How can I make use of the BackgroundMode plugin in ionic 2 TS? I don't even know how to include it into my project ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Just like AGrandt says here, you can install it with:

ionic plugin add cordova-plugin-background-mode

Then include this line after the imports:

declare var cordova:any;

And use it when the platform is ready:

platform.ready().then(
    () => {
        console.log("MyApp::constructor platform.ready");
        cordova.plugins.backgroundMode.setDefaults({ 
            title: 'My App Name', 
            text: 'Active in background...');
        cordova.plugins.backgroundMode.enable();
    }
);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...