flutter - 如何让 iOS 上的 Workmanager 颤动
问题描述
我工作了一个项目来运行工作管理器,以使应用程序在用户在应用程序之外时在后台运行。问题是现在Android上的应用程序正在运行,但是在iOS上运行并在Mac上运行模拟器,出现以下问题:
[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: MissingPluginException(No implementation found for method initialize on channel be.tramckrijte.workmanager/foreground_channel_work_manager)
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:7)
<asynchronous suspension>
#1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
#2 Workmanager.initialize (package:workmanager/src/workmanager.dart:100:30)
#3 _MyHomePageState.initState (package:toios/main.dart:58:17)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4684:58)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
#6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#7 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#8 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
#9 Element.inflat<…>
[VERBOSE-2:ui_dart_state.cc(166)] Unhandled Exception: MissingPluginException(No implementation found for method registerPeriodicTask on channel be.tramckrijte.workmanager/foreground_channel_work_manager)
#0 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:159:7)
<asynchronous suspension>
#1 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:334:12)
#2 Workmanager.registerPeriodicTask (package:workmanager/src/workmanager.dart:158:32)
#3 _MyHomePageState.initState (package:toios/main.dart:63:17)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4684:58)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4520:5)
#6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3490:14)
#7 Element.updateChild (package:flutter/src/widgets/framework.dart:3258:18)
#8 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5874:14)
#9<…>
完整的代码页如下:
import 'package:flutter/material.dart';
import 'package:geolocator/geolocator.dart';
import 'package:workmanager/workmanager.dart';
import 'package:toios/notification.dart' as notif;
const fetchBackground = "fetchBackground";
void callbackDispatcher() {
Workmanager.executeTask((task, inputData) async {
switch (task) {
case fetchBackground:
//Geolocator geoLocator = Geolocator()..forceAndroidLocationManager = true;
Position userLocation = await Geolocator().getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
notif.Notification notification = new notif.Notification();
notification.showNotificationWithoutSound(userLocation);
break;
}
return Future.value(true);
});
}
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();
// We don't need it anymore since it will be executed in background
//this._getUserPosition();
Workmanager.initialize(
callbackDispatcher,
isInDebugMode: true,
);
Workmanager.registerPeriodicTask(
"1",
fetchBackground,
frequency: Duration(minutes: 30),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
"",
style: Theme.of(context).textTheme.display1,
),
],
),
),
);
}
}
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:geolocator/geolocator.dart';
class Notification {
FlutterLocalNotificationsPlugin flutterLocalNotificationsPlugin;
Future showNotificationWithoutSound(Position position) async {
print(position.toString());
var androidPlatformChannelSpecifics = new AndroidNotificationDetails(
'1', 'location-bg', 'fetch location in background',
playSound: false, importance: Importance.Max, priority: Priority.High);
var iOSPlatformChannelSpecifics =
new IOSNotificationDetails(presentSound: false);
var platformChannelSpecifics = new NotificationDetails(
androidPlatformChannelSpecifics, iOSPlatformChannelSpecifics);
await flutterLocalNotificationsPlugin.show(
0,
'Location fetched',
position.toString(),
platformChannelSpecifics,
payload: '',
);
}
Notification() {
var initializationSettingsAndroid = new AndroidInitializationSettings('@mipmap/ic_launcher');
var initializationSettingsIOS = new IOSInitializationSettings();
var initializationSettings = new InitializationSettings(
initializationSettingsAndroid, initializationSettingsIOS);
flutterLocalNotificationsPlugin = new FlutterLocalNotificationsPlugin();
flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
}
有谁知道解决问题以在 iOS 上运行它?
解决方案
推荐阅读
- scala - 如何在 PySpark 中添加自定义 JDBC 方言
- testing - 更改 $*DISTRO 值以进行测试
- python - 带有异步错误的 Discord 机器人
- excel - Excel VBA删除和上移单元格但仅在一定范围内
- google-chrome - 如何将 Chrome 和 Firefox 浏览器集成到 AutoHotKey
- javascript - 使用带有 xlink:href 的 javascript 在 SVG 弯曲文本中分配路径不起作用
- asp.net - ASPX 有变化。CS页面需要构建。
- javascript - 将 GET 参数传递给 jsfiddle 嵌入式版本
- reactjs - React 组件不会改变父级的状态
- firebase - Firestore 聊天应用程序结构 - 未读/已读消息架构