Last active
November 9, 2025 00:46
-
-
Save YousefMohamed6/f5e1900866070a3ee606889304d0b2ab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import 'dart:async'; | |
| import 'package:firebase_messaging/firebase_messaging.dart'; | |
| import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | |
| @pragma('vm:entry-point') | |
| Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async { | |
| await NotificationService.instance.setupFlutterNotifications(); | |
| await NotificationService.instance.showNotification(message); | |
| } | |
| class NotificationService { | |
| NotificationService._(); | |
| static final NotificationService instance = NotificationService._(); | |
| final _messaging = FirebaseMessaging.instance; | |
| final _localNotifications = FlutterLocalNotificationsPlugin(); | |
| bool _isFlutterLocalNotificationsInitialized = false; | |
| Future<void> initialize() async { | |
| FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); | |
| // Request permission | |
| await _requestPermission(); | |
| // Setup message handlers | |
| await _setupMessageHandlers(); | |
| } | |
| Future<String> getDeviceToken() async { | |
| return await _messaging.getToken() ?? ''; | |
| } | |
| Future<void> _requestPermission() async { | |
| // ignore: unused_local_variable | |
| final settings = await _messaging.requestPermission( | |
| alert: true, | |
| badge: true, | |
| sound: true, | |
| provisional: false, | |
| announcement: false, | |
| carPlay: false, | |
| criticalAlert: false, | |
| ); | |
| } | |
| Future<void> setupFlutterNotifications() async { | |
| if (_isFlutterLocalNotificationsInitialized) { | |
| return; | |
| } | |
| // request permission for ios | |
| await FirebaseMessaging.instance | |
| .setForegroundNotificationPresentationOptions( | |
| alert: true, | |
| badge: true, | |
| sound: true, | |
| ); | |
| await _localNotifications | |
| .resolvePlatformSpecificImplementation< | |
| IOSFlutterLocalNotificationsPlugin | |
| >() | |
| ?.requestPermissions(alert: true, badge: true, sound: true); | |
| // android setup | |
| const channel = AndroidNotificationChannel( | |
| 'high_importance_channel', | |
| 'High Importance Notifications', | |
| description: 'This channel is used for important notifications.', | |
| importance: Importance.high, | |
| ); | |
| await _localNotifications | |
| .resolvePlatformSpecificImplementation< | |
| AndroidFlutterLocalNotificationsPlugin | |
| >() | |
| ?.createNotificationChannel(channel); | |
| const initializationSettingsAndroid = AndroidInitializationSettings( | |
| '@mipmap/launcher_icon', | |
| ); | |
| // ios setup | |
| final initializationSettingsDarwin = DarwinInitializationSettings(); | |
| final initializationSettings = InitializationSettings( | |
| android: initializationSettingsAndroid, | |
| iOS: initializationSettingsDarwin, | |
| ); | |
| // flutter notification setup | |
| await _localNotifications.initialize( | |
| initializationSettings, | |
| onDidReceiveNotificationResponse: (details) {}, | |
| ); | |
| _isFlutterLocalNotificationsInitialized = true; | |
| } | |
| Future<void> showNotification(RemoteMessage message) async { | |
| RemoteNotification? notification = message.notification; | |
| AndroidNotification? android = message.notification?.android; | |
| if (notification != null && android != null) { | |
| await _localNotifications.show( | |
| notification.hashCode, | |
| notification.title, | |
| notification.body, | |
| const NotificationDetails( | |
| android: AndroidNotificationDetails( | |
| 'high_importance_channel', | |
| 'High Importance Notifications', | |
| channelDescription: | |
| 'This channel is used for important notifications.', | |
| importance: Importance.high, | |
| priority: Priority.high, | |
| icon: '@mipmap/launcher_icon', | |
| ), | |
| iOS: DarwinNotificationDetails( | |
| presentAlert: true, | |
| presentBadge: true, | |
| presentSound: true, | |
| ), | |
| ), | |
| payload: message.data.toString(), | |
| ); | |
| } | |
| } | |
| Future<void> _setupMessageHandlers() async { | |
| //foreground message | |
| FirebaseMessaging.onMessage.listen((message) async { | |
| showNotification(message); | |
| }); | |
| // opened app | |
| final initialMessage = await _messaging.getInitialMessage(); | |
| if (initialMessage != null) { | |
| _handleBackgroundMessage(initialMessage); | |
| } | |
| } | |
| void _handleBackgroundMessage(RemoteMessage message) { | |
| showNotification(message); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment