cordova-plugin-background-mode: 'cordova.plugins.backgroundMode.onactivate is not a function' in Ionic 3 app

I did a fresh install of this plugin using the Ionic V3 instructions (https://ionicframework.com/docs/v3/native/background-mode/). When I try to run the app in the simulator or device I get TypeError: cordova.plugins.backgroundMode.onactivate is not a function. (In 'cordova.plugins.backgroundMode.onactivate()', 'cordova.plugins.backgroundMode.onactivate' is undefined when I try to put the app in the background. It sees the subscription to the enable correctly. Any suggestions as to what is going wrong? I’ve the necessary import to app.module.ts.

Here is the code that is in the app.component.ts

import { BackgroundMode } from '@ionic-native/background-mode';

....

  constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, backgroundMode: BackgroundMode) {
    platform.ready().then(() => {
      // Okay, so the platform is ready and our plugins are available.
      // Here you can do any higher level native things you might need.
      statusBar.styleDefault();
      splashScreen.hide();
      backgroundMode.enable();
      backgroundMode.on('enable').subscribe(() => {
        console.log('enabled')
      })
      backgroundMode.on('activate').subscribe(() => {
        console.log('activated')
      })
    });
  }

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 3
  • Comments: 27 (2 by maintainers)

Most upvoted comments

Thats the problem: using a forked version is always the solution

Is there any code to call besides backgroundMode.setDefaults({silent:false}) to make sure that the notification is instantiated?

Hi @rastographics

Maybe:

const options = {
      title: 'your title', text: 'your text',
      hidden: true, silent: false
    };
    this.plataform.ready().then(() => {
      cordova.plugins.backgroundMode.setDefaults(options);

@n1705771 How did you solved ‘cordova.plugins.backgroundMode.onactivate is not a function’ ?

I installed

https://github.com/lkonzen-garupa/cordova-plugin-background-mode

and add the code for wake_lock

Is working for android 5, 7 and 10 😃