|
|
import 'dart:convert';
|
|
|
import 'dart:io';
|
|
|
|
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
|
import 'package:flutter/material.dart';
|
|
|
import 'package:google_api_availability/google_api_availability.dart';
|
|
|
import 'package:huawei_push/huawei_push.dart' as h_push;
|
|
|
import 'package:test_sa/controllers/notification/notification_manger.dart';
|
|
|
import 'package:test_sa/models/device/device_transfer.dart';
|
|
|
import 'package:test_sa/models/new_models/gas_refill_model.dart';
|
|
|
import 'package:test_sa/modules/cm_module/views/service_request_detail_main_view.dart';
|
|
|
import 'package:test_sa/modules/pm_module/ppm_wo/ppm_details_page.dart';
|
|
|
import 'package:test_sa/modules/pm_module/recurrent_wo/recurrent_work_order_view.dart';
|
|
|
import 'package:test_sa/views/pages/device_transfer/device_transfer_details.dart';
|
|
|
import 'package:test_sa/views/pages/user/gas_refill/gas_refill_details.dart';
|
|
|
import 'package:test_sa/views/widgets/loaders/no_data_found.dart';
|
|
|
|
|
|
@pragma('vm:entry-point')
|
|
|
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {}
|
|
|
|
|
|
class FirebaseNotificationManger {
|
|
|
static FirebaseMessaging messaging = FirebaseMessaging.instance;
|
|
|
static String? token;
|
|
|
|
|
|
static Future<void> getToken() async {
|
|
|
NotificationSettings settings = await messaging.requestPermission(alert: true, announcement: false, badge: true, carPlay: false, criticalAlert: false, provisional: false, sound: true);
|
|
|
|
|
|
if (Platform.isAndroid) {
|
|
|
try {
|
|
|
if (!(await isGoogleServicesAvailable())) {
|
|
|
h_push.Push.enableLogger();
|
|
|
final result = await h_push.Push.setAutoInitEnabled(true);
|
|
|
h_push.Push.onMessageReceivedStream.listen(_onMessageReceived, onError: _onMessageReceiveError);
|
|
|
|
|
|
h_push.Push.getTokenStream.listen((hToken) {
|
|
|
// onToken(token);
|
|
|
// print("Huawei Token: ${hToken}");
|
|
|
token = hToken;
|
|
|
}, onError: (e) {
|
|
|
print("Huawei TokenError" + e.toString());
|
|
|
});
|
|
|
h_push.Push.getToken('');
|
|
|
} else {
|
|
|
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
|
|
|
try {
|
|
|
token = await messaging.getToken();
|
|
|
} catch (ex) {}
|
|
|
}
|
|
|
}
|
|
|
} catch (ex) {
|
|
|
print("Notification Exception: " + ex.toString());
|
|
|
}
|
|
|
} else {
|
|
|
if (settings.authorizationStatus == AuthorizationStatus.authorized) {
|
|
|
try {
|
|
|
token = await messaging.getToken();
|
|
|
} catch (ex) {}
|
|
|
}
|
|
|
}
|
|
|
//print("pushToken:$token");
|
|
|
}
|
|
|
|
|
|
static void _onMessageReceived(h_push.RemoteMessage remoteMessage) {
|
|
|
print("onMessageReceivedStream:${remoteMessage.toMap()}");
|
|
|
}
|
|
|
|
|
|
static void _onMessageReceiveError(Object error) {
|
|
|
print("onMessageReceivedStream:${error.toString()}");
|
|
|
}
|
|
|
|
|
|
static Future<bool> isGoogleServicesAvailable() async {
|
|
|
try {
|
|
|
GooglePlayServicesAvailability availability = await GoogleApiAvailability.instance.checkGooglePlayServicesAvailability();
|
|
|
String status = availability.toString().split('.').last;
|
|
|
if (status == "success") {
|
|
|
return true;
|
|
|
}
|
|
|
return false;
|
|
|
} catch (ex) {
|
|
|
return false;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static void handleMessage(context, Map<String, dynamic> messageData) {
|
|
|
if (messageData["requestType"] != null && messageData["requestNumber"] != null) {
|
|
|
Widget? serviceClass;
|
|
|
// PPM=1,
|
|
|
// ServiceRequestEngineer = 3,
|
|
|
// AssetTransfer=7,
|
|
|
// SparePartTransaction= 8,
|
|
|
// GasRefill=9,
|
|
|
// TechnicalRetirmentWO = 11,
|
|
|
// Recurrent = 12,
|
|
|
switch (messageData["transactionType"]) {
|
|
|
case 1:
|
|
|
serviceClass = PpmDetailsPage(requestId: int.parse(messageData["requestNumber"].toString()));
|
|
|
break;
|
|
|
//these three request are same corrective maintenance....
|
|
|
case 3:
|
|
|
serviceClass = ServiceRequestDetailMain(requestId: messageData["requestNumber"] ?? '');
|
|
|
break;
|
|
|
case 8:
|
|
|
serviceClass = ServiceRequestDetailMain(requestId: messageData["requestNumber"] ?? '');
|
|
|
break;
|
|
|
case 11:
|
|
|
serviceClass = ServiceRequestDetailMain(requestId: messageData["requestNumber"] ?? '');
|
|
|
break;
|
|
|
case 7:
|
|
|
serviceClass = DeviceTransferDetails(model: DeviceTransfer(id: int.parse(messageData["requestNumber"].toString())));
|
|
|
break;
|
|
|
case 9:
|
|
|
serviceClass = GasRefillDetailsPage(
|
|
|
priority: messageData["priority"],
|
|
|
date: messageData["createdOn"],
|
|
|
model: GasRefillModel(id: int.parse(messageData["requestNumber"].toString())),
|
|
|
);
|
|
|
break;
|
|
|
case 12:
|
|
|
serviceClass = RecurrentWorkOrderView(taskId: int.parse(messageData["requestNumber"].toString()));
|
|
|
//Didn't handle task request yet...
|
|
|
// case 6:
|
|
|
// serviceClass = TaskRequestDetailsView(
|
|
|
// taskId: int.parse(messageData["requestNumber"].toString()),
|
|
|
// requestDetails: RequestsDetails(nameOfType: messageData["sourceName"], status: messageData["statusName"], priority: messageData["priorityName"], date: messageData["createdDate"]));
|
|
|
// return;
|
|
|
default:
|
|
|
serviceClass = const Scaffold(body: Center(child: NoDataFound()));
|
|
|
}
|
|
|
|
|
|
// if (messageData["requestType"] == "Service request to engineer") {
|
|
|
// serviceClass = ServiceRequestDetailMain(requestId: messageData["requestNumber"] ?? '');
|
|
|
// } else if (messageData["requestType"] == "Gas Refill") {
|
|
|
// serviceClass = GasRefillDetailsPage(
|
|
|
// priority: messageData["priority"],
|
|
|
// date: messageData["createdOn"],
|
|
|
// model: GasRefillModel(id: int.parse(messageData["requestNumber"].toString())),
|
|
|
// );
|
|
|
// } else if (messageData["requestType"] == "Asset Transfer") {
|
|
|
// serviceClass = DeviceTransferDetails(model: DeviceTransfer(id: int.parse(messageData["requestNumber"].toString())));
|
|
|
// } else if (messageData["requestType"] == "PPM") {
|
|
|
// serviceClass = PpmDetailsPage(requestId: int.parse(messageData["requestNumber"].toString()));
|
|
|
// }
|
|
|
// else if (data["requestType"] == "WorkOrder") {
|
|
|
//
|
|
|
// }
|
|
|
Navigator.of(context).push(MaterialPageRoute(builder: (_) => serviceClass!));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static initialized(BuildContext context) async {
|
|
|
//TOD0 add platform check here also
|
|
|
if (!(await isGoogleServicesAvailable()) && Platform.isAndroid) {
|
|
|
var initialNotification = await h_push.Push.getInitialNotification();
|
|
|
if (initialNotification != null) {
|
|
|
Map<String, dynamic> remoteData = Map<String, dynamic>.from(initialNotification["extras"] as Map);
|
|
|
handleMessage(context, remoteData);
|
|
|
}
|
|
|
|
|
|
h_push.Push.onNotificationOpenedApp.listen((message) {
|
|
|
try {
|
|
|
if (message is Map<String, dynamic>) {
|
|
|
Map<String, dynamic> remoteData = message;
|
|
|
remoteData = remoteData["extras"];
|
|
|
|
|
|
handleMessage(context, remoteData);
|
|
|
}
|
|
|
} catch (ex) {
|
|
|
print("parsingError:$ex");
|
|
|
}
|
|
|
}, onError: (e) => print("onNotificationOpenedApp Error${e.toString()}"));
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
NotificationSettings settings;
|
|
|
|
|
|
try {
|
|
|
settings = await messaging.requestPermission(
|
|
|
alert: true,
|
|
|
announcement: false,
|
|
|
badge: true,
|
|
|
carPlay: false,
|
|
|
criticalAlert: false,
|
|
|
provisional: false,
|
|
|
sound: true,
|
|
|
);
|
|
|
} catch (error) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
if (settings.authorizationStatus != AuthorizationStatus.authorized) {
|
|
|
return;
|
|
|
}
|
|
|
|
|
|
await FirebaseMessaging.instance.setAutoInitEnabled(true);
|
|
|
await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions(alert: true, badge: true, sound: true);
|
|
|
|
|
|
FirebaseMessaging.instance.getInitialMessage().then((initialMessage) {
|
|
|
if (initialMessage != null) {
|
|
|
handleMessage(context, initialMessage.data);
|
|
|
}
|
|
|
});
|
|
|
|
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
|
|
print('notification i got is ${message.toMap()}');
|
|
|
if (Platform.isAndroid) {
|
|
|
if (message.data["notificationType"] != 'NurseConfirmArrive') {
|
|
|
NotificationManger.showNotification(
|
|
|
title: message.notification?.title ?? "", subtext: message.notification?.body ?? "", hashcode: int.tryParse("1234" ?? "") ?? 1, payload: json.encode(message.data), context: context);
|
|
|
}
|
|
|
}
|
|
|
return;
|
|
|
});
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
|
|
handleMessage(context, message.data);
|
|
|
});
|
|
|
FirebaseMessaging.onBackgroundMessage(firebaseMessagingBackgroundHandler);
|
|
|
}
|
|
|
}
|