我正在使用 Flutter 构建 IOS 和 Android 应用程序。我的应用程序需要在应用程序关闭时运行一个后台任务(假设大约每 15 分钟一次)。然后显示通知。我正在关注这篇文章,https://www.geeksforgeeks.org/background-local-notifications-in-flutter/?fbclid=IwAR0sKCVATW068AxuRIyDO693vcqwU0VTPVl1bgVkM3J3EGCHExv8P13dvz4。所以,我正在使用这个库,https://pub.dev/packages/workmanager。它适用于Android,但不适用于IOS。
这是我的代码。
void main() {
// needed if you intend to initialize in the `main` function
WidgetsFlutterBinding.ensureInitialized();
Workmanager().initialize(
// The top level function, aka callbackDispatcher
callbackDispatcher,
// If enabled it will post a notification whenever
// the task is running. Handy for debugging tasks
isInDebugMode: true
);
// Periodic task registration
Workmanager().registerPeriodicTask(
"2",
//This is the value that will be
// returned in the callbackDispatcher
"simplePeriodicTask",
// When no frequency is provided
// the default 15 minutes is set.
// Minimum frequency is 15 min.
// Android will automatically change
// your frequency to 15 min
// if you have configured a lower frequency.
frequency: const Duration(minutes: 15),
);
runApp(const MyApp());
}
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) {
// initialise the plugin of flutterlocalnotifications.
FlutterLocalNotificationsPlugin flip = FlutterLocalNotificationsPlugin();
// app_icon needs to be a added as a drawable
// resource to the Android head project.
var android = const AndroidInitializationSettings('@mipmap/ic_launcher');
var ios = const IOSInitializationSettings();
// initialise settings for both Android and iOS device.
var settings = InitializationSettings(android: android, iOS: ios);
flip.initialize(settings);
_showNotificationWithDefaultSound(flip);
return Future.value(true);
});
}
Future _showNotificationWithDefaultSound(flip) async {
var androidPlatformChannelSpecifics = const AndroidNotificationDetails("your channel id", "my channel name");
var iOSPlatformChannelSpecifics = const IOSNotificationDetails();
// initialise channel platform for both Android and iOS device.
var platformChannelSpecifics = NotificationDetails(android: androidPlatformChannelSpecifics, iOS: iOSPlatformChannelSpecifics);
await flip.show(0, 'GeeksforGeeks',
'Your are one step away to connect with GeeksforGeeks',
platformChannelSpecifics, payload: 'Default_Sound'
);
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
正如我所提到的,它按预期工作并在 Android 上显示通知。当我在 IOS 模拟器上运行它时,我收到以下错误。
An Observatory debugger and profiler on iPhone 12 Pro Max is available at: http://127.0.0.1:56183/fjCBE_KZDpw=/
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: PlatformException(unhandledMethod("registerPeriodicTask") error, Unhandled method registerPeriodicTask, null, null)
#0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:607:7)
#1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:156:18)
<asynchronous suspension>
#2 Workmanager.registerPeriodicTask (package:workmanager/src/workmanager.dart:173:7)
<asynchronous suspension>
The Flutter DevTools debugger and profiler on iPhone 12 Pro Max is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:56183/fjCBE_KZDpw=/
基本上 workmanager 不工作。我的代码有什么问题,我该如何解决?