notificaciones

This commit is contained in:
Juan Felipe Duarte
2023-06-23 10:39:10 -05:00
parent fa1b373d5e
commit 90ec1293ea
12 changed files with 116 additions and 39 deletions
+6 -4
View File
@@ -10,14 +10,15 @@ class UserModel {
final Reference? photo;
final int? tarifa;
final String? phoneNumber;
final String? token;
UserModel(this.name, this.city, this.profession, this.state, this.photo,
this.tarifa, this.phoneNumber);
this.tarifa, this.phoneNumber, this.token);
static Future<UserModel> fromJson(
Map<String, dynamic>? json, String uid) async {
try {
if (json == null) return UserModel('', '', '', null, null, 0, '');
if (json == null) return UserModel('', '', '', null, null, 0, '', '');
String? avatar = json['photo'];
return UserModel(
@@ -28,10 +29,11 @@ class UserModel {
avatar != null ? storage.ref().child(avatar) : null,
json['tarifa'] ?? 0,
json['phoneNumber'],
json['token'],
);
} catch (e) {
print('XD user $e');
return UserModel('', '', '', null, null, 0, '');
return UserModel('', '', '', null, null, 0, '', '');
}
}
@@ -43,7 +45,7 @@ class UserModel {
return fromJson(data, uid);
} catch (e) {
print('Error getting user: $e');
return UserModel('', '', '', null, null, 0, '');
return UserModel('', '', '', null, null, 0, '', '');
}
}
}
+1 -1
View File
@@ -99,7 +99,7 @@ class _CalendarScreenState extends State<CalendarScreen> {
return Scaffold(
floatingActionButtonLocation: kIsWeb
? FloatingActionButtonLocation.startFloat
: FloatingActionButtonLocation.startFloat,
: FloatingActionButtonLocation.endFloat,
resizeToAvoidBottomInset: false,
appBar: PopAppbar(
onPressed: () {
+17 -12
View File
@@ -40,6 +40,7 @@ class Professional {
final List<String> professionalEspecializado;
final ScoresModel scores;
final int? tarifa;
final String? token;
Professional({
required this.id,
@@ -54,6 +55,7 @@ class Professional {
required this.latitude,
required this.longitude,
this.tarifa,
this.token,
});
String getEspecializaciones() {
@@ -96,18 +98,20 @@ class Professional {
.toList();
Professional professional = Professional(
id: user.id,
name: data['name'],
professionName: data['profesion'],
cityName: data['city'],
professionalRef: storage.ref().child(photo),
professionalEspecializado: especializaciones,
ubicacion: data['ubicacion'] ?? '',
realAddress: data['address'] ?? '',
latitude: data['latitude'] ?? 0,
longitude: data['longitude'] ?? 0,
scores: await ScoresModel.scoreFrom(uid, true, true),
tarifa: data['tarifa'] ?? 0);
id: user.id,
name: data['name'],
professionName: data['profesion'],
cityName: data['city'],
professionalRef: storage.ref().child(photo),
professionalEspecializado: especializaciones,
ubicacion: data['ubicacion'] ?? '',
realAddress: data['address'] ?? '',
latitude: data['latitude'] ?? 0,
longitude: data['longitude'] ?? 0,
scores: await ScoresModel.scoreFrom(uid, true, true),
tarifa: data['tarifa'] ?? 0,
token: data['token'] ?? '',
);
return professional;
}
} catch (e) {
@@ -252,6 +256,7 @@ class _ProfessionalScreenState extends State<ProfessionalScreen> {
longitude: data['longitude'] ?? 0,
scores: await ScoresModel.scoreTo(user.id, true, true),
tarifa: data['tarifa'] ?? 0,
token: data['token'] ?? '',
);
professionals.add(professional);
}
+50
View File
@@ -1,3 +1,5 @@
import 'dart:convert';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:firebase_storage/firebase_storage.dart';
@@ -19,6 +21,7 @@ import 'package:prosappco/src/screens/profile.dart';
import 'package:prosappco/src/screens/service_after.dart';
import 'package:prosappco/src/screens/service_type.dart';
import 'package:prosappco/src/screens/ubicacion.dart';
import 'package:http/http.dart' as http;
class ServiceScreen extends StatefulWidget {
const ServiceScreen({super.key});
@@ -43,6 +46,40 @@ class _ServiceScreenState extends State<ServiceScreen> {
}
}
Future<void> sendPushNotification(String token) async {
try {
http.Response response = await http.post(
Uri.parse('https://fcm.googleapis.com/fcm/send'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
'Authorization':
'key=AAAAORdR-xU:APA91bF_wblg86jHAC-uexrXPHavYRlk5wge1Gf46m56V4J2D2L37Cp_hf46JZUzpvsWPSpqc5ewHelKI9LTifUG_s2mciMI6e5VLKo7E1R8btbNo7iaM9do2ctoyHKUm1atlZBdKaN2',
},
body: jsonEncode(
<String, dynamic>{
'notification': <String, dynamic>{
'body': 'alguien a solicitado tus servicios',
'title': 'Nueva solicitud',
},
'priority': 'high',
'data': <String, dynamic>{
'click_action': 'FLUTTER_NOTIFICATION_CLICK',
'id': '1',
'status': 'done'
},
'to': token,
},
),
);
print(token);
print(response);
response;
} catch (e) {
print('error al enviar notificacion $e');
}
}
EventoService eventoService = EventoService();
String _locationPosition = '';
String ubicacion = '';
@@ -52,6 +89,7 @@ class _ServiceScreenState extends State<ServiceScreen> {
final TextEditingController _serviceTypeController = TextEditingController();
final TextEditingController _observacionController = TextEditingController();
String professionalId = '';
String professionalToken = '';
int? professionalTarifa;
String professionalAddress = '';
String professionalUbicacion = '';
@@ -453,6 +491,9 @@ class _ServiceScreenState extends State<ServiceScreen> {
false,
)
.then((value) {
if (professionalToken != '') {
sendPushNotification(professionalToken);
}
Navigator.pushReplacement(
context,
CupertinoPageRoute(
@@ -712,6 +753,11 @@ class _ServiceScreenState extends State<ServiceScreen> {
professionalId = profesional.id;
if (profesional.token != '') {
professionalToken =
profesional.token!;
}
professionalTarifa =
profesional.tarifa ?? 0;
@@ -914,6 +960,10 @@ class _ServiceScreenState extends State<ServiceScreen> {
false,
)
.then((value) {
if (professionalToken != '') {
sendPushNotification(
professionalToken);
}
Navigator.pushReplacement(
context,
CupertinoPageRoute(