katzer / cordova-plugin-background-mode

Keep app running in background
Apache License 2.0
1.38k stars 1.02k forks source link

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

Open daviddickson opened 5 years ago

daviddickson commented 5 years ago

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 commented 5 years ago

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

daviddickson commented 5 years ago

@i91 I'm seeing this issue on iOS.

daviddickson commented 5 years ago

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

Genarito commented 5 years ago

Same problem on Android

lkonzen-garupa commented 5 years ago

I'm having the same problem on Android.

image

Genarito commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 likeon{EVENT}, such asonactivateandondeactivate`.

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

ewwwgiddings commented 5 years ago

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 commented 5 years ago

image Any progress?

lkonzen-garupa commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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.

n1705771 commented 5 years ago

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

lkonzen-garupa commented 5 years ago

@n1705771 it's all working fine.

rastographics commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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

Genarito commented 5 years ago

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

imarquezc commented 5 years ago

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 commented 5 years ago

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 commented 4 years ago

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