import 'package:awesome_notifications/awesome_notifications.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/material.dart'; import 'package:intl/intl.dart'; import '../../../models/notification_message.dart'; import '../../app_home_widget/home_widget_repository.dart'; import '../../network/request.dart'; import '../../network/request_helper.dart'; import 'awsome_notification_controller.dart'; class AwsomeNotificationHandler { alarm() async { late ReceivedAction? initialAction; AwesomeNotifications().initialize( // set the icon to null if you want to use the default app icon null, [ NotificationChannel( channelKey: 'alerts', channelName: 'Alerts', channelDescription: 'Notification tests as alerts', playSound: true, onlyAlertOnce: true, groupAlertBehavior: GroupAlertBehavior.Children, importance: NotificationImportance.High, defaultPrivacy: NotificationPrivacy.Public, defaultColor: const Color(0xFF007EA7), criticalAlerts: true, ledColor: Colors.white) ], // Channel groups are only visual and are not required // channelGroups: [ // NotificationChannelGroup( // channelGroupKey: 'basic_channel_group', // channelGroupName: 'Basic group') // ], debug: false); initialAction = await AwesomeNotifications() .getInitialNotificationAction(removeFromActionEvents: false); AwesomeNotifications().setListeners( onActionReceivedMethod: NotificationController.onActionReceivedMethod, onNotificationCreatedMethod: NotificationController.onNotificationCreatedMethod, onNotificationDisplayedMethod: NotificationController.onNotificationDisplayedMethod, onDismissActionReceivedMethod: NotificationController.onDismissActionReceivedMethod); AwesomeNotifications().isNotificationAllowed().then((isAllowed) { //It would be more appropriate if you can show your own dialog //to the user before requesting the notifications permissons. if (!isAllowed) { AwesomeNotifications().requestPermissionToSendNotifications( permissions: [ NotificationPermission.Alert, NotificationPermission.Sound, NotificationPermission.Badge, NotificationPermission.Vibration, NotificationPermission.Light, NotificationPermission.FullScreenIntent, ], ); } }); } show(RemoteMessage message) async { NotificationMessage notificationMessage = NotificationMessage.fromJson(message.data); switch (notificationMessage.notificationType.toString()) { case "1": await showNotificationTypeNews(notificationMessage); break; case "2": await showNotificationTypeMessage(notificationMessage); break; case "3": await HomeWidgetRepository().fetchWidget(); break; case "4": await showNotificationTypeEmoji(notificationMessage); break; } } showNotificationTypeNews(NotificationMessage message) async { DateTime time = await _getTime(); AwesomeNotifications().createNotification( content: NotificationContent( id: DateTime.now().millisecondsSinceEpoch ~/ 1000, channelKey: 'alerts', actionType: ActionType.Default, title: "\u200f ${message.title} \u200f", // body: message.body.toString(), notificationLayout: NotificationLayout.Default, largeIcon: message.image.toString(), payload: message.toPayload(), color: const Color(0xFF007EA7)), schedule: time.year != null ? null : NotificationCalendar( hour: time.hour, minute: time.minute, )); } showNotificationTypeMessage(NotificationMessage message) async { DateTime time = await _getTime(); AwesomeNotifications().createNotification( content: NotificationContent( id: DateTime.now().millisecondsSinceEpoch ~/ 1000, channelKey: 'alerts', actionType: ActionType.Default, body: "${message.title.toString()} ${message.body.toString()}", largeIcon: message.photo.toString(), roundedLargeIcon: true, bigPicture: message.image.toString(), payload: message.toPayload(), notificationLayout: NotificationLayout.BigPicture, color: const Color(0xFF007EA7)), schedule: time.year != null ? null : NotificationCalendar( hour: time.hour, minute: time.minute, ) // actionButtons: [ // NotificationActionButton( // key: 'REPLY', // label: 'جواب دادن', // requireInputText: true, // actionType: ActionType.SilentAction), // NotificationActionButton( // key: 'DISMISS', // label: 'خوانده شده', // actionType: ActionType.DismissAction, // isDangerousOption: true) // ] ); } showNotificationTypeEmoji(NotificationMessage message) async { AwesomeNotifications().createNotification( content: NotificationContent( id: DateTime.now().millisecondsSinceEpoch ~/ 1000, channelKey: 'alerts', title: 'Emojis are awes' 'ome too! ${Emojis.animals_lady_beetle}${Emojis.activites_balloon}${Emojis.emotion_red_heart}', body: 'Simple body with a bunch of Emojis! ${Emojis.transport_police_car} ${Emojis.animals_dog} ${Emojis.flag_UnitedStates} ${Emojis.person_baby}', largeIcon: 'https://cdn.britannica.com/72/232772-050-4E3D86CC/mind-blown-emoji-head-exploding-emoticon.jpg', notificationLayout: NotificationLayout.BigPicture, payload: { 'title': 'Notification Title', 'body': 'Notification Body', 'image': 'path/to/smallImage.png', // Path to small image 'largeImage': 'path/to/largeImage.png', // Path to large image })); } Future _getTime() async { final service = RequestService( RequestHelper.notificationTime(), ); await service.httpGet(); if (service.isSuccess) { final time = service.data('time'); DateTime result = DateTime.now(); if (time.toString().isNotEmpty) { DateFormat format = DateFormat("HH:mm"); DateTime dateTime = format.parse(time); result = DateTime.now() .copyWith(hour: dateTime.hour, minute: dateTime.minute); } return result; } else { return DateTime.now(); } } showNotificationScheduled(NotificationMessage message) async { DateTime time = await _getTime(); AwesomeNotifications().createNotification( content: NotificationContent( id: DateTime.now().millisecondsSinceEpoch ~/ 1000, channelKey: 'alerts', title: 'Emojis are awes' 'ome too! ${Emojis.animals_lady_beetle}${Emojis.activites_balloon}${Emojis.emotion_red_heart}', body: 'Simple body with a bunch of Emojis! ${Emojis.transport_police_car} ${Emojis.animals_dog} ${Emojis.flag_UnitedStates} ${Emojis.person_baby}', largeIcon: 'https://cdn.britannica.com/72/232772-050-4E3D86CC/mind-blown-emoji-head-exploding-emoticon.jpg', notificationLayout: NotificationLayout.BigPicture, wakeUpScreen: true, category: NotificationCategory.Alarm, payload: { 'title': 'Notification Title', 'body': 'Notification Body', 'image': 'path/to/smallImage.png', // Path to small image 'largeImage': 'path/to/largeImage.png', // Path to large image }), schedule: NotificationCalendar( hour: time.hour, minute: time.minute, // timezone: await AwesomeNotifications().getLocalTimeZoneIdentifier() )); } }