有没有一种方法可以在不使用远程服务器的情况下实现以下功能:在我的应用程序中,我可以安排提醒。这些在应用程序打开或在后台运行时触发,但不会在应用程序终止时触发。当应用程序终止或以某种方式将它们安排到某种操作系统级别时,是否有任何库可以触发计划通知?或者我需要通过像 firebase 这样的远程服务器来触发它们吗?
目前我正在使用 flutter_local_notifications 包。我的通知服务如下所示:
import 'package:timezone/timezone.dart' as tz;
import '../db_models/reminder_model.dart';
class NotificationService {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
// Initialize the notification settings
Future<void> initNotifications() async {
final InitializationSettings initializationSettings = InitializationSettings(
android: AndroidInitializationSettings('@mipmap/ic_launcher'),
iOS: DarwinInitializationSettings(),
// Add other platform-specific settings if needed
);
await _flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: _onDidReceiveNotificationResponse,
onDidReceiveBackgroundNotificationResponse: _onDidReceiveBackgroundNotificationResponse,
);
}
// Schedule a notification
Future<void> scheduleNotification(Reminder reminder) async {
// Convert triggerTime to a DateTime object
// Extract the current date
final currentDate = DateTime.now();
// Extract the hour and minute from the triggerTime
final timeParts = reminder.getTriggerTime().split(':');
final hour = int.parse(timeParts[0]);
final minute = int.parse(timeParts[1]);
// Combine them into a DateTime object
final scheduledTime = DateTime(currentDate.year, currentDate.month, currentDate.day, hour, minute);
//final DateTime scheduledTime = DateTime.parse(reminder.getTriggerTime());
final tz.TZDateTime tzScheduledTime = tz.TZDateTime.from(scheduledTime, tz.local);
print('TriggerTime: $tzScheduledTime');
// Define the notification details
var androidDetails = AndroidNotificationDetails(
'channel_id',
'channel_name',
channelDescription: 'channel_description',
importance: Importance.max,
priority: Priority.high,
);
var iosDetails = DarwinNotificationDetails();
var platformDetails = NotificationDetails(android: androidDetails, iOS: iosDetails);
// Schedule the notification
await _flutterLocalNotificationsPlugin.zonedSchedule(
reminder.getId(),
'Water Reminder',
'Time to drink water!',
tzScheduledTime,
platformDetails,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime,
matchDateTimeComponents: DateTimeComponents.time, // To repeat daily at the same time
payload: "hey boss!!!!"
);
var pendingNotifications = await _flutterLocalNotificationsPlugin.pendingNotificationRequests();
for (var notification in pendingNotifications) {
print('Notification: id=${notification.id}, title=${notification.title}, body=${notification.body}, payload=${notification.payload}');
}
}
Future<void> triggerNotificationNow(int notificationId) async {
// Retrieve the details of the scheduled notification by its ID
// This step depends on how you store your scheduled notifications
var androidDetails = AndroidNotificationDetails(
'channel_id',
'channel_name',
channelDescription: 'channel_description',
importance: Importance.max,
priority: Priority.high,
);
var iosDetails = DarwinNotificationDetails();
var platformDetails = NotificationDetails(android: androidDetails, iOS: iosDetails);
// Schedule the notification to trigger immediately
await _flutterLocalNotificationsPlugin.zonedSchedule(
notificationId,
"Test",
"Wabec lol haha",
tz.TZDateTime.now(tz.local).add(Duration(seconds: 5)),
platformDetails, uiLocalNotificationDateInterpretation: UILocalNotificationDateInterpretation.absoluteTime
);
}
// Handle notification response when the app is running
void _onDidReceiveNotificationResponse(NotificationResponse response) {
// Handle the notification response
}
// Handle notification response when the app is in the background
}
void _onDidReceiveBackgroundNotificationResponse(
NotificationResponse response) {
print("Notification clicked with payload: ${response.payload}");
// Handle the background notification response
}
当我安排通知时,它会在应用程序在前台/后台运行时触发,但不会在应用程序终止时触发。
我在 Android API 上进行了测试:28、29 和 34(我知道从某个 API 开始,您需要特殊权限才能进行精确调度)
您需要使用 Firebase 推送通知或 One Signal,因为 ios 不支持后台获取或此类内容。之后,您可以将传入的通知(来自该服务)重定向到您的本地解决方案。