274 lines
9.4 KiB
Dart
274 lines
9.4 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 '../../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();
|
|
getTokens();
|
|
}
|
|
|
|
Future<void> getNotificationSettings() async {
|
|
// Get the current notification settings.
|
|
notificationSettings =
|
|
await FirebaseMessaging.instance.getNotificationSettings();
|
|
print('------noti======');
|
|
print(notificationSettings!.authorizationStatus.toString());
|
|
// if (notificationSettings?.authorizationStatus.toString() ==
|
|
// 'AuthorizationStatus.denied') {
|
|
// await Get.defaultDialog(
|
|
// title: 'AuthorizationStatus',
|
|
// content: TextButton(
|
|
// onPressed: () {
|
|
// requestPermission();
|
|
// },
|
|
// child: Text(
|
|
// "Text Button",
|
|
// ),
|
|
// ),
|
|
// );
|
|
// }
|
|
update();
|
|
}
|
|
|
|
Future checkAndRequestPermission() async {
|
|
if (notificationSettings!.authorizationStatus ==
|
|
AuthorizationStatus.denied) {
|
|
await requestPermission();
|
|
}
|
|
}
|
|
|
|
Future requestPermission() async {
|
|
// Check if the platform is Android
|
|
if (Platform.isAndroid) {
|
|
// Request permission for Android
|
|
fcmToken.requestPermission().then((value) {
|
|
if (value.authorizationStatus == AuthorizationStatus.denied) {
|
|
print('dddddddddddddddddddddd');
|
|
}
|
|
print(value.authorizationStatus);
|
|
});
|
|
} else {
|
|
// Request permission for iOS
|
|
NotificationSettings settings = await fcmToken.requestPermission(
|
|
alert: true,
|
|
announcement: true,
|
|
badge: true,
|
|
carPlay: true,
|
|
criticalAlert: true,
|
|
provisional: false,
|
|
sound: true,
|
|
);
|
|
print('User granted permission: ${settings.authorizationStatus}');
|
|
fcmToken.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");
|
|
}
|
|
}
|
|
|
|
void getToken() async {
|
|
// final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
await fcmToken.getToken().then(
|
|
(token) {
|
|
box.write(BoxName.tokenFCM, token);
|
|
print(box.read(BoxName.tokenFCM).toString());
|
|
},
|
|
);
|
|
FirebaseMessaging.onMessage.listen((event) {
|
|
print(
|
|
'onMessage: ${event.notification!.title} ${event.notification!.body}');
|
|
if (Get.currentRoute == '/') {
|
|
// If the app is in the foreground, show a dialog to the user
|
|
if (event.notification!.title.toString().contains('ride')) {
|
|
Get.snackbar('ride', 'message', backgroundColor: AppColor.redColor);
|
|
} else if (event.notification!.title.toString().contains('انذار')) {
|
|
// Get.to(() => const ProfilePage());
|
|
} else if (event.notification!.title
|
|
.toString()
|
|
.contains('تعديل طلبيات')) {}
|
|
} else if (event.notification!.title.toString().contains('Deleted')) {
|
|
// LogOutController().removeKeys(prefs);
|
|
} else {
|
|
// If the app is in the background or terminated, show a system tray message
|
|
RemoteNotification? notification = event.notification;
|
|
AndroidNotification? android = event.notification?.android;
|
|
// if (notification != null && android != null) {
|
|
if (notification != null && android != null) {
|
|
print(
|
|
'onMessageOpenedApp: ${notification.title} ${notification.body}');
|
|
if (event.notification!.title.toString().contains('ride')) {
|
|
Get.snackbar('ride', 'message', backgroundColor: AppColor.redColor);
|
|
// SendGpsNow().getSiteNotification();
|
|
} else if (event.notification!.title.toString().contains('انذار')) {
|
|
// Get.to(() => const ProfilePage());
|
|
} else if (event.notification!.title
|
|
.toString()
|
|
.contains('تعديل طلبيات')) {
|
|
// Get.to(() => const OrdersDriverPage());
|
|
} else if (event.notification!.title.toString().contains('Deleted')) {
|
|
// LogOutController().removeKeys(prefs);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage event) {
|
|
print(
|
|
'onMessageOpenedApp: ${event.notification!.title} ${event.notification!.body}');
|
|
RemoteNotification? notification = event.notification;
|
|
if (notification != null) {
|
|
print('onMessageOpenedApp: ${notification.title} ${notification.body}');
|
|
|
|
if (event.notification!.title.toString().contains('ride')) {
|
|
Get.snackbar('ride', 'message', backgroundColor: AppColor.redColor);
|
|
// SendGpsNow().getSiteNotification();
|
|
} else if (event.notification!.title.toString().contains('انذار')) {
|
|
// Get.to(() => const ProfilePage());
|
|
} else if (event.notification!.title
|
|
.toString()
|
|
.contains('تعديل طلبيات')) {
|
|
// Get.to(() => const OrdersDriverPage());
|
|
} else if (event.notification!.title.toString().contains('Deleted')) {
|
|
// LogOutController().removeKeys(prefs);
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
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');
|
|
});
|
|
}
|
|
}
|