How do I get badge to reset when the app is minimized? - flutter

We need some help with the flutter app badge.
We are completing a chat app in Flutter that runs on Firebase.
We want the app to reset its badge to Zero when a user checked all the unread messages and minimized the app. Currently, the reset is not happening. This is our screenplay.
Our Screenplay
We use flutter_app_badger with this code:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
log("MyApp: AppLifecycaeState.resumed");
FlutterAppBadger.removeBadge();
break;
case AppLifecycleState.paused:
updateMyActiveStatus(false);
log("MyApp: AppLifecycleState.paused");
break;
case AppLifecycleState.inactive:
log("MyApp: AppLifecycleState.inactive");
break;
case AppLifecycleState.detached:
log("MyApp:
AppLifecycleState.detached");
break;
default:
break;
}
We also tried this code:
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
log("MyApp: AppLifecycaeState.resumed");
break;
case AppLifecycleState.paused:
updateMyActiveStatus(false);
log("MyApp: AppLifecycleState.paused");
FlutterAppBadger.removeBadge();
break;
case AppLifecycleState.inactive:
log("MyApp: AppLifecycleState.inactive");
break;
case AppLifecycleState.detached: l
log("MyApp: AppLifecycleState.detached");
break;
default:
break;
}
Neither seems to work.
We read that the Flutter App Badger is not fully compatible with all Android devices but it should work with Samsung, HTC and many Android brands.
In our testing, we use Samsung.

Related

Multiple WidgetsBindingObserver widgets performances

I would simply like to know if there is any performances issue by using in multiple widgets the WidgetsBindingObserver and more specifically, the call to :
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
switch (state) {
case AppLifecycleState.resumed:
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
Is it the same listener for the whole or is it memory consuming for multiple listeners added to multiple places ?

Flutter use WidgetsBindingObserver only when on top of the stack

I have a StatefulWidget and at the beginning of the State method's build I do
WidgetsBinding.instance
.addObserver(LifecycleEventHandler(resumeCallBack: () async {
if (mounted) {
refreshState();
}
}));
This is the code of the LifecycleEventHandler
class LifecycleEventHandler extends WidgetsBindingObserver {
final AsyncCallback resumeCallBack;
LifecycleEventHandler({
this.resumeCallBack,
});
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
if (resumeCallBack != null) {
await resumeCallBack();
}
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
}
the resumeCallBack is being called every time the app is resumed, even if the page is not visible to the user (if I navigate to another page without removing this from the stack).
Is there a way to run this callback only if the page is visible to the user? I don't want to dispose the state/page but just avoid unnecessary state updates

Is it possible to manually control the flutter AppLifeCycleState?

Is it possible to manually control the flutter AppLifeCycleState?
#override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.resumed:
debugPrint("Colorun - resumed");
break;
case AppLifecycleState.inactive:
debugPrint("Colorun - inactive");
break;
case AppLifecycleState.paused:
debugPrint("Colorun - paused");
break;
case AppLifecycleState.detached:
debugPrint("Colorun - detached");
break;
}
}
Is it possible to change the flutter app from running state to paused state?

Flutter interstitial ad with AppOpen ads

Currently Im using native_admob_flutter for my flutter app. I also included AppOpenAds and Interstital ad within the app.
I want to display the AppOpen ad when user open the app/ resume to the app, here is how I do it in main.dart:
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
switch (state) {
case AppLifecycleState.resumed:
loadAndDisplayAppOpenAd();
break;
case AppLifecycleState.inactive:
break;
case AppLifecycleState.paused:
break;
case AppLifecycleState.detached:
break;
}
}
However I found that displaying Interstial ad will also change the app state -> inactive -> paused -> resume, which trigger AppOpen ad.
This really create bad user experinence. Is there a way to overcome this? Thank you!
You may need a variable to make sure that app has been paused by user action, and not by interstitial.
static bool pausedByInterstitial = false;
...
class AppLifecycleReactor extends WidgetsBindingObserver {
final AppOpenAdManager appOpenAdManager;
AppLifecycleReactor({required this.appOpenAdManager});
static bool pausedByInterstitial = false;
#override
Future<void> didChangeAppLifecycleState(AppLifecycleState state) async {
if (state == AppLifecycleState.resumed && !pausedByInterstitial) {
appOpenAdManager.showAdIfAvailable();
}
}
}
...
showInter() {
AppLifecycleReactor.pausedByInterstitial = true;
InterstitialListener(
...
onAdHiddenCallback: (ad) {
AppLifecycleReactor.pausedByInterstitial = false;
},
...
);
}

flutter didChangeAppLifecycleState never runs

I implemented the WidgetsBindingObserver, but the app is NEVER sent to the background so it doesn't recognize the AppLifecycleState.resumed
this is the current implementation
#override
void didChangeAppLifecycleState(AppLifecycleState state) async {
print('\n\ndidChangeAppLifecycleState');
switch (state) {
case AppLifecycleState.resumed:
print('\n\nresumed');
_mymethod();
break;
case AppLifecycleState.inactive:
print('\n\ninactive');
break;
case AppLifecycleState.paused:
print('\n\npaused');
break;
case AppLifecycleState.detached:
print('\n\ndetached');
break;
}
}
to simulate the process i do the next in android
run the project as --release
open the widget with the WidgetsBindingObserver
open another app (like chrome or phone settings)
return to the app
when returning to the app i can see my widget on screen, the app doesn't restart, but NONE of the prints appears on the console not event the print('\n\ndidChangeAppLifecycleState'); and _mymethod(); is never executed
The WidgetsBindingObserver mixin requires a bit more work than merely implementing the interface. You also need to add the following to your widget state class:
#override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
}
#override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}