import 'package:firebase_core/firebase_core.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; class FirebaseService { static FirebaseMessaging? _messaging; static Future initialize() async { try { // Initialize Firebase Messaging _messaging = FirebaseMessaging.instance; // Request permission for notifications NotificationSettings settings = await _messaging!.requestPermission( alert: true, announcement: false, badge: true, carPlay: false, criticalAlert: false, provisional: false, sound: true, ); if (settings.authorizationStatus == AuthorizationStatus.authorized) { print('User granted permission'); } else if (settings.authorizationStatus == AuthorizationStatus.provisional) { print('User granted provisional permission'); } else { print('User declined or has not accepted permission'); } // Get FCM token String? token = await _messaging!.getToken(); print('FCM Token: $token'); // Analytics optional: disabled to simplify dependencies // Handle background messages FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); // Handle foreground messages FirebaseMessaging.onMessage.listen(_handleForegroundMessage); // Handle notification taps FirebaseMessaging.onMessageOpenedApp.listen(_handleNotificationTap); } catch (e) { print('Error initializing Firebase: $e'); } } // Send notification to specific topic static Future subscribeToTopic(String topic) async { try { await _messaging?.subscribeToTopic(topic); print('Subscribed to topic: $topic'); } catch (e) { print('Error subscribing to topic: $e'); } } // Unsubscribe from topic static Future unsubscribeFromTopic(String topic) async { try { await _messaging?.unsubscribeFromTopic(topic); print('Unsubscribed from topic: $topic'); } catch (e) { print('Error unsubscribing from topic: $e'); } } // Log custom event static Future logEvent({ required String name, Map? parameters, }) async { // No-op (analytics disabled) return; } // Log news view static Future logNewsView(String newsId, String category) async { await logEvent( name: 'news_view', parameters: { 'news_id': newsId, 'category': category, 'timestamp': DateTime.now().millisecondsSinceEpoch, }, ); } // Log swipe action static Future logSwipe(String direction) async { await logEvent( name: 'news_swipe', parameters: { 'direction': direction, 'timestamp': DateTime.now().millisecondsSinceEpoch, }, ); } // Log ad view static Future logAdView(String adType) async { await logEvent( name: 'ad_view', parameters: { 'ad_type': adType, 'timestamp': DateTime.now().millisecondsSinceEpoch, }, ); } } // Background message handler Future _firebaseMessagingBackgroundHandler(RemoteMessage message) async { // Ensure Firebase is initialized in background isolate try { await Firebase.initializeApp(); } catch (_) {} print('Handling a background message: ${message.messageId}'); // Log the message for analytics FirebaseService.logEvent( name: 'background_message_received', parameters: { 'message_id': message.messageId, 'title': message.notification?.title, 'body': message.notification?.body, }, ); } // Handle foreground message void _handleForegroundMessage(RemoteMessage message) { print('Got a message whilst in the foreground!'); print('Message data: ${message.data}'); if (message.notification != null) { print('Message also contained a notification: ${message.notification}'); // Log the message for analytics FirebaseService.logEvent( name: 'foreground_message_received', parameters: { 'message_id': message.messageId, 'title': message.notification!.title, 'body': message.notification!.body, }, ); } } // Handle notification tap void _handleNotificationTap(RemoteMessage message) { print('Notification tapped: ${message.data}'); // Navigate to appropriate screen based on notification data // This will be implemented in the main app navigation }

MalCare Firewall

Blocked because of Malicious Activities

Reference ID: 98667415068b4360a7cc9b