Rain/lib/app/utils/notification.dart

59 lines
1.6 KiB
Dart
Raw Normal View History

2023-06-17 20:57:57 +03:00
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
2023-09-08 18:55:26 +03:00
import 'package:rain/app/controller/controller.dart';
2023-06-17 20:57:57 +03:00
import 'package:rain/main.dart';
import 'package:timezone/timezone.dart' as tz;
class NotificationShow {
2025-05-28 17:42:15 +03:00
static const String _channelId = 'Rain';
static const String _channelName = 'DARK NIGHT';
Future<void> showNotification(
2023-06-17 20:57:57 +03:00
int id,
String title,
String body,
DateTime date,
2023-09-07 16:49:26 +03:00
String icon,
2023-06-17 20:57:57 +03:00
) async {
2025-05-28 17:42:15 +03:00
try {
final imagePath = await _getLocalImagePath(icon);
final notificationDetails = await _buildNotificationDetails(imagePath);
final scheduledTime = _getScheduledTime(date);
2023-09-08 18:55:26 +03:00
2025-05-28 17:42:15 +03:00
await flutterLocalNotificationsPlugin.zonedSchedule(
id,
title,
body,
scheduledTime,
notificationDetails,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
payload: imagePath,
);
} catch (e) {
print('Error showing notification: $e');
}
}
Future<String> _getLocalImagePath(String icon) async {
return await WeatherController().getLocalImagePath(icon);
}
2023-06-17 20:57:57 +03:00
2025-05-28 17:42:15 +03:00
Future<NotificationDetails> _buildNotificationDetails(
String imagePath,
) async {
final androidNotificationDetails = AndroidNotificationDetails(
_channelId,
_channelName,
priority: Priority.high,
importance: Importance.max,
playSound: false,
enableVibration: false,
largeIcon: FilePathAndroidBitmap(imagePath),
2023-06-17 20:57:57 +03:00
);
2025-05-28 17:42:15 +03:00
return NotificationDetails(android: androidNotificationDetails);
}
tz.TZDateTime _getScheduledTime(DateTime date) {
return tz.TZDateTime.from(date, tz.local);
2023-06-17 20:57:57 +03:00
}
}