71 lines
2.4 KiB
Dart
71 lines
2.4 KiB
Dart
// * Service for managing local notifications in the app
|
|
// * Handles notification permissions, creation, and management
|
|
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
|
|
// * Notification service class that handles all notification functionality
|
|
class NotificationService {
|
|
// Plugin instance for local notifications
|
|
final notifiactionPlugin = FlutterLocalNotificationsPlugin();
|
|
|
|
// Initialization status flag
|
|
bool _isInitialized = false;
|
|
|
|
// Getter for initialization status
|
|
bool get isInitialized => _isInitialized;
|
|
|
|
// * Initialize the notification service
|
|
// * - Requests notification permissions
|
|
// * - Configures Android-specific settings
|
|
// * - Initializes the notification plugin
|
|
Future<void> initNotification() async {
|
|
// Prevent multiple initializations
|
|
if (_isInitialized) return;
|
|
|
|
// Request permissions for Android notifications
|
|
await notifiactionPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()!.requestNotificationsPermission();
|
|
|
|
// Android-specific initialization settings
|
|
const initSettingsAndroid = AndroidInitializationSettings(
|
|
'@mipmap/ic_launcher', // App icon for notifications
|
|
);
|
|
|
|
// Overall initialization settings
|
|
const initSettings = InitializationSettings(android: initSettingsAndroid);
|
|
|
|
// Initialize plugin
|
|
await notifiactionPlugin.initialize(initSettings);
|
|
_isInitialized = true;
|
|
}
|
|
|
|
// * Create default notification settings
|
|
// * Configures a notification with:
|
|
// * - Low importance and priority
|
|
// * - Ongoing status
|
|
// * - Specific channel for tracking
|
|
NotificationDetails notificationDetails() {
|
|
return const NotificationDetails(
|
|
android: AndroidNotificationDetails(
|
|
"tracking0", // Channel ID
|
|
"tracking ongoing", // Channel name
|
|
importance: Importance.low,
|
|
priority: Priority.low,
|
|
ongoing: true, // Notification persists
|
|
),
|
|
);
|
|
}
|
|
|
|
// * Show a new notification
|
|
// * @param id The notification ID (default: 0)
|
|
// * @param title The notification title
|
|
Future<void> showNotification({int id = 0, String? title}) async {
|
|
return notifiactionPlugin.show(id, title, "", notificationDetails());
|
|
}
|
|
|
|
// * Delete an existing notification
|
|
// * @param id The ID of the notification to delete (default: 0)
|
|
Future<void> deleteNotification({id = 0}) async {
|
|
await notifiactionPlugin.cancel(id);
|
|
}
|
|
}
|