我有一个
flutter
,它是使用flutterflow
应用程序构建的,该应用程序正在尝试在其中运行后台服务..此服务应该每3分钟运行一个功能..
这是我当前的代码:
// Automatic FlutterFlow imports
import '/backend/schema/structs/index.dart';
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!
import 'dart:io'; // For platform-specific checks
import 'package:flutter_background_service/flutter_background_service.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
late Function doableAction;
Future mainAction(Future Function() theAction) async {
doableAction = theAction;
await initializeService();
}
Future<void> initializeService() async {
if (Platform.isAndroid) {
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'your_notification_channel_id', // id
'Your Notification Channel', // title
importance: Importance.high,
);
final FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
await flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
final service = FlutterBackgroundService();
// Configure and start the service
await service.configure(
androidConfiguration: AndroidConfiguration(
onStart: onStart,
autoStart: true,
isForegroundMode: true, // Ensure it runs as a foreground service
notificationChannelId: 'your_notification_channel_id',
initialNotificationTitle: 'Azan Notify',
initialNotificationContent: 'Azan notify is running in background',
),
iosConfiguration: IosConfiguration(
onForeground: onStart,
autoStart: true,
),
);
// Start the service
service.startService();
// Execute the custom action once after service initialization
await doTheAction();
}
// This function will run the background task
Future<void> onStart(ServiceInstance service) async {
if (service is AndroidServiceInstance) {
service.on('setAsForeground').listen((event) {
service.setAsForegroundService();
});
service.on('setAsBackground').listen((event) {
service.setAsBackgroundService();
});
service.on('onTaskRemoved').listen((event) {
service.stopSelf();
});
}
// Run the action repeatedly in a loop
while (true) {
// Removed the `isRunning` check
print('Running doTheAction in background...');
await doTheAction();
// Wait for 3 minutes before repeating the action
await Future.delayed(Duration(minutes: 3));
}
}
Future<void> doTheAction() async {
try {
print('Executing doableAction...');
await doableAction(); // Ensure this is an async function
} catch (e) {
print('Error in doTheAction: $e');
}
}
我的操作
doableAction
正在播放应用程序资产中的音频文件..结果如下:
1-当打开应用程序并到达我调用
mainActoin
函数的页面时..音频文件按预期播放..并且即使我将应用程序保留在前台也不会再次运行。
2-当我将应用程序置于后台时..我看到通知..但音频文件再也不会播放..
1.为两个平台(Android & iOS)分配权限,
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
<uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<application>
<service android:name="com.example.projectName"
android:permission="android.permission.BIND_JOB_SERVICE"
android:foregroundServiceType="dataSync"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
</intent-filter>
</service>
<meta-data
android:name="flutterEmbedding"
android:value="2"/>
</application>
</manifest>
android:foregroundServiceType="dataSync" 中出现任何错误,只需将其替换为
android:foregroundServiceType="location"
info.plist
<array>
<string>fetch</string>
<string>processing</string>
<string>location</string>
</array>
您剩余的代码