Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
daviddickson opened this issue Feb 28, 2019 · 27 comments

Comments

@daviddickson
Copy link
Contributor

daviddickson commented Feb 28, 2019

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')
      })
    });
  }
@i91
Copy link

i91 commented Mar 1, 2019

See pull request - #423

@daviddickson
Copy link
Contributor Author

@i91 I'm seeing this issue on iOS.

@daviddickson
Copy link
Contributor Author

Fixed in pull request #433

@Genarito
Copy link

Same problem on Android

@lkonzen-garupa
Copy link

I'm having the same problem on Android.

image

@Genarito
Copy link

Genarito commented Mar 14, 2019

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

@ZaLiTHkA
Copy link

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
Copy link

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. :/

@ewwwgiddings
Copy link

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 ?

@fax1ty
Copy link

fax1ty commented Apr 7, 2019

image
Any progress?

@lkonzen-garupa
Copy link

@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

@rastographics
Copy link

rastographics commented Apr 10, 2019

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?

@lkonzen-garupa
Copy link

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

@rastographics
Copy link

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?

@lkonzen-garupa
Copy link

I understand.
Maybe that commit is creating this problem:
lkonzen-garupa@b2a9cd9

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.

@n1705771
Copy link

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

@lkonzen-garupa
Copy link

@n1705771 it's all working fine.

@rastographics
Copy link

@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?

@lkonzen-garupa
Copy link

@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();
});

@MInesGomes
Copy link

MInesGomes commented Apr 16, 2019

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

@imarquezc
Copy link

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!

@JesperBalslev
Copy link

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.

@fdambrosio
Copy link

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

@Genarito
Copy link

Genarito commented Jul 7, 2019

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

@imarquezc
Copy link

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
Copy link

imarquezc commented Aug 1, 2019

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..

@kagisoW
Copy link

kagisoW commented Mar 28, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests