notificaciones al pedir servicio y al enviar mensaje de chat
This commit is contained in:
@@ -0,0 +1,115 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:cloud_firestore/cloud_firestore.dart';
|
||||
import 'package:firebase_auth/firebase_auth.dart';
|
||||
import 'package:firebase_core/firebase_core.dart';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:equatable/equatable.dart';
|
||||
import 'package:prosappco/firebase_options.dart';
|
||||
import 'package:prosappco/local_notifications/local_notifications.dart';
|
||||
import 'package:user_repository/user_repository.dart';
|
||||
|
||||
part 'notification_event.dart';
|
||||
part 'notification_state.dart';
|
||||
|
||||
Future<void> firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
await Firebase.initializeApp();
|
||||
}
|
||||
|
||||
class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
|
||||
FirebaseMessaging messaging = FirebaseMessaging.instance;
|
||||
|
||||
final Future<void> Function() requestLocalNotificationPermission;
|
||||
final void Function({
|
||||
required int id,
|
||||
String? title,
|
||||
String? body,
|
||||
String? data,
|
||||
}) showLocalNotification;
|
||||
|
||||
NotificationBloc(
|
||||
{required this.requestLocalNotificationPermission,
|
||||
required this.showLocalNotification})
|
||||
: super(const NotificationState()) {
|
||||
on<NotificationStatusChanged>(_notificationStatusChanged);
|
||||
|
||||
_initialStatusCheck();
|
||||
|
||||
_onForegroundMessage();
|
||||
}
|
||||
|
||||
static Future<void> initializeFCM() async {
|
||||
await Firebase.initializeApp(
|
||||
options: DefaultFirebaseOptions.currentPlatform,
|
||||
);
|
||||
}
|
||||
|
||||
void _notificationStatusChanged(
|
||||
NotificationStatusChanged event, Emitter<NotificationState> emit) {
|
||||
emit(state.copyWith(status: event.status));
|
||||
|
||||
_getFCMToken();
|
||||
}
|
||||
|
||||
void _initialStatusCheck() async {
|
||||
final settings = await messaging.getNotificationSettings();
|
||||
add(NotificationStatusChanged(settings.authorizationStatus));
|
||||
_getFCMToken();
|
||||
}
|
||||
|
||||
Future<void> _saveFCMTokenToFirestore(String token) async {
|
||||
try {
|
||||
CollectionReference users = FirebaseFirestore.instance.collection('users');
|
||||
|
||||
await users.doc(FirebaseAuth.instance.currentUser!.uid).update({
|
||||
'token': token,
|
||||
});
|
||||
} catch (e) {
|
||||
print('tokenFCM Error saving FCM token to Firestore: $e');
|
||||
}
|
||||
}
|
||||
|
||||
void _getFCMToken() async {
|
||||
if (state.status != AuthorizationStatus.authorized) return;
|
||||
|
||||
final fcmToken = await messaging.getToken();
|
||||
log('tokenFCM ${fcmToken.toString()}');
|
||||
|
||||
if (fcmToken == null) return;
|
||||
|
||||
_saveFCMTokenToFirestore(fcmToken);
|
||||
}
|
||||
|
||||
void _handleRemoteMessage(RemoteMessage message) {
|
||||
if (message.notification == null) return;
|
||||
|
||||
showLocalNotification(
|
||||
id: 1,
|
||||
title: message.notification!.title,
|
||||
body: message.notification!.body,
|
||||
);
|
||||
}
|
||||
|
||||
void _onForegroundMessage() {
|
||||
FirebaseMessaging.onMessage.listen(_handleRemoteMessage);
|
||||
}
|
||||
|
||||
void requestPermission() async {
|
||||
NotificationSettings settings = await messaging.requestPermission(
|
||||
alert: true,
|
||||
announcement: false,
|
||||
badge: true,
|
||||
carPlay: false,
|
||||
criticalAlert: false,
|
||||
provisional: false,
|
||||
sound: true,
|
||||
);
|
||||
|
||||
// Solicitar permiso a las local notificaciones
|
||||
|
||||
await requestLocalNotificationPermission();
|
||||
|
||||
add(NotificationStatusChanged(settings.authorizationStatus));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
part of 'notification_bloc.dart';
|
||||
|
||||
abstract class NotificationEvent extends Equatable {
|
||||
const NotificationEvent();
|
||||
|
||||
@override
|
||||
List<Object> get props => [];
|
||||
}
|
||||
|
||||
class NotificationStatusChanged extends NotificationEvent {
|
||||
final AuthorizationStatus status;
|
||||
|
||||
const NotificationStatusChanged(this.status);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
part of 'notification_bloc.dart';
|
||||
|
||||
class NotificationState extends Equatable {
|
||||
final AuthorizationStatus status;
|
||||
|
||||
const NotificationState({
|
||||
this.status = AuthorizationStatus.notDetermined,
|
||||
});
|
||||
|
||||
NotificationState copyWith({
|
||||
AuthorizationStatus? status,
|
||||
}) =>
|
||||
NotificationState(
|
||||
status: status ?? this.status,
|
||||
);
|
||||
|
||||
@override
|
||||
List<Object> get props => [status];
|
||||
}
|
||||
|
||||
class NotificationInitial extends NotificationState {}
|
||||
Reference in New Issue
Block a user