Observable is not working when app is running in background in ionic? - ionic-framework

When I do Login I am starting to check observable working but when application is in background it is not executing. I am using observable because after every 5 seconds refresh should happen. It seems it is not running in background. Any other solution to run the code after every 1 minute whether application is in background or foreground. Thanks in advance !
Loginsuccessfully
this.provider.startBackgroundRefreshInterval();
In service class below code added
startBackgroundRefreshInterval() {
var refreshInterval = localStore.getPersistedData(App.ISCHECKBOX_KEY);
this.timerSubscription = Observable.interval(parseInt(refreshInterval))
.subscribe((val) => {
console.log("Observable subscribed");
});
}
/**
* unsubscribe subscription
*/
unSubscribeOnehourSubscription() {
console.log("Observable Unsubscribed");
this.timerOneHourSubscription.unsubscribe();
}
Now refreshInterval is based on checkbox value selection
updateCheckboxList(value:any) {
this.service.unSubscribeSubscription();
this.service.startBackgroundRefreshInterval();
}
Once application will go in background Observable not calling
currently refreshInterval selected as 1 minute
what is other way to call functon continuosly in background in ionic ?

I am currently working on the same thing. While I can't say I know all the things about this, here is what I have found so far.
When your app goes to the background, it is essentially put to sleep. Meaning none of the code you are counting on, including intervals is going to execute.
Here are some solutions to this problem I have worked on.
1) BackgroundFetch as seen here in this cordova plugin https://github.com/transistorsoft/cordova-plugin-background-fetch and https://ionicframework.com/docs/native/background-fetch will in both android and ios attempt to run a callback every approximately 15 minutes (read the link for why that timing is a thing).
2) BackgroundMode as seen here https://ionicframework.com/docs/native/background-mode will put your app into a "stay active while in background" mode that you can control. This will work in keeping your app up and running even in background, but you may find that this is not acceptable to the stores (particularly might get rejected by ios store).
That's what I've found thus far. Hope it's helpful.

Related

How to restart an app in ionic after not using it for 5 or 10 min?

I have a query about ionic with capacitor v3.
I would like to be able to restart the app at a specific time when the app is in the background, example: I have my app open but then I start using other applications and so 5 to 10 minutes elapse, when I reopen my app I want it to be show the splash screen and reload the current data. This does not currently happen, I can leave the app in the background for about 20 minutes and when I open it again I have the same page and the same products with the same stock. I don't know if this has to do with the life cycle of the app as it happens in Flutter.
Thanks a lot.
You cannot restart it, but you can detect when app is in the background or in the foreground and then write the logic where you want (for example, navigate to home page and refresh data).
this.platform.pause.subscribe(async () => {
//write your logic here when app goes to background
});
this.platform.resume.subscribe(async () => {
//write your logic here when app is brought to foreground
});

How do I prevent my app from running in the "recent menu" on Android 12

Android 12 introduced the behavior where the last app you had active are continuing to run when inside the recent menu. I'm wondering if there's a flag or something to put in AndroidManifest file to prevent this behavior as it's conflicting with some logics in our app (which works fine for Android < 12). I've googled but it's hard to find unless you know exactly what this "feature" is called.
Steps:
Start app
Open recent menu
Observe that you can interact and that the app is still running as if you had it open/active
Why is this a problem? Because a user is now able to force quit the app (swiping it away) without entering the "paused" state in our game (using Unity) meaning some save logic won't run.
This can be worked around in one way or another, but for now I would like to just pause the app in recent menu if possible (our app has zero reason for being active in recent menu).
EDIT:
As #WyattL mentioned in his answer, android:excludeFromRecents="true" might work, but it will cause drop in playtime and retention of the game and would prefer to have a more proper solution to this "unnecessary" feature of Android 12.
I can't be sure without testing on every phone as it seems the issue varies by device (thanks Ruzihm), but if opening the Recent Apps screen generates an OnApplicationFocus() call, this would provide a solution.
In an active MonoBehaviour:
private void OnApplicationFocus( bool focus )
{
if( focus ) {
UnpauseLogic();
}
else {
PauseLogic();
SaveLogic();
}
}
(It might also be worth trying OnApplicationQuit() in case it's called on specific devices during a swipe termination, but in my own tests it was never called.)
According to some brief research, did you try adding
<activity>
...
android:excludeFromRecents="true"
android:label=""
...
</activity>
to the AndroidManifest.xml?

applicationShouldTerminate called unexpectedly in macOS Swift app

Many people appear to have a problem where their AppDelegate's applicationShouldTerminate is never called. I have the opposite problem!
Often (at a guess 20% of the time) when I come back to my Mac in the morning, I discover that something caused my app's applicationShouldTerminate to fire. My app then ran its cleanup code, after which applicationWillTerminate fired. However the app never shut down — it's still marked as running in my dock, and when I click on it there, applicationDidFinishLaunching fires and it starts up. Because it was already running, the dock icon does not bounce.
The logs indicate this seems to only happen when I wrap up for the day and my Mac goes to sleep, possibly only after having been plugged back in after running off its battery.
At first I thought it might be because my Mac was trying to shutdown apps to install a system update but this happens even when there are no updates available. And no other apps on my system have the issue.
The same happens with my app on a friend's Mac.
I do have a "tricky" applicationShouldTerminate to get around run loop issues (nb. I'm using Promises):
var shuttingDown: Bool = false
func applicationShouldTerminate(_ sender: NSApplication) -> NSApplication.TerminateReply {
log.debug("applicationShouldTerminate")
if shuttingDown { return .terminateNow }
shuttingDown = true
StuffController.shared.terminateRunningStuff()
.timeout(20) // If it hasn't shutdown within 20 seconds, force it.
.always {
// Tell the app it should now actually terminate.
NSApplication.shared.terminate(self)
}
return .terminateCancel
}
Can anyone suggest a reason my applicationShouldTerminate is firing without the user asking it to quit?
Turns out this is a feature not a bug (lol). In Mac OS X Lion (10.7) Apple introduced a feature called "Automatic Termination" where apps would automatically quit after a while of inactivity.
Note that this is intended to be invisible to the end-user; the app appears to be running in the dock, and should restore itself when needed, as if nothing ever happened.
It can be enabled or disabled via the "Application can be killed to reclaim memory or other extraordinary circumstances" configuration in Xcode (the NSSupportsAutomaticTermination key in an app's Info.plist).

OneSignal sendTag right after init doesn't work

I am using OneSignal in a Ionic project. I am initializing OneSignal when device is ready:
window.plugins.OneSignal.setLogLevel({logLevel: 6, visualLevel: 4});
window.plugins.OneSignal.startInit(appId)
// Controls how OneSignal notifications will be shown when one is received while your app is in focus
.inFocusDisplaying(window.plugins.OneSignal.OSInFocusDisplayOption.None) // None means silent
.endInit();
// set OS tag to uid in case multiple users on same device
window.plugins.OneSignal.sendTag("uid", user.auth.uid);
});
As you can see, right after init I need to send a tag.
My problem is that when I launch the app for the first time, the init it done properly but the sendTag seems not to be done... (on second launch the sendTag is done though).
But I need the sendTag to be done the first time around...
I figured that may be the init is not done yet (or something like that) and that it prevents the sendTag to happen...
Do you know if that's the real problem here ? And would you have any suggestion ? May be a way to know when init is finished (catch a OneSignal event)...
** UPDATE ****
It looks like sendTag if working after all (I simplified my code for the purpose of this post). But the real initial issue I had was when sendTag is called in the getTags' callback.
The following code doesn't executes when written right after endInit():
window.plugins.OneSignal.getTags(function(tags) {
if (!tags) window.plugins.OneSignal.sendTag("uid", user.auth.uid);
});
Any idea?

How to simulate the very first start of an app in Xcode 6

I need to test some behaviors of the iOS 8 at the very first start of my app. Is it possible to simulate this in Xcode 6? If yes, then how?
Deleting the app will do it but note that certain pieces of information will be cached for a while like your permissions settings (notification, calendar, etc.). You can go to settings.app and reset settings to clear those out if that matters in your use case.
If you mean the FIRST start of the app, then what I did to achive this is, on start (viewDidLoad) check in the NSUserDefaults for example ,if the value "hasAlreadyStarted" exists (NSUserDefaults.objectForKey(..) ), if not, then its the first start of the app, and then i would set the value to true, so when you close the app, and open again, the value will exist.
Dareon I'm not sure what exactly you want to achieve. In Xcode 6 yes you can simulate your app from start. If you want to test behaviours I think you are looking for instruments. Right Click on the Xcode icon in your dock select option and choose instruments. You can add several instruments your phone or emulator support like connectivity or gps or memory to see exactly the behaviour of your app. Hope that helps
Well if by very start of the app cycle you mean before the app loads, there is a way.
In your ViewController call the ViewWillLoad function:
class ViewController
{
override func viewWillAppear(animated: Bool)
{
// your code
}
}
This event will be called before the view loads or appears.
Hope it helps :)
As Shanti K said in the comment, if you delete your application from the simulator and then run it again, you will be simulating the first run. To delete the application from the simulator, you mimic the same behavior on a device.
Click and hold on the icon, until they start shaking. Click the close X next to your application, and verify that you want to delete it if it asks. Then Shift + Command + H to simulate hitting the home button.