Keep app running in the background

I am testing out ionic badges (on the homescreen icon on my app). Basically, I have a service running that polls and checks for new data, like messages. When a new message is received, the user would be notified via the badge that something has occurred within the app that needs their attention. However when the app closes, the badges dont appear on the icon on the homescreen until I re-enter the app. How do I keep the app running in the background? Is this possible with ionic?

Not possible at this time, but thereā€™s a proposal for a capacitor plugin.

Just found another capacitor plugin that maybe can be useful to you:

Hi @phild! I think using push notifications may support you better. Have you tried it? At the same time that you advise the user you can increase the badge on its phone home screen.

Hey @Hills90210 - iā€™m trying that one but so far no luck. Thanks for looking though!

Hey @TomCosta - I havent tried push notifications but I am trying the ionic badge, which I believe works very similarly to the push notifications (or maybe not?). I will look into it though.

Ultimately I think my issue is that once the app is in the background, the services halt until the app reopens again. So iā€™ve been trying to find a way to keep those services running while the app is in the background. Iā€™ll still look into push notifications though.

Ahh okay, I get you now. There is nothing to do with notifications for sure. If your focus is to maintain the background services working, you may try the Capacitor Background Tasks to do it. Hope it helps.

Does not work for capacitor v3

Hello, yes, it is possible, you need to go to the settings and search for your application and turn on the gun in the background, click windows settings and applications and there you have properties and you have to mark it to work in the background, I did it and itā€™s ok

I dont see my app in the background refresh settings. I am not sure if it is because it is not deployed to the app store or something? It is currently running only on my phone through xCode.

This is not something missing in Ionic. This is something missing the native frameworks. Ionic cannot make plugins for something that Apple and Google donā€™t support. Basically they do not want you to be able to use all the battery of the device, so they donā€™t allow this.
For Android, some hacks exist, but for iOS they do not.
There are background workers that will work for a while, but the OS will eventually kill your background thread.

You need remote push notifications sent from a server for this purposes. It is possible to send notifications that are invisible to the user but are only meant to trigger some event in the app, like a background thread fetching data.

I participated on building the Covid detection app in my country, and here there is a specific API (Exposure notification API) which was necessary to keep the app running in the background.
We tried for a long time to build this for our selves, but it cannot be done. You cannot keep a background thread alive infinitely in a custom app. The purpose is to protect the users.

7 Likes

This is just not true. There are many applications that stay alive in the background. Pretty much all exercise apps work this way. You can minimize the application and do other things, and the application stays alive, records sensor data, and can be popped back up anytime.

that post is 2 years old, things have changed. But the statement still is true. Ionic cannot create a plugin for this nor anyone else. This is not doable in a hybrid app. You need to just build the app in the native language.

The Cordova background-mode plugin was able to do this ā€œimpossible thingā€ up until recent versions of android. I guess this limitation must be a more recent development in the mobile world. Thanks for the feedback.

Hello friends, Iā€™m facing a similar problem where I need to keep the push notification active for a longer duration. So, I have come up with the following approach: running the plugin in the background and checking if the FCM plugin listener remains active within the background plugin. The objective is to monitor any state changes and, if there is a change, open a local push notification and configure it according to the desired settings.

This process would be executed at regular intervals, for example, every 1 minute. However, if there is a user action, the flow would be paused and would only resume if the app returns to the background.

I havenā€™t tested it yet; it was just an idea that crossed my mind. It might be a bad idea, but itā€™s just an idea.

Hi!
At my work we made this plugin
I hope it works

Guide:

  • You must first enable it (BackgroundMode.enable()) in order to start using its functions
  • Then, you can change the settings (BackgroundMode.setSettings())
  • You should also disable battery optimizations (BackgroundMode.disableBatteryOptimizations()). You can first check if they have already been disabled (BackgroundMode.isIgnoringBatteryOptimizations())
  • And thatā€™s it!
  • Also, what I do is listen to the ā€˜backButtonā€™ event with the Capacitor App plugin and send the app to the background after it is emitted (BackgroundMode.moveToBackground())

The code would be something like this:

await BackgroundMode.enable()
await BackgroundMode.setSettings({
          title: 'your_notification_title',
          text: 'your_notification_text',
          icon: 'ic_foreground_logo', // This will look for ic_foreground_logo.png in platforms/android/res/drawable/mipmap
          color: 'ff0000', // Hex format like 'ff0000'
          channelName: 'your_channel_name',
          channelDescription: 'your_channel_description',
          resume: true,
          hidden: false,
          disableWebViewOptimization: true
        });
const res = await BackgroundMode.isIgnoringBatteryOptimizations();
if (!res.isIgnoring) {
   await BackgroundMode.disableBatteryOptimizations();
}

App.addListener('backButton', () => {
      this.moveToBackground();
 });

Only for Background task:

2 Likes

The Ionic/Capacitor team is shipping a first party plugin for background tasks soon! (I think this summer some time). Learn more here - Background Tasks is coming to Capacitor! - YouTube

3 Likes

Is there any update on this? Has it been released?

Yes it has been released - Background Runner Capacitor Plugin API | Capacitor Documentation

1 Like

Hello everyone, how are you doing?

Iā€™ve been working on a feature for my app for a few days now, in which I need it to remain relevant after receiving a push notification. Here are some things Iā€™ve tried:

1 - Schedule the push notification using LocalNotification (without success), as the app needs to be open to detect the push notification.
2 - I tried using VOIP ConnectService on Android and CallKit on iOS, but due to a lack of understanding of native programming, I ended up putting that aside.
3 - I attempted to use background modes (Background Tasks) or (Background Run) with plugins, but I couldnā€™t implement it correctly. I need to detect the push notification when the app is primarily closed. It may be due to my lack of attention, but perhaps this is not the right path for what I need.
4 - I tried using native alarm scheduling, but I had little success in detecting the push notification when the app is in the background, and there were issues with scheduling the alarm after the app is closed. I created a class on Android for the app to work as a broadcast service.

My demand is complex for me! Itā€™s been challenging to make the app trigger some kind of relevant alert/notification/sound upon receiving the notification. Iā€™m losing hope. The hardest part of all is detecting the push notification when the app is in the background or completely closed to perform an action. :face_exhaling:

Has anyone done something similar or have any ideas on what I can follow? Any help or guidance would be greatly appreciated. Thank you!