Skip to content

Commit

Permalink
Fixed foreground services on android 14
Browse files Browse the repository at this point in the history
  • Loading branch information
Decoder07 committed Apr 10, 2024
1 parent f63811e commit e895923
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 24 deletions.
50 changes: 28 additions & 22 deletions packages/hmssdk_flutter/example/lib/foreground_task_handler.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:isolate';

import 'package:flutter_foreground_task/flutter_foreground_task.dart';
import 'package:hms_room_kit/hms_room_kit.dart';
import 'package:hmssdk_flutter_example/main.dart';

///[ForegroundTaskHandler] is a class that extends [TaskHandler]
Expand Down Expand Up @@ -50,29 +51,34 @@ class ForegroundTaskHandler extends TaskHandler {
}
}

void initForegroundTask() {
FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: '100ms_flutter_notification',
channelName: '100ms Flutter Notification',
channelDescription:
'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: "launcher")),
iosNotificationOptions:
const IOSNotificationOptions(showNotification: false),
foregroundTaskOptions: const ForegroundTaskOptions(),
);
Future<bool> initForegroundTask() async {
bool isPermissionsGiven = await Utilities.getPermissions();
if (isPermissionsGiven) {
FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: '100ms_flutter_notification',
channelName: '100ms Flutter Notification',
channelDescription:
'This notification appears when the foreground service is running.',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: "launcher")),
iosNotificationOptions:
const IOSNotificationOptions(showNotification: false),
foregroundTaskOptions: const ForegroundTaskOptions(),
);

///[startService] starts the foreground task
FlutterForegroundTask.startService(
notificationTitle: 'Foreground Service is running',
notificationText: 'Tap to return to the app',
callback: startCallback);
///[startService] starts the foreground task
FlutterForegroundTask.startService(
notificationTitle: 'Foreground Service is running',
notificationText: 'Tap to return to the app',
callback: startCallback);
return true;
}
return false;
}

///[stopForegroundTask] stops the foreground task
Expand Down
2 changes: 1 addition & 1 deletion packages/hmssdk_flutter/example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ class _HomePageState extends State<HomePage> {
Utilities.saveStringData(
key: "meetingLink", value: meetingLinkController.text.trim());
FocusManager.instance.primaryFocus?.unfocus();
initForegroundTask();
await initForegroundTask();
Navigator.push(
context,
MaterialPageRoute(
Expand Down
2 changes: 1 addition & 1 deletion packages/hmssdk_flutter/example/lib/qr_code_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class _QRCodeScreenState extends State<QRCodeScreen> {
Constant.roomCode = rawValue.trim();
}
Utilities.saveStringData(key: "meetingLink", value: rawValue.trim());
initForegroundTask();
await initForegroundTask();
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (_) => WithForegroundTask(
child: HMSPrebuilt(
Expand Down

0 comments on commit e895923

Please sign in to comment.