cordova-plugin-background-mode icon indicating copy to clipboard operation
cordova-plugin-background-mode copied to clipboard

'cordova.plugins.backgroundMode.onactivate is not a function' in Ionic 3 app

Open daviddickson opened this issue 5 years ago • 27 comments

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')
      })
    });
  }

daviddickson avatar Feb 28 '19 21:02 daviddickson

See pull request - https://github.com/katzer/cordova-plugin-background-mode/pull/423

i91 avatar Mar 01 '19 17:03 i91

@i91 I'm seeing this issue on iOS.

daviddickson avatar Mar 05 '19 15:03 daviddickson

Fixed in pull request https://github.com/katzer/cordova-plugin-background-mode/pull/433

daviddickson avatar Mar 05 '19 16:03 daviddickson

Same problem on Android

Genarito avatar Mar 11 '19 15:03 Genarito

I'm having the same problem on Android.

image

lkonzen-garupa avatar Mar 14 '19 20:03 lkonzen-garupa

You can solve this (until contributors merge @daviddickson PR) by running:

ionic cordova plugin add https://github.com/fyayc-chrisat/cordova-plugin-background-mode --no-fetch
ionic cordova platform rm android
ionic cordova platform add android

If you're not using Ionic just remove ionic from the commands. Keep in mind that It's a forked version.

Hope It helps :D

Genarito avatar Mar 14 '19 20:03 Genarito

I just tried @daviddickson's fork now, but I do my initial dev using an Android device, so after I had the same error I took a closer look at that fork and noticed it only includes changes for iOS.

since @fyayc-chrisat's fork covers the Android side, but was forked from an earlier point (6 commits behind the source repo), I simply applied the Android String.format() fix to the relevant file in David's fork, but the same error kept coming up.

looking through the commits in this repo since the latest 0.7.2 tag, it seems the onactivate, ondeactivate and (something I think we all missed) onfailure methods have actually been removed.


@katzer I do see that they were already marked as "deprecated", but could you perhaps shed some light on this?

from my testing, this error doesn't seem to break any functionality in either Android or iOS, but the @ionic-native/background-mode wrapper still expects them to be there, so as it is right now, we can't hook in any custom functionality when the app moves to the background or foreground.

ZaLiTHkA avatar Mar 20 '19 09:03 ZaLiTHkA

actually, I think we might be going down the wrong path here altogether...

PR 423 is trying to print out a string as cordova.plugins.backgroundMode.on({EVENT}, {PARAMS}), whereas the Ionic Native wrapper is expecting the plugin to have methods like on{EVENT}, such as onactivateandondeactivate`.

so now I'm even more confused by which way this whole thing is going. :/

ZaLiTHkA avatar Mar 20 '19 09:03 ZaLiTHkA

Is there any plan to merge @daviddickson pull request? I see there is a fix through his fork, but is this something that's being tested to merge with the master @katzer ?

ewwwgiddings avatar Mar 22 '19 21:03 ewwwgiddings

image Any progress?

fax1ty avatar Apr 07 '19 20:04 fax1ty

@fax1ty I'm testing with beta users now and until now it's working. Added to the disable batery optimizations. You can try this on that fork: https://github.com/lkonzen-garupa/cordova-plugin-background-mode

lkonzen-garupa avatar Apr 08 '19 10:04 lkonzen-garupa

Thanks @lkonzen-garupa . I'm trying your fork right now... I have backgroundMode.setDefaults({silent:false}) set and see that no notification comes up anymore?

This is great if for me because I hated having that notification required to be there. However, before using your fork, the background audio of my app would always cut out after moving to background, unless I made the notification appear with silent:false.

How did you fix this so the notification is not needed anymore?

rastographics avatar Apr 10 '19 12:04 rastographics

@rastographics about audio I don't change anithing. I just use the plugin https://github.com/floatinghotpot/cordova-plugin-nativeaudio.

All is working and playing fine, even when in background.

On device is ready:

window.plugins.NativeAudio.preloadSimple(
    'beep',
    'audio/bell.mp3'
);

When I need to play that audio:

window.plugins.NativeAudio.play('beep');

lkonzen-garupa avatar Apr 10 '19 13:04 lkonzen-garupa

I'm using <audio> tag inside the apps html template to play an internet stream...it works for about 10-12 minutes but then stops. (Must be when Android system puts it in background).

When using katzer's plugin, there is a constant notification created that keeps the app from dying. I can let the app play the internet stream in background for hours and it does not stop. But the problem is the errors mentioned in this thread that come up when building.

With your fork, the errors go away, but so does the constant notification, so the app dies after being in background for 10-12 minutes and the audio stops.

I know your plugin does not change anything with the audio...but something is done different that does not create that notification which keeps the app from being killed after long time. Does that make sense?

rastographics avatar Apr 10 '19 14:04 rastographics

I understand. Maybe that commit is creating this problem: https://github.com/lkonzen-garupa/cordova-plugin-background-mode/commit/b2a9cd94ae61c3474850d22fb6c5a41d15295f96

But, in my case, it still showing the notification for a long time. What your test environment? Maybe something related with it and I not get it until now with my users.

lkonzen-garupa avatar Apr 10 '19 14:04 lkonzen-garupa

@lkonzen-garupa have you solved this issue? I have the same issue...

n1705771 avatar Apr 12 '19 11:04 n1705771

@n1705771 it's all working fine.

lkonzen-garupa avatar Apr 12 '19 12:04 lkonzen-garupa

@lkonzen-garupa My test environment is android 8 running on a moto g6. Something that happens with your fork that is different is when app runs for first time, android prompts me for permission to let app run in background. I thought that is how your fork is able to keep going in background without notification.

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

rastographics avatar Apr 12 '19 13:04 rastographics

@rastographics In m case, the prompt just occur if I execute that following comand:

window.cordova.plugins.backgroundMode.disableBatteryOptimizations();

And I use just when needed to enable the background.

window.cordova.plugins.backgroundMode.on('enable', function() {
   window.cordova.plugins.backgroundMode.disableBatteryOptimizations();
});

lkonzen-garupa avatar Apr 12 '19 13:04 lkonzen-garupa

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 :-)

MInesGomes avatar Apr 16 '19 06:04 MInesGomes

I can't get this to work on device with iOS 12.

I got rid of the error TypeError: cordova.plugins.backgroundMode.onactivate is not a function. (In 'cordova.plugins.backgroundMode.onactivate()' doing what @daviddickson suggested on #433 but the plugin isn't working either.. I mean, it just does nothing when in background.

I also tried what @zhangxiongwang suggested on #450 but I'm still unsuccessful.

Someone experiencing the same issue? Please help! thanks!

imarquezc avatar Jul 02 '19 17:07 imarquezc

I can't get this to work on device with iOS 12.

I got rid of the error TypeError: cordova.plugins.backgroundMode.onactivate is not a function. (In 'cordova.plugins.backgroundMode.onactivate()' doing what @daviddickson suggested on #433 but the plugin isn't working either.. I mean, it just does nothing when in background.

I also tried what @zhangxiongwang suggested on #450 but I'm still unsuccessful.

Someone experiencing the same issue? Please help! thanks!

Exact same problem here, it just doesn't work on IOS. Would love a solution.

JesperBalslev avatar Jul 04 '19 17:07 JesperBalslev

thanks https://github.com/lkonzen-garupa/cordova-plugin-background-mode has fixed the error

fdambrosio avatar Jul 07 '19 15:07 fdambrosio

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

Genarito avatar Jul 07 '19 18:07 Genarito

I can't get this to work on device with iOS 12.

I got rid of the error TypeError: cordova.plugins.backgroundMode.onactivate is not a function. (In 'cordova.plugins.backgroundMode.onactivate()' doing what @daviddickson suggested on #433 but the plugin isn't working either.. I mean, it just does nothing when in background.

I also tried what @zhangxiongwang suggested on #450 but I'm still unsuccessful.

Someone experiencing the same issue? Please help! thanks!

So I took all those changes and the ones posted on https://github.com/lkonzen-garupa/cordova-plugin-background-mode and made a new fork https://github.com/platanus/cordova-plugin-background-mode.

The weird thing is that it works on ios only when cordova-plugin-background-geolocation is activated.

any guess? Thanks

imarquezc avatar Aug 01 '19 14:08 imarquezc

Nevermind.. I was missing the "Audio" capabilitie in background modes.. Now it works perfectly! Still can't figure out why it worked before without that capabilitie and the cordova-plugin-background-geolocation activated though..

imarquezc avatar Aug 01 '19 16:08 imarquezc

https://stackoverflow.com/a/54398403/6617276

kagisoW avatar Mar 28 '20 20:03 kagisoW