Skip to content

Commit

Permalink
Adicionar tratamento de desconexão no socket e inicializar o serviço …
Browse files Browse the repository at this point in the history
…de notificações no main.dart
  • Loading branch information
Polabiel committed Sep 16, 2024
1 parent 525ba27 commit 99a74a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
21 changes: 10 additions & 11 deletions lib/api/services/socket_client_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,34 @@ void onStart(ServiceInstance service) async {
socket.onConnect((_) async {
print('Connected. Socket ID: ${socket.id}');
var user = await SharedLocalStorageService().getProfile();

if (user != null && user.id != null) {
socket.emit("id", user.id);
} else {
return;
}
});

// Listener para o evento 'productAvailable'
socket.onDisconnect((_) {
print('Disconnected');
});

socket.on('productAvailable', (data) async {
print(data);

final product = await ProductRepository().getbyId(data['productId']);
final pharmacy = await PharmacyRepository().getPharmacyById(data['pharmacyId']);
final pharmacy =
await PharmacyRepository().getPharmacyById(data['pharmacyId']);
print('Produto disponível: $data');

// Enviar uma notificação para o usuário mesmo inativo ou ativo
NotificationService.showNotification(
await NotificationService.showNotification(
0, // ID da notificação
'Produto Disponível', // Título da notificação
'Um novo produto está disponível: ${product.name} na farmácia ${pharmacy.name}', // Corpo da notificação
);
});

socket.onDisconnect((_) {
print('Disconnected');
});

socket.on("stock", (data) {
print(data);
});

service.on("stop").listen((event) {
service.stopSelf();
print("background process is now stopped");
Expand Down
2 changes: 2 additions & 0 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import 'package:flutter/material.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:gohealth/api/services/notification_client_service.dart';
import 'package:gohealth/src/app/app_page.dart';
import 'package:gohealth/api/services/socket_client_service.dart';
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await dotenv.load(fileName: ".env");
NotificationService.initialize();
await initializeService();
runApp(const App());
}

0 comments on commit 99a74a0

Please sign in to comment.