From 90ec1293ea8a25c255faf048bd66d7fba4ab61b5 Mon Sep 17 00:00:00 2001 From: Juan Felipe Duarte <70235683+DuarteJFelipe@users.noreply.github.com> Date: Fri, 23 Jun 2023 10:39:10 -0500 Subject: [PATCH] notificaciones --- android/app/build.gradle | 2 +- lib/src/models/user_model.dart | 10 ++-- lib/src/screens/calendar.dart | 2 +- lib/src/screens/professional.dart | 29 ++++++----- lib/src/screens/service.dart | 50 +++++++++++++++++++ linux/flutter/generated_plugin_registrant.cc | 4 ++ linux/flutter/generated_plugins.cmake | 1 + macos/Flutter/GeneratedPluginRegistrant.swift | 2 + pubspec.lock | 50 +++++++++++-------- pubspec.yaml | 1 + .../flutter/generated_plugin_registrant.cc | 3 ++ windows/flutter/generated_plugins.cmake | 1 + 12 files changed, 116 insertions(+), 39 deletions(-) diff --git a/android/app/build.gradle b/android/app/build.gradle index d2de8c0..1d327db 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -50,7 +50,7 @@ android { // You can update the following values to match your application needs. // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration. minSdkVersion "21".toInteger() - targetSdkVersion flutter.targetSdkVersion + targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName } diff --git a/lib/src/models/user_model.dart b/lib/src/models/user_model.dart index e1ee4c9..9939061 100644 --- a/lib/src/models/user_model.dart +++ b/lib/src/models/user_model.dart @@ -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 fromJson( Map? 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, '', ''); } } } diff --git a/lib/src/screens/calendar.dart b/lib/src/screens/calendar.dart index 80fc12a..12cf391 100644 --- a/lib/src/screens/calendar.dart +++ b/lib/src/screens/calendar.dart @@ -99,7 +99,7 @@ class _CalendarScreenState extends State { return Scaffold( floatingActionButtonLocation: kIsWeb ? FloatingActionButtonLocation.startFloat - : FloatingActionButtonLocation.startFloat, + : FloatingActionButtonLocation.endFloat, resizeToAvoidBottomInset: false, appBar: PopAppbar( onPressed: () { diff --git a/lib/src/screens/professional.dart b/lib/src/screens/professional.dart index f280fd1..3d72f1a 100644 --- a/lib/src/screens/professional.dart +++ b/lib/src/screens/professional.dart @@ -40,6 +40,7 @@ class Professional { final List 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 { longitude: data['longitude'] ?? 0, scores: await ScoresModel.scoreTo(user.id, true, true), tarifa: data['tarifa'] ?? 0, + token: data['token'] ?? '', ); professionals.add(professional); } diff --git a/lib/src/screens/service.dart b/lib/src/screens/service.dart index e4b3f83..5040fbb 100644 --- a/lib/src/screens/service.dart +++ b/lib/src/screens/service.dart @@ -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 { } } + Future sendPushNotification(String token) 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': 'alguien a solicitado tus servicios', + 'title': 'Nueva solicitud', + }, + 'priority': 'high', + 'data': { + '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 { 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 { false, ) .then((value) { + if (professionalToken != '') { + sendPushNotification(professionalToken); + } Navigator.pushReplacement( context, CupertinoPageRoute( @@ -712,6 +753,11 @@ class _ServiceScreenState extends State { professionalId = profesional.id; + if (profesional.token != '') { + professionalToken = + profesional.token!; + } + professionalTarifa = profesional.tarifa ?? 0; @@ -914,6 +960,10 @@ class _ServiceScreenState extends State { false, ) .then((value) { + if (professionalToken != '') { + sendPushNotification( + professionalToken); + } Navigator.pushReplacement( context, CupertinoPageRoute( diff --git a/linux/flutter/generated_plugin_registrant.cc b/linux/flutter/generated_plugin_registrant.cc index 7299b5c..0f5e8dc 100644 --- a/linux/flutter/generated_plugin_registrant.cc +++ b/linux/flutter/generated_plugin_registrant.cc @@ -6,10 +6,14 @@ #include "generated_plugin_registrant.h" +#include #include #include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) awesome_notifications_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "AwesomeNotificationsPlugin"); + awesome_notifications_plugin_register_with_registrar(awesome_notifications_registrar); g_autoptr(FlPluginRegistrar) file_selector_linux_registrar = fl_plugin_registry_get_registrar_for_plugin(registry, "FileSelectorPlugin"); file_selector_plugin_register_with_registrar(file_selector_linux_registrar); diff --git a/linux/flutter/generated_plugins.cmake b/linux/flutter/generated_plugins.cmake index 786ff5c..3b861be 100644 --- a/linux/flutter/generated_plugins.cmake +++ b/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + awesome_notifications file_selector_linux url_launcher_linux ) diff --git a/macos/Flutter/GeneratedPluginRegistrant.swift b/macos/Flutter/GeneratedPluginRegistrant.swift index 1568684..412a16d 100644 --- a/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/macos/Flutter/GeneratedPluginRegistrant.swift @@ -5,6 +5,7 @@ import FlutterMacOS import Foundation +import awesome_notifications import cloud_firestore import file_selector_macos import firebase_auth @@ -16,6 +17,7 @@ import geolocator_apple import url_launcher_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { + AwesomeNotificationsPlugin.register(with: registry.registrar(forPlugin: "AwesomeNotificationsPlugin")) FLTFirebaseFirestorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseFirestorePlugin")) FileSelectorPlugin.register(with: registry.registrar(forPlugin: "FileSelectorPlugin")) FLTFirebaseAuthPlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseAuthPlugin")) diff --git a/pubspec.lock b/pubspec.lock index 941eb1c..fd50ffb 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -25,6 +25,14 @@ packages: url: "https://pub.dev" source: hosted version: "2.11.0" + awesome_notifications: + dependency: "direct main" + description: + name: awesome_notifications + sha256: "2b430c75cc879d6cfd52bb6eb2b5c1591ed425347816408cdcbd3f6916bba14c" + url: "https://pub.dev" + source: hosted + version: "0.7.4+1" boolean_selector: dependency: transitive description: @@ -141,10 +149,10 @@ packages: dependency: "direct main" description: name: diacritic - sha256: c09a420e737dc036122121fea9f774767e95068f34a17894ee9db30c5bda3075 + sha256: a84e03ec2779375fb86430dbe9d8fba62c68376f2499097a5f6e75556babe706 url: "https://pub.dev" source: hosted - version: "0.1.3" + version: "0.1.4" fake_async: dependency: transitive description: @@ -197,10 +205,10 @@ packages: dependency: transitive description: name: file_selector_platform_interface - sha256: "2a7f4bbf7bd2f022ecea85bfb1754e87f7dd403a9abc17a84a4fa2ddfe2abc0a" + sha256: "412705a646a0ae90f33f37acfae6a0f7cbc02222d6cd34e479421c3e74d3853c" url: "https://pub.dev" source: hosted - version: "2.5.1" + version: "2.6.0" file_selector_windows: dependency: transitive description: @@ -314,10 +322,10 @@ packages: dependency: "direct main" description: name: flutter_animate - sha256: f611a67082d4c5ff9b8b7737ec7e675a22af70a94c8c3c22109f14b078d4d2bf + sha256: be54662837a6e66cc53ee88549e808c625275e0faf5a43e11cf3182cb0bd1b02 url: "https://pub.dev" source: hosted - version: "4.1.1+1" + version: "4.2.0" flutter_dialogs: dependency: "direct main" description: @@ -516,10 +524,10 @@ packages: dependency: "direct main" description: name: google_maps_flutter - sha256: "7b417a64ee7a060f42cf44d8c274d3b562423f6fe57d2911b7b536857c0d8eb6" + sha256: "7e35644d8a88ad86409976db8fa23ddc7d933f8239e57405e4660534be09acd2" url: "https://pub.dev" source: hosted - version: "2.3.0" + version: "2.3.1" google_maps_flutter_android: dependency: transitive description: @@ -628,10 +636,10 @@ packages: dependency: transitive description: name: image_picker_android - sha256: "216dbfb6609e46f78613b8913d046a162588ad4f34909ae7bf85e9f94ea6d922" + sha256: d2bab152deb2547ea6f53d82ebca9b7e77386bb706e5789e815d37e08ea475bb url: "https://pub.dev" source: hosted - version: "0.8.7" + version: "0.8.7+3" image_picker_for_web: dependency: transitive description: @@ -684,10 +692,10 @@ packages: dependency: "direct main" description: name: intl - sha256: "3bc132a9dbce73a7e4a21a17d06e1878839ffbf975568bc875c60537824b0c4d" + sha256: "910f85bce16fb5c6f614e117efa303e85a1731bb0081edf3604a2ae6e9a3cc91" url: "https://pub.dev" source: hosted - version: "0.18.1" + version: "0.17.0" intl_phone_field: dependency: "direct main" description: @@ -889,10 +897,10 @@ packages: dependency: "direct main" description: name: table_calendar - sha256: "1e3521a3e6d3fc7f645a58b135ab663d458ab12504f1ea7f9b4b81d47086c478" + sha256: "7f1270313c0cdb245b583ed8518982c01d4a7e95869b3c30abcbae3b642c45d0" url: "https://pub.dev" source: hosted - version: "3.0.9" + version: "3.0.8" term_glyph: dependency: transitive description: @@ -953,10 +961,10 @@ packages: dependency: transitive description: name: url_launcher_android - sha256: eed4e6a1164aa9794409325c3b707ff424d4d1c2a785e7db67f8bbda00e36e51 + sha256: "15f5acbf0dce90146a0f5a2c4a002b1814a6303c4c5c075aa2623b2d16156f03" url: "https://pub.dev" source: hosted - version: "6.0.35" + version: "6.0.36" url_launcher_ios: dependency: transitive description: @@ -985,10 +993,10 @@ packages: dependency: transitive description: name: url_launcher_platform_interface - sha256: "6c9ca697a5ae218ce56cece69d46128169a58aa8653c1b01d26fcd4aad8c4370" + sha256: bfdfa402f1f3298637d71ca8ecfe840b4696698213d5346e9d12d4ab647ee2ea url: "https://pub.dev" source: hosted - version: "2.1.2" + version: "2.1.3" url_launcher_web: dependency: transitive description: @@ -1057,10 +1065,10 @@ packages: dependency: transitive description: name: win32 - sha256: "7dacfda1edcca378031db9905ad7d7bd56b29fd1a90b0908b71a52a12c41e36b" + sha256: "1414f27dd781737e51afa9711f2ac2ace6ab4498ee98e20863fa5505aa00c58c" url: "https://pub.dev" source: hosted - version: "5.0.3" + version: "5.0.4" xdg_directories: dependency: transitive description: @@ -1079,4 +1087,4 @@ packages: version: "6.3.0" sdks: dart: ">=3.0.0 <4.0.0" - flutter: ">=3.3.0" + flutter: ">=3.10.0" diff --git a/pubspec.yaml b/pubspec.yaml index 5776f25..8714379 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -67,6 +67,7 @@ dependencies: flutter_dialogs: ^3.0.0 universal_html: ^2.2.3 firebase_messaging: ^14.6.3 + awesome_notifications: ^0.7.4+1 dev_dependencies: flutter_test: diff --git a/windows/flutter/generated_plugin_registrant.cc b/windows/flutter/generated_plugin_registrant.cc index b762e91..d7253b0 100644 --- a/windows/flutter/generated_plugin_registrant.cc +++ b/windows/flutter/generated_plugin_registrant.cc @@ -6,12 +6,15 @@ #include "generated_plugin_registrant.h" +#include #include #include #include #include void RegisterPlugins(flutter::PluginRegistry* registry) { + AwesomeNotificationsPluginCApiRegisterWithRegistrar( + registry->GetRegistrarForPlugin("AwesomeNotificationsPluginCApi")); FileSelectorWindowsRegisterWithRegistrar( registry->GetRegistrarForPlugin("FileSelectorWindows")); FirebaseCorePluginCApiRegisterWithRegistrar( diff --git a/windows/flutter/generated_plugins.cmake b/windows/flutter/generated_plugins.cmake index 4427958..a4eaa73 100644 --- a/windows/flutter/generated_plugins.cmake +++ b/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + awesome_notifications file_selector_windows firebase_core geolocator_windows