didvan-app/lib/services/notification/awsome/awsome_notification_control...

118 lines
4.2 KiB
Dart

import 'package:awesome_notifications/awesome_notifications.dart';
import 'package:didvan/main.dart';
import 'package:url_launcher/url_launcher_string.dart';
import '../../../models/notification_message.dart';
import '../../../models/requests/infography.dart';
import '../../../models/requests/news.dart';
import '../../../models/requests/radar.dart';
import '../../../routes/routes.dart';
class NotificationController {
/// Use this method to detect when a new notification or a schedule is created
@pragma("vm:entry-point")
static Future<void> onNotificationCreatedMethod(
ReceivedNotification receivedNotification) async {
// Your code goes here
// print("onNotificationCreatedMethod--------------------------------------------------------------");
}
/// Use this method to detect every time that a new notification is displayed
@pragma("vm:entry-point")
static Future<void> onNotificationDisplayedMethod(
ReceivedNotification receivedNotification) async {
// Your code goes here
// print("onNotificationDisplayedMethod--------------------------------------------------------------");
}
/// Use this method to detect if the user dismissed a notification
@pragma("vm:entry-point")
static Future<void> onDismissActionReceivedMethod(
ReceivedAction receivedAction) async {
// Your code goes here
// print("onDismissActionReceivedMethod--------------------------------------------------------------");
}
/// Use this method to detect when the user taps on a notification or action button
@pragma("vm:entry-point")
static Future<void> onActionReceivedMethod(
ReceivedAction receivedAction) async {
NotificationMessage data =
NotificationMessage.fromJson(receivedAction.payload!);
// Your code goes here
// Navigate into pages, avoiding to open the notification details page over another details page already opened
String route = "";
dynamic args;
bool openComments = data.notificationType.toString() == "2";
if (data.link.toString().isEmpty) {
switch (data.type!) {
case "infography":
route = Routes.infography;
args = {
// 'onMarkChanged': (id, value) =>
// markChangeHandler(data.type, id, value),
'id': int.parse(data.id.toString()),
'args': const InfographyRequestArgs(page: 0),
'hasUnmarkConfirmation': false,
'goToComment': openComments
};
break;
case "news":
route = Routes.newsDetails;
args = {
// 'onMarkChanged': (id, value) =>
// markChangeHandler(data.type, id, value),
'id': int.parse(data.id.toString()),
'args': const NewsRequestArgs(page: 0),
'hasUnmarkConfirmation': false,
'goToComment': openComments
};
break;
case "radar":
route = Routes.radarDetails;
args = {
// 'onMarkChanged': (id, value) =>
// markChangeHandler(data.type, id, value),
'id': int.parse(data.id.toString()),
'args': const RadarRequestArgs(page: 0),
'hasUnmarkConfirmation': false,
'goToComment': openComments
};
break;
case "video":
route = Routes.studioDetails;
args = {
'type': 'podcast',
'id': int.parse(data.id.toString()),
'goToComment': openComments
};
break;
case "podcast":
route = Routes.podcasts;
args = {
'type': 'podcast',
'id': data.id,
};
break;
}
} else {
if (data.link!.startsWith('http')) {
launchUrlString(
'${data.link}',
mode: LaunchMode.inAppWebView,
);
}
}
if (route.isNotEmpty) {
navigatorKey.currentState!.pushNamed(route, arguments: args);
}
// MyApp.navigatorKey.currentState?.pushNamedAndRemoveUntil('/notification-page',
// (route) => (route.settings.name != '/notification-page') || route.isFirst,
// arguments: receivedAction);
// print("onActionReceivedMethod--------------------------------------------------------------");
}
}