@@ -87,28 +87,37 @@ class AndroidForegroundRuntime {
8787 return ;
8888 }
8989
90- final l10n = lookupKickLocalizations ();
91- await ensurePermissions ();
92- if (await FlutterForegroundTask .checkNotificationPermission () !=
93- NotificationPermission .granted) {
94- return ;
95- }
96- await _setNotificationState (mode: _notificationModeProxy);
97- if (await isRunning ()) {
98- await FlutterForegroundTask .updateService (
90+ try {
91+ final l10n = lookupKickLocalizations ();
92+ await ensurePermissions ();
93+ if (await FlutterForegroundTask .checkNotificationPermission () !=
94+ NotificationPermission .granted) {
95+ return ;
96+ }
97+ await _setNotificationState (mode: _notificationModeProxy);
98+ if (await isRunning ()) {
99+ await FlutterForegroundTask .updateService (
100+ notificationTitle: l10n.runtimeNotificationTitle,
101+ notificationText: l10n.runtimeNotificationReturn,
102+ );
103+ return ;
104+ }
105+
106+ await FlutterForegroundTask .startService (
107+ serviceId: 701 ,
99108 notificationTitle: l10n.runtimeNotificationTitle,
100- notificationText: l10n.runtimeNotificationReturn,
109+ notificationText: l10n.runtimeNotificationManage,
110+ notificationInitialRoute: '/home' ,
111+ callback: startForegroundRuntimeCallback,
101112 );
102- return ;
113+ } on PlatformException {
114+ // Platform channel may not be available if the engine is still
115+ // recovering after a background/foreground transition. The proxy can
116+ // continue without the foreground service — it will be retried on the
117+ // next start cycle.
118+ } on StateError {
119+ // BackgroundIsolateBinaryMessenger may not be ready yet.
103120 }
104-
105- await FlutterForegroundTask .startService (
106- serviceId: 701 ,
107- notificationTitle: l10n.runtimeNotificationTitle,
108- notificationText: l10n.runtimeNotificationManage,
109- notificationInitialRoute: '/home' ,
110- callback: startForegroundRuntimeCallback,
111- );
112121 }
113122
114123 static Future <bool > ensureTemporaryRunning ({String ? notificationTitle}) async {
@@ -162,11 +171,17 @@ class AndroidForegroundRuntime {
162171 return ;
163172 }
164173
165- if (await isRunning ()) {
166- await FlutterForegroundTask .stopService ();
174+ try {
175+ if (await isRunning ()) {
176+ await FlutterForegroundTask .stopService ();
177+ }
178+ await FlutterForegroundTask .removeData (key: _notificationModeStorageKey);
179+ await FlutterForegroundTask .removeData (key: _notificationTitleStorageKey);
180+ } on PlatformException {
181+ // Platform channel may be unavailable during background transitions.
182+ } on StateError {
183+ // BackgroundIsolateBinaryMessenger may not be ready.
167184 }
168- await FlutterForegroundTask .removeData (key: _notificationModeStorageKey);
169- await FlutterForegroundTask .removeData (key: _notificationTitleStorageKey);
170185 }
171186
172187 static Future <void > _setNotificationState ({
0 commit comments