如何在一段时间后取消计划以前计划的本地通知?



我正在使用flutter_local_notifications库每 1 小时安排一次本地通知。它按预期工作,但现在,我需要一种方法来启动/停止通知计划(例如,按下按钮(。

我在文档中找不到有关取消预定通知请求的任何内容。

取消/删除通知

// cancel the notification with id value of zero
await flutterLocalNotificationsPlugin.cancel(0);
// 0 is your notification id

取消/删除所有通知

await flutterLocalNotificationsPlugin.cancelAll();

是的,这可以取消当前和未来的通知。只需确保正确的通知 ID。

在文档中给出 取消/删除通知

await flutterLocalNotificationsPlugin.cancel(0);

我会做这样的事情:

final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Below will give you any unpresented/scheduled notifications
final List<PendingNotificationRequest> pendingNotificationRequests =
await _flutterLocalNotificationsPlugin.pendingNotificationRequests();
for (var _pendingRequest in pendingNotificationRequests) {
_flutterLocalNotificationsPlugin.cancel(_pendingRequest.id);
} 

在这里,_pendingRequest.id将为您提供取消特定通知请求所需的通知 ID。 这是在安卓中测试过的。将很快在iOS上更新状态。

await flutterLocalNotificationsPlugin.cancelAll(); //cancel future note
await flutterLocalNotificationsPlugin.pendingNotificationRequests(); //restart note

您的通知将再次显示。

如果要取消所有通知,则必须使用以下方法执行此操作:

await flutterLocalNotificationsPlugin.cancelAll();

此外,如果要取消特定通知,可以使用通知的特定 ID 执行此操作:

await flutterLocalNotificationsPlugin.cancel(0);  // 0 is your notification id

此通知 ID 来自何处?

当您声明要在屏幕上显示通知时,您必须添加一些数据,如下所示:

await _flutterLocalNotificationsPlugin.show(
0, // this is the notification id
message.notification.title,
message.notification.body,
notificationDetails,
payload: jsonEncode(message.data),
);

在这里,我正在使用颤振本地通知插件来显示通知!!

希望这能解决您的问题。

最新更新