// lib/services/push_service.dart import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter_local_notifications/flutter_local_notifications.dart'; class PushService { final _fcm = FirebaseMessaging.instance; final _local = FlutterLocalNotificationsPlugin(); final _channelId = 'ride_updates'; Future init() async { // 1) Request permissions await _fcm.requestPermission(); // 2) Create Android notification channel const androidChannel = AndroidNotificationChannel( 'ride_updates', 'Ride Updates', importance: Importance.max); await _local .resolvePlatformSpecificImplementation< AndroidFlutterLocalNotificationsPlugin>() ?.createNotificationChannel(androidChannel); // 3) Initialize plugin await _local.initialize( const InitializationSettings( android: AndroidInitializationSettings('@mipmap/ic_launcher'), iOS: DarwinInitializationSettings(), ), ); // 4) Listen to FCM FirebaseMessaging.onMessage.listen(_showLocalNotification); FirebaseMessaging.onBackgroundMessage(_backgroundHandler); } Future _showLocalNotification(RemoteMessage msg) async { final title = msg.data['title'] ?? 'Ride Update'; final body = msg.data['body'] ?? ''; await _local.show( msg.hashCode, title, body, NotificationDetails( android: AndroidNotificationDetails( _channelId, 'Ride Updates', icon: '@mipmap/ic_launcher', ), ), payload: msg.data['rideId'], ); } static Future _backgroundHandler(RemoteMessage msg) async { await PushService().init(); await PushService()._showLocalNotification(msg); } Future showLocalNotification({ required String title, required String body, String? payload, }) => _local.show( body.hashCode, title, body, NotificationDetails( android: AndroidNotificationDetails( _channelId, 'Ride Updates', icon: '@mipmap/ic_launcher', ), ), payload: payload, ); }