Skip to content

Commit

Permalink
Fixed: android background fails on first run
Browse files Browse the repository at this point in the history
On latest versions of Android initialization fails on first run.

If this happens, after getting the permissions, we re-try the
initialization.

Also:

- The notification is not longer used by default.

- Badge removed from the icon when the notification is used.

Close #162
  • Loading branch information
J-Pabon committed Jan 25, 2023
1 parent f1f463b commit 8d7d89f
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions lib/app/utils/platform/platform_background_manager_mobile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class PlatformBackgroundManagerMobile
notificationText: S.current.messageBackgroundNotificationAndroid,
notificationIcon: const AndroidResource(name: 'notification_icon'),
notificationImportance: AndroidNotificationImportance.Default,
showBadge: false,
enableWifiLock: true,
);

Expand All @@ -36,9 +37,25 @@ class PlatformBackgroundManagerMobile
});
}

hasPermissions = await FlutterBackground.initialize(androidConfig: config);
// On recent Android versions (13), the first time the plugin is initialized,
// it fails -even after the user granted permissions. Then on the second run
// it works as expected.
//
// There is curently an issue referencing this in the plugin's repository,
// and the workaround for it:
// https://github.com/JulianAssmann/flutter_background/issues/56#issuecomment-1218307725
final initializationOk =
await FlutterBackground.initialize(androidConfig: config)
.then((initialized) async {
final permissionsOk = await FlutterBackground.hasPermissions;
if (!initialized && permissionsOk) {
return await FlutterBackground.initialize(androidConfig: config);
}

return initialized;
});

if (hasPermissions) {
if (initializationOk) {
final backgroundExecution =
await FlutterBackground.enableBackgroundExecution();

Expand Down

0 comments on commit 8d7d89f

Please sign in to comment.