cordova-plugin-background-mode
cordova-plugin-background-mode copied to clipboard
'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')
})
});
}
See pull request - https://github.com/katzer/cordova-plugin-background-mode/pull/423
@i91 I'm seeing this issue on iOS.
Fixed in pull request https://github.com/katzer/cordova-plugin-background-mode/pull/433
Same problem on Android
I'm having the same problem on Android.
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
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.
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
onactivateand
ondeactivate`.
so now I'm even more confused by which way this whole thing is going. :/
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 ?
Any progress?
@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
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 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');
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?
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 have you solved this issue? I have the same issue...
@n1705771 it's all working fine.
@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 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();
});
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 :-)
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!
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.
thanks https://github.com/lkonzen-garupa/cordova-plugin-background-mode has fixed the error
Thats the problem: using a forked version is always the solution
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
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..
https://stackoverflow.com/a/54398403/6617276