314 lines
11 KiB
Dart
314 lines
11 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
import 'package:firebase_messaging/firebase_messaging.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:get/get.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:ride/constant/colors.dart';
|
|
import 'package:ride/constant/style.dart';
|
|
import 'package:ride/controller/functions/launch.dart';
|
|
import 'package:ride/views/widgets/elevated_btn.dart';
|
|
|
|
import '../../constant/box_name.dart';
|
|
import '../../constant/credential.dart';
|
|
import '../../constant/links.dart';
|
|
import '../../main.dart';
|
|
|
|
class FirebasMessagesController extends GetxController {
|
|
final fcmToken = FirebaseMessaging.instance;
|
|
|
|
List<String> tokens = [];
|
|
List dataTokens = [];
|
|
NotificationSettings? notificationSettings;
|
|
@override
|
|
void onInit() async {
|
|
super.onInit();
|
|
// getNotificationSettings();
|
|
getToken();
|
|
await getTokens();
|
|
}
|
|
|
|
Future<void> getNotificationSettings() async {
|
|
// Get the current notification settings
|
|
NotificationSettings? notificationSettings =
|
|
await FirebaseMessaging.instance.getNotificationSettings();
|
|
print(
|
|
'Notification authorization status: ${notificationSettings.authorizationStatus}');
|
|
|
|
// Call the update function if needed
|
|
update();
|
|
}
|
|
|
|
Future<void> requestFirebaseMessagingPermission() async {
|
|
FirebaseMessaging messaging = FirebaseMessaging.instance;
|
|
|
|
// Check if the platform is Android
|
|
if (Platform.isAndroid) {
|
|
// Request permission for Android
|
|
await messaging.requestPermission();
|
|
} else if (Platform.isIOS) {
|
|
// Request permission for iOS
|
|
NotificationSettings settings = await messaging.requestPermission(
|
|
alert: true,
|
|
announcement: true,
|
|
badge: true,
|
|
carPlay: true,
|
|
criticalAlert: true,
|
|
provisional: false,
|
|
sound: true,
|
|
);
|
|
print('User granted permission: ${settings.authorizationStatus}');
|
|
messaging.setForegroundNotificationPresentationOptions(
|
|
alert: true, badge: true, sound: true);
|
|
}
|
|
}
|
|
|
|
Future getTokens() async {
|
|
var res = await http.post(
|
|
Uri.parse(AppLink.getTokens),
|
|
headers: {
|
|
'Authorization':
|
|
'Basic ${base64Encode(utf8.encode(AppCredintials.basicAuthCredentials))}',
|
|
},
|
|
body: {},
|
|
);
|
|
// print(res.body);
|
|
var jsonResponse = jsonDecode(res.body);
|
|
// print(jsonResponse);
|
|
if (jsonResponse['status'] == 'success') {
|
|
dataTokens = jsonResponse['data'];
|
|
for (var i = 0; i < dataTokens.length; i++) {
|
|
tokens.add(jsonResponse['data'][i]['token']);
|
|
}
|
|
box.write(BoxName.tokens, tokens);
|
|
print(box.read(BoxName.tokens));
|
|
} else {
|
|
Get.defaultDialog(title: "Warning", middleText: "Server Error");
|
|
}
|
|
}
|
|
|
|
Future getToken() async {
|
|
fcmToken.getToken().then((token) {
|
|
box.write(BoxName.tokenFCM, token);
|
|
print(token);
|
|
});
|
|
|
|
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
|
|
if (message.data.isNotEmpty) {
|
|
if (message.notification!.title!.contains('Order')) {
|
|
var myListString = message.data['myList'];
|
|
var myList = jsonDecode(myListString) as List<dynamic>;
|
|
Get.defaultDialog(
|
|
title: message.notification!.title.toString(),
|
|
content: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
children: [
|
|
// Text(message.notification!.body.toString()),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: TextButton.icon(
|
|
onPressed: () {
|
|
String mapUrl =
|
|
'https://www.google.com/maps/dir/${myList[0]}/${myList[0]}/';
|
|
print(mapUrl);
|
|
launchUrl1(mapUrl);
|
|
},
|
|
icon: const Icon(Icons.map),
|
|
label: Text('Rouats of Trip'.tr)),
|
|
),
|
|
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text(
|
|
'Tatal From Passenger is '.tr +
|
|
double.parse(myList[2]).toStringAsFixed(2),
|
|
style: AppStyle.title,
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text(
|
|
'Tatal For You is '.tr +
|
|
double.parse(myList[3]).toStringAsFixed(2),
|
|
style: AppStyle.title,
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text(
|
|
'Distance is '.tr + myList[4].toString() + ' Minutes'.tr,
|
|
style: AppStyle.title,
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Text(
|
|
'Duration of Trip is '.tr + myList[5].toString(),
|
|
style: AppStyle.title,
|
|
),
|
|
),
|
|
Padding(
|
|
padding: const EdgeInsets.all(8.0),
|
|
child: Row(
|
|
mainAxisAlignment: MainAxisAlignment.spaceBetween,
|
|
children: [
|
|
MyElevatedButton(
|
|
title: 'Apply Order'.tr,
|
|
onPressed: () {
|
|
//TODO save and request to passenger
|
|
},
|
|
),
|
|
MyElevatedButton(
|
|
title: 'Refuse Order'.tr,
|
|
onPressed: () {
|
|
//TODO save and request to passenger and driver log upto 3 per 1 day
|
|
},
|
|
kolor: AppColor.redColor,
|
|
),
|
|
],
|
|
),
|
|
)
|
|
],
|
|
));
|
|
}
|
|
}
|
|
});
|
|
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
|
|
print(
|
|
'onMessageOpenedApp: ${message.notification!.title} ${message.notification!.body}');
|
|
RemoteNotification? notification = message.notification;
|
|
if (notification != null) {
|
|
print('onMessageOpenedApp: ${notification.title} ${notification.body}');
|
|
if (message.notification!.title!.contains('SEFER')) {
|
|
Get.snackbar('SEFER', 'message', backgroundColor: AppColor.redColor);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
void sendNotificationAll(String title, body) async {
|
|
// Get the token you want to subtract.
|
|
String token = box.read(BoxName.tokenFCM);
|
|
tokens = box.read(BoxName.tokens);
|
|
// Subtract the token from the list of tokens.
|
|
tokens.remove(token);
|
|
|
|
// Save the list of tokens back to the box.
|
|
// box.write(BoxName.tokens, tokens);
|
|
tokens = box.read(BoxName.tokens);
|
|
// print(tokens);
|
|
for (var i = 0; i < tokens.length; i++) {
|
|
http
|
|
.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'key=${AppCredintials.serverAPI}'
|
|
},
|
|
body: jsonEncode({
|
|
'notification': <String, dynamic>{
|
|
'title': title,
|
|
'body': body,
|
|
'sound': 'true'
|
|
},
|
|
'priority': 'high',
|
|
'data': <String, dynamic>{
|
|
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
|
|
'id': '1',
|
|
'status': 'done'
|
|
},
|
|
'to': tokens[i],
|
|
}))
|
|
.whenComplete(() {})
|
|
.catchError((e) {
|
|
print('sendNotification() error: $e');
|
|
});
|
|
}
|
|
}
|
|
|
|
// void sendNotificationToManager(String title, body) async {
|
|
// // tokens = box.read(BoxName.tokenManager);
|
|
|
|
// for (var i = 0; i < tokens.length; i++) {
|
|
// http
|
|
// .post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
|
|
// headers: <String, String>{
|
|
// 'Content-Type': 'application/json',
|
|
// 'Authorization': 'key=${AppCredintials.serverAPI}'
|
|
// },
|
|
// body: jsonEncode({
|
|
// 'notification': <String, dynamic>{
|
|
// 'title': title,
|
|
// 'body': body,
|
|
// 'sound': 'true'
|
|
// },
|
|
// 'priority': 'high',
|
|
// 'data': <String, dynamic>{
|
|
// 'click_action': 'FLUTTER_NOTIFICATION_CLICK',
|
|
// 'id': '1',
|
|
// 'status': 'done'
|
|
// },
|
|
// 'to': tokens[i],
|
|
// }))
|
|
// .whenComplete(() {})
|
|
// .catchError((e) {
|
|
// print('sendNotification() error: $e');
|
|
// });
|
|
// }
|
|
// }
|
|
|
|
// void sendNotificationDriverId(String title, body, token) async {
|
|
// http
|
|
// .post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
|
|
// headers: <String, String>{
|
|
// 'Content-Type': 'application/json',
|
|
// 'Authorization': 'key=${AppCredintials.serverAPI}'
|
|
// },
|
|
// body: jsonEncode({
|
|
// 'notification': <String, dynamic>{
|
|
// 'title': title,
|
|
// 'body': body,
|
|
// 'sound': 'true'
|
|
// },
|
|
// 'priority': 'high',
|
|
// 'data': <String, dynamic>{
|
|
// 'click_action': 'FLUTTER_NOTIFICATION_CLICK',
|
|
// 'id': '1',
|
|
// 'status': 'done'
|
|
// },
|
|
// 'to': token,
|
|
// }))
|
|
// .whenComplete(() {})
|
|
// .catchError((e) {
|
|
// print('sendNotification() error: $e');
|
|
// });
|
|
// }
|
|
|
|
void sendNotificationDriverId(String title, body, Strin1, Strin2, Strin3,
|
|
Strin4, Strin5, Strin6, token) async {
|
|
http
|
|
.post(Uri.parse('https://fcm.googleapis.com/fcm/send'),
|
|
headers: <String, String>{
|
|
'Content-Type': 'application/json',
|
|
'Authorization': 'key=${AppCredintials.serverAPI}'
|
|
},
|
|
body: jsonEncode({
|
|
'notification': <String, dynamic>{
|
|
'title': title,
|
|
'body': body,
|
|
'sound': 'true'
|
|
},
|
|
'data': {
|
|
'myList': jsonEncode(
|
|
[Strin1, Strin2, Strin3, Strin4, Strin5, Strin6]),
|
|
},
|
|
'priority': 'high',
|
|
'to': token,
|
|
}))
|
|
.whenComplete(() {})
|
|
.catchError((e) {
|
|
print('sendNotification() error: $e');
|
|
});
|
|
}
|
|
}
|