问题描述
我想在 Flutter(Android 应用程序)中设置本地通知,我了解了 workmanager 包,但我不知道如何将其用于我的目的。我想在用户选择的确切时间发送通知。
时间选择:
Container(
padding: EdgeInsets.all(15),child: IconButton(icon: Icon(Icons.alarm,color: Colors.white,size: 70,),onpressed: (){
DatePicker.showTimePicker(context,theme: DatePickerTheme(
containerHeight: 210.0,showTitleActions: true,onConfirm: (time) {
ttime = '${time.hour} : ${time.minute} : ${time.second}';
//print(_time);
_setNotifyTime();
setState(() {});
},currentTime: DateTime.Now(),locale: LocaleType.en);
setState(() {});
}),
在 sharedpreferences 中保存这个时间:
Future<void> _setNotifyTime() async{
final prefs = await SharedPreferences.getInstance();
final savednotifyTime = await _getStringFromSharedPrefs();
await prefs.setString('notificationTime',ttime);
//print("this $savednotifyTime");
return savednotifyTime;
}
从共享首选项中获取此时间:
Future<String> _getStringFromSharedPrefs() async{
final prefs = await SharedPreferences.getInstance();
notifyTime = prefs.getString('notificationTime');
return notifyTime;
}
按选定时间发送本地通知:
Future<void> showDailyAtTime() async {
var splited = notifyTime.split(':');
int hour = int.parse(splited[0]);
int minute = int.parse(splited[1]);
int second = int.parse(splited[2]);
print(hour.toString());
print(minute.toString());
print(second.toString());
var reviewTime = Time(hour,minute,second);
var androidChannelSpecifics = AndroidNotificationDetails(
'CHANNEL_ID 2','CHANNEL_NAME 2',"CHANNEL_DESCRIPTION 2",importance: Importance.max,priority: Priority.high,);
var iosChannelSpecifics = IOSNotificationDetails();
var platformChannelSpecifics =
NotificationDetails(android: androidChannelSpecifics,iOS: iosChannelSpecifics);
await FlutterlocalnotificationsPlugin.showDailyAtTime(
0,'Title at ${reviewTime.hour}:${reviewTime.minute}.${reviewTime.second}','Test Body',//null
reviewTime,platformChannelSpecifics,payload: 'Test Payload',);
}
WorkManager 实现:
void callbackdispatcher() {
Workmanager().executeTask((taskName,inputData) async {
notificationPlugin.showDailyAtTime();
return Future.value(true);
});
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Workmanager().initialize(callbackdispatcher);
await Workmanager().registerPeriodicTask("test_workertask","test_workertask",inputData: {"data1": "value1","data2": "value2"},frequency: Duration(minutes: 15),initialDelay: Duration(minutes: 15));
runApp(MyApp());
}
当我从应用程序内的按钮调用它时,这是有效的,但一旦我进入后台,它就不起作用。 当我实现上面的代码时,它给出了以下错误:
HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_2
D/EGL_emulation( 6089): eglMakeCurrent: 0xe90561a0: ver 2 0 (tinfo 0xe0eadb70)
E/Flutter ( 6089): [ERROR:Flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'split' was called on null.
E/Flutter ( 6089): Receiver: null
E/Flutter ( 6089): Tried calling: split(":")
E/Flutter ( 6089): #0 Object.noSuchMethod (dart:core- patch/object_patch.dart:54:5)
E/Flutter ( 6089): #1 NotificationPlugin.showDailyAtTime (package:finance_manager/widgets/NotificationPlugin.dart:107:30)
E/Flutter ( 6089): #2 callbackdispatcher.<anonymous closure> (package:finance_manager/main.dart:14:24)
E/Flutter ( 6089): #3 callbackdispatcher.<anonymous closure> (package:finance_manager/main.dart:12:29)
E/Flutter ( 6089): #4 Workmanager.executeTask.<anonymous closure> (package:workmanager/src/workmanager.dart:89:28)
E/Flutter ( 6089): #5 Workmanager.executeTask.<anonymous closure> (package:workmanager/src/workmanager.dart:87:45)
E/Flutter ( 6089): #6 MethodChannel._handleAsMethodCall (package:Flutter/src/services/platform_channel.dart:435:55)
E/Flutter ( 6089): #7 MethodChannel.setMethodCallHandler.<anonymous closure> (package:Flutter/src/services/platform_channel.dart:382:34)
E/Flutter ( 6089): #8 _DefaultBinaryMessenger.handlePlatformMessage (package:Flutter/src/services/binding.dart:284:33)
E/Flutter ( 6089): #9 _invoke3.<anonymous closure> (dart:ui/hooks.dart:221:15)
E/Flutter ( 6089): #10 _rootRun (dart:async/zone.dart:1354:13)
E/Flutter ( 6089): #11 _CustomZone.run (dart:async/zone.dart:1258:19)
E/Flutter ( 6089): #12 _CustomZone.runGuarded (dart:async/zone.dart:1162:7)
E/Flutter ( 6089): #13 _invoke3 (dart:ui/hooks.dart:220:10)
E/Flutter ( 6089): #14 Platformdispatcher._dispatchPlatformMessage (dart:ui/platform_dispatcher.dart:457:7)
E/Flutter ( 6089): #15 _dispatchPlatformMessage (dart:ui/hooks.dart:90:31)
E/Flutter ( 6089):
I/WM-WorkerWrapper( 6089): Worker result SUCCESS for Work [ id=87272c98-d905-4b69-a1ce-86c13336ab8b,tags={ be.tramckrijte.workmanager.BackgroundWorker } ]
请帮帮我。
感谢您提前回复。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)