116 lines
3.2 KiB
Dart
116 lines
3.2 KiB
Dart
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));
|
|
}
|
|
}
|