Sync update: 2026-05-18 15:45:06
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:firebase_messaging/firebase_messaging.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart';
|
||||
@@ -15,7 +16,8 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
|
||||
|
||||
class FirebaseService extends GetxService {
|
||||
final FirebaseMessaging _messaging = FirebaseMessaging.instance;
|
||||
final FlutterLocalNotificationsPlugin _localNotifications = FlutterLocalNotificationsPlugin();
|
||||
final FlutterLocalNotificationsPlugin _localNotifications =
|
||||
FlutterLocalNotificationsPlugin();
|
||||
|
||||
Future<FirebaseService> init() async {
|
||||
// 1. Request Permission
|
||||
@@ -28,8 +30,9 @@ class FirebaseService extends GetxService {
|
||||
// 2. Initialize Local Notifications (for foreground)
|
||||
const androidInit = AndroidInitializationSettings('@mipmap/ic_launcher');
|
||||
const iosInit = DarwinInitializationSettings();
|
||||
const initSettings = InitializationSettings(android: androidInit, iOS: iosInit);
|
||||
|
||||
const initSettings =
|
||||
InitializationSettings(android: androidInit, iOS: iosInit);
|
||||
|
||||
await _localNotifications.initialize(
|
||||
initSettings,
|
||||
onDidReceiveNotificationResponse: _onNotificationTap,
|
||||
@@ -65,9 +68,48 @@ class FirebaseService extends GetxService {
|
||||
});
|
||||
}
|
||||
|
||||
// 7. Request and register FCM Token on the server
|
||||
_setupFcmTokenRegistration();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
void _setupFcmTokenRegistration() async {
|
||||
try {
|
||||
final token = await _messaging.getToken();
|
||||
if (token != null) {
|
||||
print('[FCM] Token obtained: ${token.substring(0, 15)}...');
|
||||
_registerTokenOnServer(token);
|
||||
}
|
||||
} catch (e) {
|
||||
print('[FCM ERROR] Could not get token: $e');
|
||||
}
|
||||
|
||||
_messaging.onTokenRefresh.listen((newToken) {
|
||||
print('[FCM] Token refreshed');
|
||||
_registerTokenOnServer(newToken);
|
||||
});
|
||||
}
|
||||
|
||||
void _registerTokenOnServer(String token) {
|
||||
final wsSvc = Get.find<WhatsAppService>();
|
||||
if (wsSvc.status.value == WsStatus.waReady ||
|
||||
wsSvc.status.value == WsStatus.connected) {
|
||||
print('[FCM] Sending token to server...');
|
||||
wsSvc.sendFcmToken(token);
|
||||
} else {
|
||||
// Listen to status changes and send once connected
|
||||
late StreamSubscription sub;
|
||||
sub = wsSvc.status.listen((status) {
|
||||
if (status == WsStatus.waReady || status == WsStatus.connected) {
|
||||
print('[FCM] WebSocket connected, sending token to server...');
|
||||
wsSvc.sendFcmToken(token);
|
||||
sub.cancel();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void _showLocalNotification(RemoteMessage message) {
|
||||
final notification = message.notification;
|
||||
if (notification == null) return;
|
||||
@@ -80,7 +122,8 @@ class FirebaseService extends GetxService {
|
||||
ticker: 'ticker',
|
||||
);
|
||||
const iosDetails = DarwinNotificationDetails();
|
||||
const details = NotificationDetails(android: androidDetails, iOS: iosDetails);
|
||||
const details =
|
||||
NotificationDetails(android: androidDetails, iOS: iosDetails);
|
||||
|
||||
_localNotifications.show(
|
||||
notification.hashCode,
|
||||
@@ -101,7 +144,7 @@ class FirebaseService extends GetxService {
|
||||
void _handleNotificationClick(Map<String, dynamic> data) {
|
||||
final chatId = data['chatId'];
|
||||
final name = data['name'] ?? 'Chat';
|
||||
|
||||
|
||||
if (chatId != null) {
|
||||
// Mock a conversation model to navigate to ChatScreen
|
||||
final dummyChat = ConversationModel(
|
||||
@@ -113,7 +156,7 @@ class FirebaseService extends GetxService {
|
||||
pinned: false,
|
||||
isMuted: false,
|
||||
);
|
||||
|
||||
|
||||
Get.to(() => ChatScreen(conversation: dummyChat));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user