import 'package:awesome_notifications/awesome_notifications.dart'; import 'package:didvan/models/notification_message.dart'; import 'package:didvan/services/app_home_widget/home_widget_repository.dart'; import 'package:didvan/services/app_initalizer.dart'; import 'package:didvan/services/storage/storage.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:get/get.dart'; class NotificationService { static Future initializeNotification() async { await AwesomeNotifications().initialize( null, [ NotificationChannel( channelKey: 'content', channelGroupKey: 'content', channelName: 'content Notification', channelDescription: 'Notification channel', defaultColor: Colors.blueAccent, ledColor: Colors.white, importance: NotificationImportance.Max, channelShowBadge: true, onlyAlertOnce: true, playSound: true, criticalAlerts: true), // NotificationChannel( // channelKey: 'mention', // channelGroupKey: 'mention', // channelName: 'mention Notification', // channelDescription: 'Notification channel', // defaultColor: Colors.blueAccent, // ledColor: Colors.white, // importance: NotificationImportance.Max, // channelShowBadge: true, // onlyAlertOnce: true, // playSound: true, // criticalAlerts: true), ], // channelGroups: [ // NotificationChannelGroup( // channelGroupKey: 'mention', channelGroupName: 'Group mention'), // ], debug: true); await AwesomeNotifications() .isNotificationAllowed() .then((isAllowed) async { if (!isAllowed) { await AwesomeNotifications().requestPermissionToSendNotifications(); } }); await startListeningNotificationEvents(); } @pragma("vm:entry-point") static Future startListeningNotificationEvents() async { await AwesomeNotifications().setListeners( onActionReceivedMethod: _onActionReceivedMethod, onDismissActionReceivedMethod: _onDismissActionReceivedMethod, ); return; } @pragma('vm:entry-point') static Future _onActionReceivedMethod( ReceivedAction receivedAction) async { try { NotificationMessage data = NotificationMessage.fromJson(receivedAction.payload!); HomeWidgetRepository.data = data; print("data: ${HomeWidgetRepository.data}"); await HomeWidgetRepository.decideWhereToGoNotif(); await StorageService.delete( key: 'notification${AppInitializer.createNotificationId(data)}'); } catch (e) { e.printError(); } return; } @pragma('vm:entry-point') static Future _onDismissActionReceivedMethod( ReceivedAction receivedAction) async { try { NotificationMessage data = NotificationMessage.fromJson(receivedAction.payload!); if (data.notificationType!.contains('2')) { await StorageService.delete( key: 'notification${AppInitializer.createNotificationId(data)}'); } } catch (e) { e.printError(); } } // @pragma('vm:entry-point') // static Future _onNotificationCreatedMethod( // ReceivedNotification receivedNotification) async { // } // static Future _onNotificationDisplayedMethod( // ReceivedNotification receivedNotification) async {} static Future showFirebaseNotification(RemoteMessage message) async { try { final data = NotificationMessage.fromJson(message.data); if (data.notificationType!.contains('3')) { HomeWidgetRepository.fetchWidget(); return; } NotificationService.showNotification(data); } catch (e) { e.printError(); } } static Future showNotification(NotificationMessage data) async { if (data.notificationType!.contains('2')) { final String? storedValue = await StorageService.getValue( key: 'notification${AppInitializer.createNotificationId(data)}'); if (storedValue != null) { AwesomeNotifications() .dismiss(AppInitializer.createNotificationId(data)); data.body = '${data.body}\n$storedValue'; } } await AwesomeNotifications().createNotification( content: NotificationContent( id: data.notificationType!.contains('1') ? DateTime.now().millisecondsSinceEpoch ~/ 1000 : AppInitializer.createNotificationId(data), channelKey: // data.notificationType!.contains('1') ? 'content' // : 'mention' , title: data.title, body: data.body, actionType: ActionType.Default, notificationLayout: data.notificationType!.contains('1') ? NotificationLayout.BigPicture : NotificationLayout.Messaging, summary: data.notificationType!.contains('1') ? "خبر جدید" : "منشن شدید", bigPicture: data.image, largeIcon: data.photo, hideLargeIconOnExpand: data.notificationType!.contains('1'), displayOnBackground: true, displayOnForeground: true, wakeUpScreen: true, roundedLargeIcon: data.notificationType!.contains('2'), payload: data.toPayload()), ); if (data.notificationType!.contains('2')) { await StorageService.setValue( key: 'notification${AppInitializer.createNotificationId(data)}', value: data.body); } } }