Files
driver_tripz/lib/controller/firebase/local_notification.dart
Hamza-Ayed 2bc71355c3 7/31/1
2024-07-31 21:19:19 +03:00

125 lines
4.5 KiB
Dart

import 'dart:convert';
import 'package:SEFER/constant/box_name.dart';
import 'package:SEFER/constant/colors.dart';
import 'package:SEFER/views/home/Captin/orderCaptin/order_request_page.dart';
import 'package:SEFER/views/home/my_wallet/walet_captain.dart';
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
import 'package:get/get.dart';
import '../../main.dart';
import '../../print.dart';
class NotificationController extends GetxController {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
// Initializes the local notifications plugin
Future<void> initNotifications() async {
const AndroidInitializationSettings android =
AndroidInitializationSettings('app_icon');
const InitializationSettings initializationSettings =
InitializationSettings(android: android);
await _flutterLocalNotificationsPlugin.initialize(
initializationSettings,
onDidReceiveNotificationResponse: onDidReceiveNotificationResponse,
onDidReceiveBackgroundNotificationResponse:
onDidReceiveBackgroundNotificationResponse,
);
// Create a notification channel
const AndroidNotificationChannel channel = AndroidNotificationChannel(
'order_channel', // Channel ID
'Order Notifications', // Channel name
description:
'This channel is used for order notifications.', // Channel description
importance: Importance.max,
);
// 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('order_channel', 'Order Notifications',
importance: Importance.max,
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,
subText: message,
actions: [
AndroidNotificationAction(
allowGeneratedReplies: true,
'id',
title.tr,
titleColor: AppColor.bronze,
showsUserInterface: true,
)
],
category: AndroidNotificationCategory.call);
NotificationDetails details = NotificationDetails(android: android);
await _flutterLocalNotificationsPlugin.show(0, title, message, details,
payload: payLoad);
// payload: 'order_page_payload');
}
// Callback when the notification is tapped
void onDidReceiveNotificationResponse(NotificationResponse response) {
// jsonDecode(response.payload);
print('Notification tapped!');
if (response.payload != null) {
print('Notification payload: ${response.payload}');
// if (response.payload != 'order_page_payload') {
// Log.print('arguments: ${box.read(BoxName.rideArguments)}');
closeOverLay();
Get.to(() => OrderRequestPage(),
arguments: {'myListString': response.payload});
// }
}
}
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
}
// Callback when the notification is tapped while the app is in the background
void onDidReceiveBackgroundNotificationResponse(
NotificationResponse response) {
print('Notification tapped while app is in background!');
if (response.payload != null) {
print('Notification payload: ${response.payload}');
if (response.payload == 'order') {
Get.to(() => OrderRequestPage(),
arguments: box.read(BoxName.rideArguments));
closeOverLay();
Log.print('arguments: ${box.read(BoxName.rideArguments)}');
}
}
}
}