Skip to content

Commit

Permalink
Notification working (#448)
Browse files Browse the repository at this point in the history
Co-authored-by: Foucauld Bellanger <[email protected]>
  • Loading branch information
foucblg and foucblg authored Jan 2, 2025
1 parent 0200665 commit 7412931
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 102 deletions.
5 changes: 0 additions & 5 deletions lib/cinema/ui/pages/detail_page/detail_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,12 +310,7 @@ class DetailPage extends HookConsumerWidget {
content: 'La séance '
'${session.name}'
' commence dans 10 minutes',
context: session.id,
isVisible: true,
title: '🎬 Cinéma',
deliveryDateTime: session.start.subtract(
const Duration(minutes: 10),
),
),
);
displayToast(
Expand Down
3 changes: 1 addition & 2 deletions lib/ph/notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import 'package:tuple/tuple.dart';

final Map<String, Tuple2<String, List<StateNotifierProvider>>> phProviders = {
"ph": Tuple2(
// L'actionTable de la notification est "session"
PhRouter.root, // La page principale du module ciné
PhRouter.root, // La page principale du module ph
[phListProvider], // Le provider de la liste des sessions
),
};
19 changes: 1 addition & 18 deletions lib/service/class/message.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,21 @@
import 'package:myecl/tools/functions.dart';

class Message {
late final String? title;
late final String? content;
late final String? actionModule;
late final String? actionTable;
late final bool isVisible;
late final String context;
late final DateTime? deliveryDateTime;

Message({
required this.title,
required this.content,
required this.actionModule,
required this.actionTable,
required this.context,
required this.isVisible,
this.deliveryDateTime,
});

Message.fromJson(Map<String, dynamic> json) {
title = json['title'];
content = json['content'];
actionModule = json['action_module'];
actionTable = json['action_table'];
context = json['context'];
isVisible = json['is_visible'];
deliveryDateTime = json['delivery_datetime'] != null
? processDateFromAPI(json['delivery_datetime'])
: null;
}

Map<String, dynamic> toJson() {
Expand All @@ -37,15 +24,11 @@ class Message {
data['content'] = content;
data['action_module'] = actionModule;
data['action_table'] = actionTable;
data['context'] = context;
data['is_visible'] = isVisible;
data['delivery_datetime'] =
(deliveryDateTime != null) ? processDateToAPI(deliveryDateTime!) : null;
return data;
}

@override
String toString() {
return 'Message{title: $title, content: $content, actionModule: $actionModule, actionTable: $actionTable, context: $context, isVisible: $isVisible}';
return 'Message{title: $title, content: $content, actionModule: $actionModule, actionTable: $actionTable}';
}
}
45 changes: 9 additions & 36 deletions lib/service/local_notification_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import 'package:qlevar_router/qlevar_router.dart';
import 'package:rxdart/subjects.dart';
import 'package:timezone/timezone.dart' as tz;
import 'package:timezone/data/latest.dart' as tz;
import 'package:uuid/uuid.dart';

class LocalNotificationService {
LocalNotificationService() {
Expand Down Expand Up @@ -65,42 +66,14 @@ class LocalNotificationService {

Future showNotification(message_class.Message message) async {
final notificationDetails = getNotificationDetails();
if (message.deliveryDateTime == null) {
_localNotificationService.show(
generateIntFromString(message.context),
message.title,
message.content,
notificationDetails,
payload: json.encode(message.toJson()),
);
return;
}
final dateToDisplay = tz.TZDateTime.from(
message.deliveryDateTime!,
tz.local,
_localNotificationService.show(
generateIntFromString(const Uuid().toString()),
message.title,
message.content,
notificationDetails,
payload: json.encode(message.toJson()),
);
final now = tz.TZDateTime.now(tz.local);
if (dateToDisplay.isAfter(now)) {
_localNotificationService.zonedSchedule(
generateIntFromString(message.context),
message.title,
message.content,
dateToDisplay,
notificationDetails,
androidScheduleMode: AndroidScheduleMode.exactAllowWhileIdle,
uiLocalNotificationDateInterpretation:
UILocalNotificationDateInterpretation.absoluteTime,
payload: json.encode(message.toJson()),
);
} else {
_localNotificationService.show(
generateIntFromString(message.context),
message.title,
message.content,
notificationDetails,
payload: json.encode(message.toJson()),
);
}
return;
}

Future<void> showPeriodicNotification(
Expand Down Expand Up @@ -283,7 +256,7 @@ void onDidReceiveBackgroundNotificationResponse(
final message = message_class.Message.fromJson(
jsonDecode(utf8.decode(response.payload!.runes.toList())),
);
if (message.actionModule != null && message.actionTable != null) {
if (message.actionModule != null) {
final provider = providers[message.actionModule];
if (provider == null) {
return;
Expand Down
50 changes: 9 additions & 41 deletions lib/service/tools/setup.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:myecl/service/class/message.dart' as message_class;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:myecl/service/local_notification_service.dart';
import 'package:myecl/service/providers/firebase_token_expiration_provider.dart';
import 'package:myecl/service/providers/firebase_token_provider.dart';
import 'package:myecl/service/providers/messages_provider.dart';
import 'package:myecl/service/providers/topic_provider.dart';
import 'package:myecl/service/repositories/notification_repository.dart';
import 'package:myecl/tools/logs/log.dart';
import 'package:myecl/tools/repository/repository.dart';
import 'package:myecl/tools/token_expire_wrapper.dart';
import 'package:myecl/user/providers/user_provider.dart';

void setUpNotification(WidgetRef ref) {
Expand Down Expand Up @@ -50,40 +49,19 @@ void setUpNotification(WidgetRef ref) {
}
});

void handleMessages() async {
tokenExpireWrapper(ref, () async {
final messages = await messageNotifier.getMessages();
messages.maybeWhen(
data: (messageList) async {
for (final message in messageList) {
Repository.logger.logNotification(message);
final actionModule = message.actionModule;
final actionTable = message.actionTable;
if (!message.isVisible &&
actionModule != null &&
actionTable != null) {
localNotificationService.handleAction(actionModule, actionTable);
} else {
localNotificationService.showNotification(message);
}
}
},
orElse: () {},
);
});
}

FirebaseMessaging.onMessage.listen((RemoteMessage message) {
message_class.Message messages =
message_class.Message.fromJson(message.data);
Repository.logger
.writeLog(Log(message: "GOT trigger onMessage", level: LogLevel.error));
handleMessages();
});

FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
Repository.logger.writeLog(
Log(message: "GOT trigger onMessageOpenedApp", level: LogLevel.error),
message_class.Message me = message_class.Message(
title: message.notification?.title ?? "No title",
content: message.notification?.body ?? "No body",
actionModule: messages.actionModule,
actionTable: messages.actionTable,
);
handleMessages();
localNotificationService.showNotification(me);
});
}

Expand All @@ -96,14 +74,4 @@ Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final LocalNotificationService localNotificationService =
LocalNotificationService();
localNotificationService.init();
final firebaseToken = await FirebaseMessaging.instance
.getToken(vapidKey: "")
.then((value) => value.toString());
NotificationRepository notificationRepository = NotificationRepository();
final messages = await notificationRepository.getMessages(firebaseToken);
for (final message in messages) {
if (message.isVisible) {
localNotificationService.showNotification(message);
}
}
}
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ dependencies:
url_launcher: ^6.2.5
pdfx: ^2.6.0
file_saver: ^0.2.12
uuid: ^4.4.2

dev_dependencies:
flutter_launcher_icons: ^0.14.2
Expand Down

0 comments on commit 7412931

Please sign in to comment.