From fe2787b233e4f8e7ce0186227580fe4ae7c5d587 Mon Sep 17 00:00:00 2001 From: Felipe Date: Tue, 22 Aug 2023 09:51:09 -0500 Subject: [PATCH] notificacion desde cita --- lib/src/models/event_model.dart | 5 -- lib/src/models/setting_model.dart | 1 - lib/src/models/user_model.dart | 2 +- lib/src/screens/cita.dart | 64 ++++++++++++++++++- lib/src/screens/city.dart | 4 +- lib/src/screens/horario.dart | 1 - lib/src/screens/my_services.dart | 1 - lib/src/screens/my_services_pro.dart | 1 - lib/src/screens/profession.dart | 4 +- lib/src/screens/professional_direccion.dart | 2 - lib/src/screens/professional_profile.dart | 8 --- lib/src/screens/professional_profile_web.dart | 11 ---- lib/src/screens/profile.dart | 2 - lib/src/screens/profile_pro.dart | 2 - lib/src/screens/profile_pro_web.dart | 2 - lib/src/screens/profile_web.dart | 24 +++---- lib/src/screens/service.dart | 1 - lib/src/screens/solicitudes.dart | 1 - 18 files changed, 75 insertions(+), 61 deletions(-) diff --git a/lib/src/models/event_model.dart b/lib/src/models/event_model.dart index 58b174d..fdf8ff8 100644 --- a/lib/src/models/event_model.dart +++ b/lib/src/models/event_model.dart @@ -74,7 +74,6 @@ Future> getByProId(String day) async { event.id = element.id; eventos.add(event); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e'); @@ -96,7 +95,6 @@ Future> getByProIdAll(String state1, String state2) async { event.id = element.id; eventos.add(event); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e'); @@ -118,7 +116,6 @@ Future> getByUserIdAll(String state1, String state2) async { event.id = element.id; eventos.add(event); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByUserId $e'); @@ -211,7 +208,6 @@ class Event { for (var element in snapshot.docs) { eventos.add(Event.fromJson(element.data())); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e'); @@ -232,7 +228,6 @@ class Event { for (var element in snapshot.docs) { eventos.add(Event.fromJson(element.data())); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e'); diff --git a/lib/src/models/setting_model.dart b/lib/src/models/setting_model.dart index f3e743f..86b11f7 100644 --- a/lib/src/models/setting_model.dart +++ b/lib/src/models/setting_model.dart @@ -61,7 +61,6 @@ class SettingModel { final Map? data = snapshot.data(); - print('data $data'); return fromJson(data!); } catch (e) { print('Error getting settings: $e'); diff --git a/lib/src/models/user_model.dart b/lib/src/models/user_model.dart index 9939061..15cecb9 100644 --- a/lib/src/models/user_model.dart +++ b/lib/src/models/user_model.dart @@ -32,7 +32,7 @@ class UserModel { json['token'], ); } catch (e) { - print('XD user $e'); + print('$e'); return UserModel('', '', '', null, null, 0, '', ''); } } diff --git a/lib/src/screens/cita.dart b/lib/src/screens/cita.dart index 95a3f0c..1de66df 100644 --- a/lib/src/screens/cita.dart +++ b/lib/src/screens/cita.dart @@ -1,4 +1,5 @@ import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_auth/firebase_auth.dart'; import 'package:firebase_storage/firebase_storage.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; @@ -15,6 +16,9 @@ import 'package:prosappco/src/screens/chat.dart'; import 'package:prosappco/src/screens/score.dart'; import 'package:url_launcher/url_launcher.dart'; import 'package:community_material_icon/community_material_icon.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; +import 'package:flutter/gestures.dart'; class CitaScreen extends StatefulWidget { final Event evento; @@ -29,12 +33,15 @@ class _CitaScreenState extends State { UserModel? user; DateTime today = DateTime.now(); String nombre = ''; + String userToken = ''; String numberPhone = ''; int tarifa = 0; Reference? ref_photo; ScoresModel? scoresModel; bool? ver = true; bool? pro; + String proName = ''; + late final FirebaseAuth _auth; String formatCurrency(int number) { final formatter = @@ -42,12 +49,46 @@ class _CitaScreenState extends State { return '\$${formatter.format(number)}'; } + Future sendPushNotification( + String user, String accion, String proName) async { + try { + http.Response response = await http.post( + Uri.parse('https://fcm.googleapis.com/fcm/send'), + headers: { + 'Content-Type': 'application/json; charset=UTF-8', + 'Authorization': + 'key=AAAAORdR-xU:APA91bF_wblg86jHAC-uexrXPHavYRlk5wge1Gf46m56V4J2D2L37Cp_hf46JZUzpvsWPSpqc5ewHelKI9LTifUG_s2mciMI6e5VLKo7E1R8btbNo7iaM9do2ctoyHKUm1atlZBdKaN2', + }, + body: jsonEncode( + { + 'notification': { + 'body': accion == 'rechazo' + ? '$proName a rechazado tu solicitud de servicio' + : '$proName a aprobado tu solicitud de servicio', + 'title': '$proName $accion', + }, + 'priority': 'high', + 'data': { + 'click_action': 'FLUTTER_NOTIFICATION_CLICK', + 'id': '1', + 'status': 'done' + }, + 'to': user + }, + ), + ); + + response; + } catch (e) { + print('error al enviar notificacion $e'); + } + } + Future _openMap(double lat, double lng) async { final Uri _url = Uri.parse('https://www.google.com/maps/search/?api=1&query=$lat,$lng'); if (!await launchUrl(_url)) { - print('hola'); throw Exception('Could not launch $_url'); } } @@ -65,6 +106,14 @@ class _CitaScreenState extends State { void initState() { super.initState(); + final uid = AuthenticationRepository.instance.getCurrentUserUid(); + _auth = FirebaseAuth.instance; + + final currentUser = _auth.currentUser; + if (currentUser != null && currentUser.displayName != null) { + proName = currentUser.displayName!; + } + if (settings == null) { SettingModel.getSettings().then( (SettingModel value) => setState(() { @@ -104,6 +153,7 @@ class _CitaScreenState extends State { setState(() { nombre = value.name; ref_photo = value.photo; + userToken = value.token ?? ''; numberPhone = value.phoneNumber ?? ''; }); }); @@ -584,6 +634,10 @@ class _CitaScreenState extends State { .doc('${widget.evento.id}') .update({"status": "denegado"}).then( (value) { + if (userToken != '') { + sendPushNotification( + userToken, 'rechazo', proName); + } Navigator.pushReplacementNamed( context, '/solicitud'); }); @@ -625,6 +679,10 @@ class _CitaScreenState extends State { .doc('${widget.evento.id}') .update({"status": "aprobado"}).then( (value) { + if (userToken != '') { + sendPushNotification( + userToken, 'acepto', proName); + } Navigator.pushReplacementNamed( context, '/solicitud'); }); @@ -654,6 +712,10 @@ class _CitaScreenState extends State { .collection("services") .doc('${widget.evento.id}') .update({"status": "denegado"}).then((value) { + if (userToken != '') { + sendPushNotification( + userToken, 'rechazo', proName); + } Navigator.pushReplacementNamed( context, '/solicitud'); }); diff --git a/lib/src/screens/city.dart b/lib/src/screens/city.dart index c3fcd35..947bdac 100644 --- a/lib/src/screens/city.dart +++ b/lib/src/screens/city.dart @@ -62,7 +62,7 @@ Future> getCountries() async { } } } catch (e) { - print('xd seqe $e'); + print('$e'); } return citys; @@ -116,8 +116,6 @@ class _CityScreenState extends State { .collection('users') .doc(uid) .update({'coordsOfCity': coordsCity}); - - print('Ciudad actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance diff --git a/lib/src/screens/horario.dart b/lib/src/screens/horario.dart index 7770304..1fa7474 100644 --- a/lib/src/screens/horario.dart +++ b/lib/src/screens/horario.dart @@ -44,7 +44,6 @@ class HorarioScreen extends StatelessWidget { await FirebaseFirestore.instance.collection('users').doc(uid).update({ 'horario': horariosMap, }); - print('Horarios actualizados correctamente'); } catch (e) { print('Error al actualizar el horario: $e'); } diff --git a/lib/src/screens/my_services.dart b/lib/src/screens/my_services.dart index 5462ea5..8b1da12 100644 --- a/lib/src/screens/my_services.dart +++ b/lib/src/screens/my_services.dart @@ -70,7 +70,6 @@ class _MyServicesScreenState extends State { event.id = element.id; eventos.add(event); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e'); diff --git a/lib/src/screens/my_services_pro.dart b/lib/src/screens/my_services_pro.dart index 21ad0a8..acf1e3e 100644 --- a/lib/src/screens/my_services_pro.dart +++ b/lib/src/screens/my_services_pro.dart @@ -65,7 +65,6 @@ class _MyServicesProScreenState extends State { event.id = element.id; eventos.add(event); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e'); diff --git a/lib/src/screens/profession.dart b/lib/src/screens/profession.dart index bcf5ff7..07be18d 100644 --- a/lib/src/screens/profession.dart +++ b/lib/src/screens/profession.dart @@ -22,7 +22,7 @@ Future> getProfessions() async { return professionsList; } catch (e) { - print('xd $e'); + print('$e'); } return []; @@ -76,8 +76,6 @@ class _ProfessionScreenState extends State { .collection('users') .doc(uid) .update({'profesion': profession}); - - print('Profesion actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance diff --git a/lib/src/screens/professional_direccion.dart b/lib/src/screens/professional_direccion.dart index 31e52f5..e016181 100644 --- a/lib/src/screens/professional_direccion.dart +++ b/lib/src/screens/professional_direccion.dart @@ -94,8 +94,6 @@ class _ProfessionalDireccionScreenState 'latitude': latitude, 'longitude': longitude, }); - - print('Ciudad actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance.collection('users').doc(uid).set({ diff --git a/lib/src/screens/professional_profile.dart b/lib/src/screens/professional_profile.dart index 3efa1d0..b8914e8 100644 --- a/lib/src/screens/professional_profile.dart +++ b/lib/src/screens/professional_profile.dart @@ -212,10 +212,8 @@ class ProfessionalProfileScreenState extends State { if (userSnapshot.exists) { await userRef.update({'imgCedula': image}); - print('¡Imagen de cédula actualizada correctamente!'); } else { await userRef.set({'imgCedula': image}); - print('¡Imagen de cédula agregada correctamente!'); } } catch (e) { print('Error al agregar o actualizar la imagen de cédula: $e'); @@ -257,10 +255,8 @@ class ProfessionalProfileScreenState extends State { if (userSnapshot.exists) { await userRef.update({'imgCertificado': image}); - print('¡Imagen de certificado actualizada correctamente!'); } else { await userRef.set({'imgCertificado': image}); - print('¡Imagen de certificado agregada correctamente!'); } } catch (e) { print('Error al agregar o actualizar la imagen de certificado: $e'); @@ -273,8 +269,6 @@ class ProfessionalProfileScreenState extends State { .collection('users') .doc(uid) .update({'photo': image}); - - print('imagen actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance @@ -393,10 +387,8 @@ class ProfessionalProfileScreenState extends State { if (userSnapshot.exists) { await userRef.update({'imgEspecializaciones': photoPaths}); - print('¡Imágenes de especializaciones actualizadas correctamente!'); } else { await userRef.set({'imgEspecializaciones': photoPaths}); - print('¡Imágenes de especializaciones agregadas correctamente!'); } } catch (e) { print( diff --git a/lib/src/screens/professional_profile_web.dart b/lib/src/screens/professional_profile_web.dart index fd3cdd6..2b84b48 100644 --- a/lib/src/screens/professional_profile_web.dart +++ b/lib/src/screens/professional_profile_web.dart @@ -347,10 +347,8 @@ class _ProfessionalProfileWebScreenState if (userSnapshot.exists) { await userRef.update({'imgCedula': image}); - print('¡Imagen de cédula actualizada correctamente!'); } else { await userRef.set({'imgCedula': image}); - print('¡Imagen de cédula agregada correctamente!'); } } catch (e) { print('Error al agregar o actualizar la imagen de cédula: $e'); @@ -364,10 +362,8 @@ class _ProfessionalProfileWebScreenState if (userSnapshot.exists) { await userRef.update({'imgCertificado': image}); - print('¡Imagen de certificado actualizada correctamente!'); } else { await userRef.set({'imgCertificado': image}); - print('¡Imagen de certificado agregada correctamente!'); } } catch (e) { print('Error al agregar o actualizar la imagen de certificado: $e'); @@ -380,8 +376,6 @@ class _ProfessionalProfileWebScreenState .collection('users') .doc(uid) .update({'photo': image}); - - print('imagen actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance @@ -403,14 +397,9 @@ class _ProfessionalProfileWebScreenState if (userSnapshot.exists) { await userRef.update({'imgEspecializaciones': filePaths}); - - print('¡Imágenes de especializaciones actualizadas correctamente!'); } else { await userRef.set({'imgEspecializaciones': filePaths}); - print('¡Imágenes de especializaciones agregadas correctamente!'); } - - print('Archivos de especializaciones actualizados correctamente'); } catch (e) { print('Error al actualizar los archivos de especializaciones: $e'); } diff --git a/lib/src/screens/profile.dart b/lib/src/screens/profile.dart index a5dac7a..08abba6 100644 --- a/lib/src/screens/profile.dart +++ b/lib/src/screens/profile.dart @@ -87,7 +87,6 @@ class _ProfileScreenState extends State { .doc(uid) .update({'photo': image}); - print('imagen actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance @@ -246,7 +245,6 @@ class _ProfileScreenState extends State { 'name': newName, 'lowerName': newName.toLowerCase(), }); - print('Nombre actualizado correctamente'); } catch (e) { print('Error al actualizar el nombre: $e'); } diff --git a/lib/src/screens/profile_pro.dart b/lib/src/screens/profile_pro.dart index e752463..133e416 100644 --- a/lib/src/screens/profile_pro.dart +++ b/lib/src/screens/profile_pro.dart @@ -303,8 +303,6 @@ class _ProfileProScreenState extends State { .collection('users') .doc(uid) .update({'banner': image}); - - print('imagen actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance diff --git a/lib/src/screens/profile_pro_web.dart b/lib/src/screens/profile_pro_web.dart index 20e9ccf..0b045d6 100644 --- a/lib/src/screens/profile_pro_web.dart +++ b/lib/src/screens/profile_pro_web.dart @@ -192,8 +192,6 @@ class _ProfileProWebScreenState extends State { .collection('users') .doc(uid) .update({'banner': image}); - - print('imagen actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance diff --git a/lib/src/screens/profile_web.dart b/lib/src/screens/profile_web.dart index 28cd5fc..59a94e8 100644 --- a/lib/src/screens/profile_web.dart +++ b/lib/src/screens/profile_web.dart @@ -153,7 +153,6 @@ class _ProfileWebScreenState extends State { 'name': newName, 'lowerName': newName.toLowerCase(), }); - print('Nombre actualizado correctamente'); } catch (e) { print('Error al actualizar el nombre: $e'); } @@ -167,17 +166,15 @@ class _ProfileWebScreenState extends State { ); return; } - if (currentUser?.displayName != newName) { - try { - await FirebaseAuth.instance.currentUser!.updateDisplayName(newName); - await FirebaseFirestore.instance.collection('users').doc(uid).update({ - 'name': newName, - 'lowerName': newName.toLowerCase(), - }); - print('Nombre actualizado correctamente'); - } catch (e) { - print('Error al actualizar el nombre: $e'); - } + + try { + await FirebaseAuth.instance.currentUser!.updateDisplayName(newName); + await FirebaseFirestore.instance.collection('users').doc(uid).update({ + 'name': newName, + 'lowerName': newName.toLowerCase(), + }); + } catch (e) { + print('Error al actualizar el nombre: $e'); } if (currentUser?.email != newEmail) { @@ -202,7 +199,6 @@ class _ProfileWebScreenState extends State { await FirebaseFirestore.instance.collection('users').doc(uid).update({ 'city': selectedCity, }); - print('Ciudad actualizada correctamente'); } catch (e) { print('Error al actualizar la ciudad: $e'); } @@ -243,8 +239,6 @@ class _ProfileWebScreenState extends State { .collection('users') .doc(uid) .update({'photo': image}); - - print('imagen actualizada correctamente'); } catch (e) { try { await FirebaseFirestore.instance diff --git a/lib/src/screens/service.dart b/lib/src/screens/service.dart index f24f42c..ae2abd0 100644 --- a/lib/src/screens/service.dart +++ b/lib/src/screens/service.dart @@ -906,7 +906,6 @@ class _ServiceScreenState extends State { }, ), ), - const SizedBox(height: 15), Row( children: [ diff --git a/lib/src/screens/solicitudes.dart b/lib/src/screens/solicitudes.dart index 1feca14..0aead74 100644 --- a/lib/src/screens/solicitudes.dart +++ b/lib/src/screens/solicitudes.dart @@ -58,7 +58,6 @@ class SolicitudScreen extends StatelessWidget { event.id = element.id; eventos.add(event); } - print('eventos $eventos'); return eventos; } catch (e) { print('Error getByProId $e');