我有一个提醒应用程序。当时间到来时,会显示上述提醒的通知。我有一个“完成”按钮,可以从数据库中删除提醒。
从数据库中删除提醒在应用程序处于后台时有效,但在应用程序关闭时无效。
关闭后,我点击通知上的“完成”按钮并打开应用程序进行检查,仍然可以看到本应删除的提醒。
这是我的 notification.dart 文件的代码:
import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:nagger/data/reminders_data.dart';
// import 'package:timezone/timezone.dart' as tz;
class NotificationController {
// final notificationsPlugin = AwesomeNotifications();
static ReceivedAction? initialAction;
static void Function()? refreshHomePageCallback;
static Future<void> initializeLocalNotifications() async {
await AwesomeNotifications().initialize(
null,
[
NotificationChannel(
channelKey: '111',
channelName: 'nice_channel_name',
channelDescription: 'Shows Reminder Notification',
)
],
);
initialAction = await AwesomeNotifications()
.getInitialNotificationAction(removeFromActionEvents: false);
}
static void initializeCallback(void Function() func) {
refreshHomePageCallback = func;
}
static Future<void> showNotification(String notifTitle) async {
await AwesomeNotifications().createNotification(
content: NotificationContent(
id: 0,
channelKey: '111',
title: notifTitle,
),
actionButtons: [
NotificationActionButton(
key: 'done',
label: 'Done'
)
]
);
}
Future<bool> scheduleNotification(
int notifID,
String notifTitle,
DateTime scheduleNotificationDateTime
) async {
return await AwesomeNotifications().createNotification(
content: NotificationContent(
id: notifID,
channelKey: '111',
title: notifTitle,
payload: {
"App name": "Nagger"
},
autoDismissible: false
),
actionButtons: [
NotificationActionButton(
key: 'done',
label: 'Done',
actionType: ActionType.SilentBackgroundAction
),
],
schedule: NotificationCalendar(
year: scheduleNotificationDateTime.year,
month: scheduleNotificationDateTime.month,
day: scheduleNotificationDateTime.day,
hour: scheduleNotificationDateTime.hour,
minute: scheduleNotificationDateTime.minute,
second: scheduleNotificationDateTime.second,
millisecond: scheduleNotificationDateTime.millisecond
)
);
}
Future<void> cancelScheduledNotification(int id) async {
await AwesomeNotifications().cancel(id);
print("$id cancelled scheduled notification.");
}
static Future<void> startListeningNotificationEvents() async {
AwesomeNotifications().setListeners(onActionReceivedMethod: onActionReceivedMethod);
}
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction
) async {
if (receivedAction.buttonKeyPressed == 'done') {
final db = RemindersData();
db.deleteReminder(receivedAction.id ?? 7);
// refreshHomePageCallback!();
}
else
{
print("Unknown action with notification.");
}
}
}
如何在应用程序关闭时使其正常运行?
try {
var response = await http.get(Uri.parse(url));
if (Platform.isAndroid) {
await Permission.storage.request();
}
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
if (response.statusCode == 200) {
String fileName = 'certificate_$certificateNumber.pdf';
const downloadsFolderPath = '/storage/emulated/0/Download/';
Directory dir = Directory(downloadsFolderPath);
File file = File('${dir.path}/$fileName');
OpenFile.open(file.path);
AwesomeNotifications().createNotification(
actionButtons: [
NotificationActionButton(
key: 'OPEN',
label: 'Open',
),
],
content: NotificationContent(
payload: {'PDF_Downloader': dir.path, 'file': fileName},
id: 1,
channelKey: "PDF_Downloader",
title: "Certificate Downloaded",
body: "PDF has been downloaded successfully in Download Folder",
),
);
await file.writeAsBytes(response.bodyBytes);
String date = DateTime.now().toString();
setState(() {
verificationDate = date;
});
这是下载 PDF 文件并显示 PDF 已成功下载的通知并提供打开它的选项的示例,因此,我已将有效负载传递给通知
在您的情况下,您必须提供频道密钥,并且必须为您的特定功能创建一个函数,并替换此处输入代码的位置dir.path
我希望它会起作用。