[英]Flutter local notifications package not working when scheduling a notification not in the range of a few hours
我正在尝试创建一个应用程序,当我必须移动汽车时提醒我,所以我需要能够在未来 2 周内安排通知。 即使我处于离线状态,我也想收到通知,因此我使用的是 package 而不是 Firebase。 当我在不久的将来手动设置时间时,例如在一个小时的范围内,它一切正常,即使我关闭应用程序,但是当我设置正确的日期时间时,我从来没有收到它们。 我将向您展示我到目前为止所获得的(请记住,我正在 Android 上对其进行测试)。
这是我的 AndroidManifest 的一部分
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationReceiver" />
<receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
</intent-filter>
</receiver>
然后这是安排通知的文件
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
class InfoBottomSheet extends StatelessWidget {
final String address;
String dateNotification;
final int code;
final dio = new Dio();
FlutterLocalNotificationsPlugin localNotificationsPlugin = FlutterLocalNotificationsPlugin();
initializeNotifications() async {
var initializeAndroid = AndroidInitializationSettings('icon');
var initializeIOS = IOSInitializationSettings();
var initSettings = InitializationSettings(initializeAndroid, initializeIOS);
await localNotificationsPlugin.initialize(initSettings);
}
Future singleNotification( DateTime datetime, String message, String subtext, int hashcode) async {
var androidChannel = AndroidNotificationDetails(
'channel-id',
'channel-name',
'channel-description',
importance: Importance.Max,
priority: Priority.Max,
);
var iosChannel = IOSNotificationDetails();
var platformChannel = NotificationDetails(androidChannel, iosChannel);
localNotificationsPlugin.schedule(
hashcode, message, subtext, datetime, platformChannel,
);
}
void getDate() async {
Response response = await dio.get('http://myApiCall/car-move-date?address=${address.toUpperCase()}');
dateNotification = response.data['date'] + " " + response.data['time'] + ":00";
}
InfoBottomSheet(this.address, this.code);
@override
Widget build(BuildContext context) {
return Container(
child: Padding(
padding: EdgeInsets.all(5.0),
child: Column(
children: <Widget>[
Padding(
padding:EdgeInsets.fromLTRB(12,12,12,6),
child:Text(
address,
),
),
Padding(padding: EdgeInsets.fromLTRB(10,20,10,20),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
SizedBox(
width: 130,
height: 50,
child:RaisedButton(
elevation: 5.0,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(20.0),
),
color: Colors.white,
onPressed: () async {
await getDate();
await initializeNotifications();
print("Park here");
var parsedDate = DateTime.parse(dateNotification);
await singleNotification(
parsedDate,
address,
"You parked here",
code,
);
Navigator.pop(context);
},
)
),
],
)
),
);
}
我删除了无用的部分,因此可能有问题,比如缺少括号或类似的东西,但我向你保证,除了通知之外,一切正常。 getDate() function 调用我的 api 并返回类似“2020-05-30 20:37”的字符串,这应该是通知的时刻。 地址是数据库中街道名称的字符串。 代码是那条街道的唯一代码。 但正如我所说,一切都很好,所以不要浪费时间在那里寻找错误。
可能是您的 API 有问题。 您不需要提出 API 请求,dart 完全可以自己提供。
void getDate() { return DateTime.now(); }
output 类似于
2020-08-29 12:34:17.633
如文档中所述,一些 Android 手机会限制后台执行以节省电池电量。 所以这可能是个问题。 从设置中检查它并无限制地启用后台执行,然后重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.