164 lines
4.9 KiB
Dart
164 lines
4.9 KiB
Dart
|
|
import 'dart:typed_data';
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:get/route_manager.dart';
|
|
|
|
import '../Controller/Dashboard/Tabs/Completedordercontroller.dart';
|
|
import '../Controller/Dashboard/Tabs/Ongoingordercontroller.dart';
|
|
import '../Controller/Dashboard/Tabs/Ordercontroller.dart';
|
|
import '../View/Home/Homeview.dart';
|
|
import '../maindev.dart';
|
|
|
|
|
|
|
|
class LocalNotificationService {
|
|
|
|
|
|
static final instance = FirebaseMessaging.instance;
|
|
static final FlutterLocalNotificationsPlugin _notificationsPlugin =
|
|
FlutterLocalNotificationsPlugin();
|
|
int? moduleId;
|
|
String? notificationOrderId;
|
|
int? locationId;
|
|
static void setValues(message){
|
|
|
|
}
|
|
|
|
static void initialize(BuildContext context) async{
|
|
|
|
instance.requestPermission(
|
|
alert: true,
|
|
badge: true,
|
|
sound: true,
|
|
);
|
|
await _notificationsPlugin.resolvePlatformSpecificImplementation<AndroidFlutterLocalNotificationsPlugin>()?.createNotificationChannel(channel);
|
|
final DarwinInitializationSettings initializationSettingsIOS = DarwinInitializationSettings(
|
|
requestSoundPermission: false,
|
|
requestBadgePermission: false,
|
|
requestAlertPermission: false,
|
|
);
|
|
final InitializationSettings initializationSettings =
|
|
InitializationSettings(
|
|
android: AndroidInitializationSettings("ic_launcher"),
|
|
iOS: initializationSettingsIOS
|
|
);
|
|
|
|
_notificationsPlugin.initialize(
|
|
initializationSettings,
|
|
onDidReceiveNotificationResponse: (NotificationResponse response) {
|
|
if (response.payload != null) {
|
|
selectNotification(response.payload!);
|
|
}
|
|
});
|
|
|
|
|
|
// TextToSpeech tts = TextToSpeech();
|
|
|
|
CurrentOrderController currentOrderController = Get.put(CurrentOrderController());
|
|
OnGoingOrderController onGoingOrderController = Get.put(OnGoingOrderController());
|
|
CompletedOrderController completedOrderController = Get.put(CompletedOrderController()) ;
|
|
|
|
|
|
FirebaseMessaging.instance.getInitialMessage().then((message) async{
|
|
if (message != null) {
|
|
|
|
RemoteNotification notification = message.notification!;
|
|
AndroidNotification? android = message.notification?.android;
|
|
if (android != null) {
|
|
|
|
display(message);
|
|
currentOrderController.getOrder('created');
|
|
onGoingOrderController.getCurrentOrder();
|
|
completedOrderController.getCompletedOrder();
|
|
|
|
}
|
|
}
|
|
});
|
|
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) async{
|
|
print('message before data ');
|
|
print(message.data);
|
|
RemoteNotification notification = message.notification!;
|
|
AndroidNotification? android = message.notification?.android;
|
|
print('message before if data ');
|
|
|
|
if (android != null) {
|
|
|
|
display(message);
|
|
currentOrderController.getOrder('created');
|
|
onGoingOrderController.getCurrentOrder();
|
|
completedOrderController.getCompletedOrder();
|
|
|
|
print('message after if data ');
|
|
}
|
|
});
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) async{
|
|
print('A new onMessageOpenedApp event was published!');
|
|
// message.data['orderid'];
|
|
RemoteNotification? notification = message.notification;
|
|
AndroidNotification? android = message.notification?.android;
|
|
if (notification != null && android != null) {
|
|
|
|
Get.to(()=> HomeView(selectedIndex: 0,));
|
|
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
|
|
static Future<dynamic> selectNotification(String? payload) async {
|
|
|
|
try {
|
|
Get.to(()=> HomeView(selectedIndex: 0,));
|
|
} on Exception catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
static void display(RemoteMessage message) async {
|
|
print('message notification${message.notification}');
|
|
print('message mutableContent${message.mutableContent}');
|
|
|
|
try {
|
|
final id = DateTime.now().millisecondsSinceEpoch ~/1000;
|
|
|
|
|
|
final NotificationDetails notificationDetails = NotificationDetails(
|
|
|
|
android: AndroidNotificationDetails(
|
|
"Nearlexpress Business",
|
|
"Nearlexpress Business Notification",
|
|
importance: Importance.max,
|
|
priority: Priority.high,
|
|
icon: 'ic_launcher',
|
|
playSound: true,
|
|
sound: RawResourceAndroidNotificationSound('ring'),
|
|
largeIcon: const DrawableResourceAndroidBitmap('ic_launcher'),
|
|
vibrationPattern: Int64List.fromList([500, 1000, 500]),
|
|
enableVibration:true,
|
|
),
|
|
);
|
|
|
|
await _notificationsPlugin.show(
|
|
id,
|
|
message.notification!.title,
|
|
message.notification!.body,
|
|
notificationDetails,
|
|
payload: message.data.toString(),
|
|
);
|
|
} on Exception catch (e) {
|
|
print(e);
|
|
}
|
|
}
|
|
|
|
|
|
} |