add notification test
This commit is contained in:
82
covas_mobile/lib/classes/notification_service.dart
Normal file
82
covas_mobile/lib/classes/notification_service.dart
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||||
|
import 'package:timezone/timezone.dart' as tz;
|
||||||
|
import 'package:timezone/data/latest_all.dart' as tz;
|
||||||
|
|
||||||
|
class NotificationService {
|
||||||
|
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
|
||||||
|
FlutterLocalNotificationsPlugin();
|
||||||
|
|
||||||
|
/// Initialisation (à appeler dans main())
|
||||||
|
static Future<void> initialize() async {
|
||||||
|
tz.initializeTimeZones();
|
||||||
|
|
||||||
|
const AndroidInitializationSettings androidInitSettings =
|
||||||
|
AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||||
|
|
||||||
|
const InitializationSettings initSettings = InitializationSettings(
|
||||||
|
android: androidInitSettings,
|
||||||
|
iOS: DarwinInitializationSettings(),
|
||||||
|
);
|
||||||
|
|
||||||
|
await _notificationsPlugin.initialize(initSettings);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Planifie une notification 1h avant l’évènement
|
||||||
|
static Future<void> scheduleEventNotification({
|
||||||
|
required String eventId,
|
||||||
|
required String title,
|
||||||
|
required String body,
|
||||||
|
required DateTime eventDate,
|
||||||
|
}) async {
|
||||||
|
final scheduledDate = eventDate.subtract(const Duration(hours: 1));
|
||||||
|
|
||||||
|
if (scheduledDate.isBefore(DateTime.now())) {
|
||||||
|
// Trop tard pour notifier
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await _notificationsPlugin.zonedSchedule(
|
||||||
|
eventId.hashCode, // identifiant unique pour l’évènement
|
||||||
|
title,
|
||||||
|
body,
|
||||||
|
tz.TZDateTime.from(scheduledDate, tz.local),
|
||||||
|
const NotificationDetails(
|
||||||
|
android: AndroidNotificationDetails(
|
||||||
|
'events_channel', // id du canal
|
||||||
|
'Évènements', // nom du canal
|
||||||
|
channelDescription: 'Notifications des évènements favoris',
|
||||||
|
importance: Importance.high,
|
||||||
|
priority: Priority.high,
|
||||||
|
),
|
||||||
|
iOS: DarwinNotificationDetails(),
|
||||||
|
),
|
||||||
|
androidScheduleMode: AndroidScheduleMode.inexactAllowWhileIdle,
|
||||||
|
uiLocalNotificationDateInterpretation:
|
||||||
|
UILocalNotificationDateInterpretation.absoluteTime,
|
||||||
|
matchDateTimeComponents: DateTimeComponents.dateAndTime,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
static Future<void> showTestNotification() async {
|
||||||
|
await _notificationsPlugin.show(
|
||||||
|
0, // id arbitraire
|
||||||
|
"Test Notification",
|
||||||
|
"Ceci est une notification de debug",
|
||||||
|
const NotificationDetails(
|
||||||
|
android: AndroidNotificationDetails(
|
||||||
|
'debug_channel',
|
||||||
|
'Debug Notifications',
|
||||||
|
channelDescription: 'Notifications de test pour debug',
|
||||||
|
importance: Importance.max,
|
||||||
|
priority: Priority.high,
|
||||||
|
),
|
||||||
|
iOS: DarwinNotificationDetails(),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Annule une notification planifiée
|
||||||
|
static Future<void> cancel(String eventId) async {
|
||||||
|
await _notificationsPlugin.cancel(eventId.hashCode);
|
||||||
|
}
|
||||||
|
}
|
@@ -5,10 +5,12 @@ import 'package:provider/provider.dart';
|
|||||||
import 'pages/LoginDemo.dart';
|
import 'pages/LoginDemo.dart';
|
||||||
import 'locale_provider.dart'; // <-- à adapter selon ton arborescence
|
import 'locale_provider.dart'; // <-- à adapter selon ton arborescence
|
||||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||||
|
import 'classes/notification_service.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
await MobileAds.instance.initialize();
|
await MobileAds.instance.initialize();
|
||||||
|
await NotificationService.initialize();
|
||||||
|
|
||||||
runApp(
|
runApp(
|
||||||
ChangeNotifierProvider(
|
ChangeNotifierProvider(
|
||||||
|
@@ -23,6 +23,7 @@ import 'package:flutter_localizations/flutter_localizations.dart';
|
|||||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import '../locale_provider.dart'; // Créé plus loin
|
import '../locale_provider.dart'; // Créé plus loin
|
||||||
|
import '../classes/notification_service.dart';
|
||||||
|
|
||||||
void main() async {
|
void main() async {
|
||||||
WidgetsFlutterBinding.ensureInitialized();
|
WidgetsFlutterBinding.ensureInitialized();
|
||||||
@@ -911,6 +912,20 @@ class _MyHomePageState extends State<ListItemMenu> {
|
|||||||
post.interested = result["interested"];
|
post.interested = result["interested"];
|
||||||
post.interestedCount = result["interested_count"];
|
post.interestedCount = result["interested_count"];
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (result["interested"] == true) {
|
||||||
|
NotificationService.scheduleEventNotification(
|
||||||
|
eventId: post.id!,
|
||||||
|
title: "Rappel évènement",
|
||||||
|
body:
|
||||||
|
"Ton évènement '${post.name}' commence dans 1 heure !",
|
||||||
|
eventDate: DateTime.parse(post.startDate!),
|
||||||
|
);
|
||||||
|
|
||||||
|
NotificationService.showTestNotification();
|
||||||
|
} else {
|
||||||
|
NotificationService.cancel(post.id!);
|
||||||
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
ScaffoldMessenger.of(context).showSnackBar(
|
||||||
SnackBar(
|
SnackBar(
|
||||||
|
@@ -6,6 +6,7 @@ import FlutterMacOS
|
|||||||
import Foundation
|
import Foundation
|
||||||
|
|
||||||
import file_selector_macos
|
import file_selector_macos
|
||||||
|
import flutter_local_notifications
|
||||||
import geolocator_apple
|
import geolocator_apple
|
||||||
import path_provider_foundation
|
import path_provider_foundation
|
||||||
import shared_preferences_foundation
|
import shared_preferences_foundation
|
||||||
@@ -14,6 +15,7 @@ import webview_flutter_wkwebview
|
|||||||
|
|
||||||
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
|
||||||
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
|
FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin"))
|
||||||
|
FlutterLocalNotificationsPlugin.register(with: registry.registrar(forPlugin: "FlutterLocalNotificationsPlugin"))
|
||||||
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
|
GeolocatorPlugin.register(with: registry.registrar(forPlugin: "GeolocatorPlugin"))
|
||||||
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
PathProviderPlugin.register(with: registry.registrar(forPlugin: "PathProviderPlugin"))
|
||||||
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
|
||||||
|
@@ -145,6 +145,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "0.1.0"
|
version: "0.1.0"
|
||||||
|
dbus:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: dbus
|
||||||
|
sha256: "79e0c23480ff85dc68de79e2cd6334add97e48f7f4865d17686dd6ea81a47e8c"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.7.11"
|
||||||
dio:
|
dio:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
@@ -270,6 +278,30 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "4.0.0"
|
version: "4.0.0"
|
||||||
|
flutter_local_notifications:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications
|
||||||
|
sha256: "674173fd3c9eda9d4c8528da2ce0ea69f161577495a9cc835a2a4ecd7eadeb35"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "17.2.4"
|
||||||
|
flutter_local_notifications_linux:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications_linux
|
||||||
|
sha256: c49bd06165cad9beeb79090b18cd1eb0296f4bf4b23b84426e37dd7c027fc3af
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "4.0.1"
|
||||||
|
flutter_local_notifications_platform_interface:
|
||||||
|
dependency: transitive
|
||||||
|
description:
|
||||||
|
name: flutter_local_notifications_platform_interface
|
||||||
|
sha256: "85f8d07fe708c1bdcf45037f2c0109753b26ae077e9d9e899d55971711a4ea66"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "7.2.0"
|
||||||
flutter_localizations:
|
flutter_localizations:
|
||||||
dependency: "direct main"
|
dependency: "direct main"
|
||||||
description: flutter
|
description: flutter
|
||||||
@@ -850,6 +882,14 @@ packages:
|
|||||||
url: "https://pub.dev"
|
url: "https://pub.dev"
|
||||||
source: hosted
|
source: hosted
|
||||||
version: "3.0.1"
|
version: "3.0.1"
|
||||||
|
timezone:
|
||||||
|
dependency: "direct main"
|
||||||
|
description:
|
||||||
|
name: timezone
|
||||||
|
sha256: "2236ec079a174ce07434e89fcd3fcda430025eb7692244139a9cf54fdcf1fc7d"
|
||||||
|
url: "https://pub.dev"
|
||||||
|
source: hosted
|
||||||
|
version: "0.9.4"
|
||||||
typed_data:
|
typed_data:
|
||||||
dependency: transitive
|
dependency: transitive
|
||||||
description:
|
description:
|
||||||
|
@@ -36,6 +36,8 @@ dependencies:
|
|||||||
|
|
||||||
# The following adds the Cupertino Icons font to your application.
|
# The following adds the Cupertino Icons font to your application.
|
||||||
# Use with the CupertinoIcons class for iOS style icons.
|
# Use with the CupertinoIcons class for iOS style icons.
|
||||||
|
flutter_local_notifications: ^17.2.0
|
||||||
|
timezone: ^0.9.4
|
||||||
cupertino_icons: ^1.0.2
|
cupertino_icons: ^1.0.2
|
||||||
http: ^1.2.1
|
http: ^1.2.1
|
||||||
shared_preferences: ^2.2.3
|
shared_preferences: ^2.2.3
|
||||||
|
Reference in New Issue
Block a user