306 lines
12 KiB
Dart
306 lines
12 KiB
Dart
import 'dart:io';
|
|
|
|
import 'package:awesome_notifications/awesome_notifications.dart';
|
|
import 'package:didvan/services/storage/storage.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 {
|
|
static ReceivedAction? initialAction;
|
|
|
|
alarm() async {
|
|
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: true);
|
|
|
|
startListeningNotificationEvents();
|
|
// Get initial notification action is optional
|
|
initialAction = await AwesomeNotifications()
|
|
.getInitialNotificationAction(removeFromActionEvents: false);
|
|
|
|
if (initialAction?.payload != null) {
|
|
NotificationMessage data =
|
|
NotificationMessage.fromJson(initialAction!.payload!);
|
|
|
|
HomeWidgetRepository.data = data;
|
|
}
|
|
// Your code goes here
|
|
|
|
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,
|
|
],
|
|
);
|
|
}
|
|
});
|
|
}
|
|
|
|
@pragma("vm:entry-point")
|
|
static Future<void> startListeningNotificationEvents() async {
|
|
await AwesomeNotifications().setListeners(
|
|
onActionReceivedMethod: NotificationController.onActionReceivedMethod,
|
|
onNotificationCreatedMethod:
|
|
NotificationController.onNotificationCreatedMethod,
|
|
onNotificationDisplayedMethod:
|
|
NotificationController.onNotificationDisplayedMethod,
|
|
onDismissActionReceivedMethod:
|
|
NotificationController.onDismissActionReceivedMethod);
|
|
return;
|
|
}
|
|
|
|
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();
|
|
if (time == null) {
|
|
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.BigPicture,
|
|
largeIcon: message.image.toString(),
|
|
bigPicture: message.image.toString(),
|
|
category: NotificationCategory.Social,
|
|
wakeUpScreen: true,
|
|
hideLargeIconOnExpand: true,
|
|
payload: message.toPayload(),
|
|
color: const Color(0xFF007EA7)),
|
|
);
|
|
} else {
|
|
AwesomeNotifications().createNotification(
|
|
content: NotificationContent(
|
|
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
channelKey: 'alerts',
|
|
actionType: ActionType.Default,
|
|
title: "\u200f ${message.title} \u200f",
|
|
// body: message.body.toString(),
|
|
notificationLayout: Platform.isAndroid
|
|
? NotificationLayout.Default
|
|
: NotificationLayout.BigPicture,
|
|
largeIcon: message.image.toString(),
|
|
bigPicture: Platform.isAndroid ? null : message.image.toString(),
|
|
payload: message.toPayload(),
|
|
color: const Color(0xFF007EA7)),
|
|
schedule: NotificationCalendar(
|
|
hour: time.hour,
|
|
minute: time.minute,
|
|
));
|
|
}
|
|
}
|
|
|
|
showNotificationTypeMessage(NotificationMessage message) async {
|
|
DateTime? time = await _getTime();
|
|
|
|
if (time == null) {
|
|
AwesomeNotifications().createNotification(
|
|
content: NotificationContent(
|
|
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
channelKey: 'alerts',
|
|
actionType: ActionType.Default,
|
|
body:
|
|
"<b>${message.title.toString()}</b> ${message.body.toString()}",
|
|
largeIcon: message.photo.toString(),
|
|
roundedLargeIcon: true,
|
|
bigPicture: message.image.toString(),
|
|
payload: message.toPayload(),
|
|
notificationLayout: NotificationLayout.BigPicture,
|
|
color: const Color(0xFF007EA7)),
|
|
);
|
|
} else {
|
|
AwesomeNotifications().createNotification(
|
|
content: NotificationContent(
|
|
id: DateTime.now().millisecondsSinceEpoch ~/ 1000,
|
|
channelKey: 'alerts',
|
|
actionType: ActionType.Default,
|
|
body:
|
|
"<b>${message.title.toString()}</b> ${message.body.toString()}",
|
|
largeIcon: Platform.isAndroid
|
|
? message.photo.toString()
|
|
: message.image.toString(),
|
|
roundedLargeIcon: true,
|
|
bigPicture: message.image.toString(),
|
|
payload: message.toPayload(),
|
|
notificationLayout: NotificationLayout.BigPicture,
|
|
color: const Color(0xFF007EA7)),
|
|
schedule: 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.Default,
|
|
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<DateTime?> _getTime() async {
|
|
final token = await StorageService.getValue(key: 'token');
|
|
final service = RequestService(RequestHelper.notificationTime(),
|
|
useAutherization: false,
|
|
requestHeaders: {
|
|
"accept": "*/*",
|
|
"Content-Type": "application/json; charset=UTF-8",
|
|
'Authorization': 'Bearer $token'
|
|
});
|
|
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;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
showNotificationScheduled(NotificationMessage message) async {
|
|
DateTime? time = await _getTime();
|
|
if (time == null) {
|
|
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
|
|
}),
|
|
);
|
|
} else {
|
|
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()
|
|
));
|
|
}
|
|
}
|
|
}
|