This commit is contained in:
Hamza-Ayed
2023-08-24 18:29:47 +03:00
parent c1869d2725
commit 63770619d6
48 changed files with 400 additions and 206 deletions

View File

@@ -22,52 +22,30 @@ class FirebasMessagesController extends GetxController {
super.onInit();
// getNotificationSettings();
getToken();
getTokens();
await getTokens();
}
Future<void> getNotificationSettings() async {
// Get the current notification settings.
notificationSettings =
// Get the current notification settings
NotificationSettings? 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",
// ),
// ),
// );
// }
print(
'Notification authorization status: ${notificationSettings.authorizationStatus}');
// Call the update function if needed
update();
}
Future checkAndRequestPermission() async {
if (notificationSettings!.authorizationStatus ==
AuthorizationStatus.denied) {
await requestPermission();
}
}
Future<void> requestFirebaseMessagingPermission() async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
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 {
await messaging.requestPermission();
} else if (Platform.isIOS) {
// Request permission for iOS
NotificationSettings settings = await fcmToken.requestPermission(
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: true,
badge: true,
@@ -77,7 +55,7 @@ class FirebasMessagesController extends GetxController {
sound: true,
);
print('User granted permission: ${settings.authorizationStatus}');
fcmToken.setForegroundNotificationPresentationOptions(
messaging.setForegroundNotificationPresentationOptions(
alert: true, badge: true, sound: true);
}
}
@@ -107,68 +85,42 @@ class FirebasMessagesController extends GetxController {
}
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) {
await fcmToken.getToken().then((token) {
box.write(BoxName.tokenFCM, token);
print(token);
});
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print(
'onMessage: ${event.notification!.title} ${event.notification!.body}');
'onMessage: ${message.notification!.title} ${message.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')) {
if (message.notification!.title!.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) {
RemoteNotification? notification = message.notification;
AndroidNotification? android = message.notification?.android;
if (notification != null && android != null) {
print(
'onMessageOpenedApp: ${notification.title} ${notification.body}');
if (event.notification!.title.toString().contains('ride')) {
if (message.notification!.title!.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) {
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print(
'onMessageOpenedApp: ${event.notification!.title} ${event.notification!.body}');
RemoteNotification? notification = event.notification;
'onMessageOpenedApp: ${message.notification!.title} ${message.notification!.body}');
RemoteNotification? notification = message.notification;
if (notification != null) {
print('onMessageOpenedApp: ${notification.title} ${notification.body}');
if (event.notification!.title.toString().contains('ride')) {
if (message.notification!.title!.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);
}
}
});
@@ -213,61 +165,61 @@ class FirebasMessagesController extends GetxController {
}
}
void sendNotificationToManager(String title, body) async {
// tokens = box.read(BoxName.tokenManager);
// 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');
});
}
}
// 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, 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');
// });
// }
}