Files
driver_tripz/lib/controller/firebase/local_notification.dart
Hamza-Ayed 6f95dafd0b 2-10/1/1
2024-10-01 19:36:38 +03:00

210 lines
7.1 KiB
Dart

import 'dart:convert';
import 'dart:typed_data';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/views/home/Captin/orderCaptin/order_request_page.dart';
import 'package:SEFER/views/home/Captin/orderCaptin/order_speed_request.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import '../../main.dart';
import '../../print.dart';
import '../../views/notification/notification_captain.dart';
import '../home/captin/home_captain_controller.dart';
class NotificationController extends GetxController {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Initializes the local notifications plugin
Future<void> initNotifications() async {
const AndroidInitializationSettings android =
AndroidInitializationSettings('@mipmap/launcher_icon');
const InitializationSettings initializationSettings =
InitializationSettings(android: android);
await _flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
onDidReceiveBackgroundNotificationResponse:
onDidReceiveBackgroundNotificationResponse,
);
// Create a notification channel
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'high_importance_channel',
'High Importance Notifications',
description: 'This channel is used for important notifications.',
importance: Importance.high,
);
// Register the channel with the system
await _flutterLocalNotificationsPlugin
.resolvePlatformSpecificImplementation<
AndroidFlutterLocalNotificationsPlugin>()
?.createNotificationChannel(channel);
}
// Displays a notification with the given title and message
void showNotification(
String title, String message, String tone, String payLoad) async {
BigTextStyleInformation bigTextStyleInformation = BigTextStyleInformation(
message,
contentTitle: title.tr,
htmlFormatContent: true,
htmlFormatContentTitle: true,
);
AndroidNotificationDetails android = AndroidNotificationDetails(
'high_importance_channel', 'High Importance Notifications',
importance: Importance.high,
priority: Priority.high,
styleInformation: bigTextStyleInformation,
playSound: true,
sound: RawResourceAndroidNotificationSound(tone),
// audioAttributesUsage: AudioAttributesUsage.alarm,
visibility: NotificationVisibility.public,
autoCancel: false,
color: AppColor.primaryColor,
showProgress: true,
showWhen: true,
ongoing: true,
enableVibration: true,
vibrationPattern: Int64List.fromList([0, 1000, 500, 1000]),
timeoutAfter: 14500,
setAsGroupSummary: true,
subText: message, fullScreenIntent: true,
actions: [
AndroidNotificationAction(
allowGeneratedReplies: true,
'id',
title.tr,
titleColor: AppColor.bronze,
showsUserInterface: true,
)
],
category: AndroidNotificationCategory.progress,
);
NotificationDetails details = NotificationDetails(android: android);
await _flutterLocalNotificationsPlugin.show(0, title, message, details,
payload: jsonEncode({'title': title, 'data': payLoad}));
}
// Callback when the notification is tapped
void onDidReceiveNotificationResponse(NotificationResponse response) {
_handleNotificationResponse(response);
}
// Callback when the notification is tapped while the app is in the background
void onDidReceiveBackgroundNotificationResponse(
NotificationResponse response) {
_handleNotificationResponse(response);
}
// Handle notification response for both foreground and background
void _handleNotificationResponse(NotificationResponse response) {
print('Notification tapped!');
Log.print('response.payload: ${response.payload}');
if (response.payload != null) {
print('Notification payload: ${response.payload}');
var payloadData = jsonDecode(response.payload.toString());
if (payloadData is Map<String, dynamic>) {
String title = payloadData['title'];
var data = payloadData['data'];
switch (title) {
case 'Order':
_handleOrderNotification(data);
break;
case 'OrderSpeed':
_handleOrderSpeedNotification(data);
break;
case 'ADS':
_handleADSNotification();
break;
default:
Log.print('Unknown notification type');
}
} else {
Log.print('Invalid payload format');
}
} else {
Log.print('Payload is null');
}
}
void _handleOrderNotification(dynamic data) {
if (data is String) {
var orderData = jsonDecode(data);
if (orderData is List && orderData.length == 34) {
closeOverLay();
Get.put(HomeCaptainController()).changeRideId();
Get.to(() => OrderRequestPage(), arguments: {'myListString': data});
} else {
Log.print('Invalid order data');
}
} else {
Log.print('Invalid order payload');
}
}
void _handleOrderSpeedNotification(dynamic data) {
if (data is String) {
var orderData = jsonDecode(data);
if (orderData is List && orderData.length == 34) {
closeOverLay();
Get.put(HomeCaptainController()).changeRideId();
Get.to(() => OrderSpeedRequest(), arguments: {'myListString': data});
} else {
Log.print('Invalid order data');
}
} else {
Log.print('Invalid order payload');
}
}
void _handleADSNotification() {
// var orderData = jsonDecode(data);
closeOverLay();
Get.to(
() => const NotificationCaptain(),
);
}
void onDidReceiveLocalNotification(
int id, String? title, String? body, String? payload) async {
// display a dialog with the notification details, tap ok to go to another page
}
}
class NotificationController1 extends GetxController {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Initializes the local notifications plugin
Future<void> initNotifications() async {
const AndroidInitializationSettings android =
AndroidInitializationSettings('@mipmap/launcher_icon');
const InitializationSettings initializationSettings =
InitializationSettings(android: android);
await _flutterLocalNotificationsPlugin.initialize(initializationSettings);
}
// Displays a notification with the given title and message
void showNotification(
String title, String message, String tone, String payLoad) async {
AndroidNotificationDetails android = AndroidNotificationDetails(
'your channel id', 'your channel name',
importance: Importance.max,
priority: Priority.high,
showWhen: false,
sound: RawResourceAndroidNotificationSound(tone));
NotificationDetails details = NotificationDetails(android: android);
await _flutterLocalNotificationsPlugin.show(0, title, message, details);
}
}