Created
September 17, 2022 17:04
-
-
Save behzodfaiziev/fb91ad4a814bb761087b24c0810d62a9 to your computer and use it in GitHub Desktop.
FCM Manager
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 'package:firebase_messaging/firebase_messaging.dart'; | |
| import 'package:flutter/foundation.dart'; | |
| import 'package:flutter_local_notifications/flutter_local_notifications.dart'; | |
| import '../../controllers/user/user_controller.dart'; // this is User controller class. It is required to save FCM token | |
| class FCM { | |
| FCM(); | |
| final String _vapidKey = | |
| 'YOUR WEB TOKEN OF FIREBASE ACCOUNT/yourProject/PROJECT SETTINGS/CLOUD MESSAGING/WEB CONFIGURATION/ WEB PUSH CERTIFICATES/Key Pair'; | |
| String _fcmToken = ''; | |
| final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin = | |
| FlutterLocalNotificationsPlugin(); | |
| Future<void> initialize() async { | |
| _flutterLocalNotificationsPlugin.initialize(_initializationSettings); | |
| _getFCMToken(); | |
| _onTokenRefresh(); | |
| onMessageListener(); | |
| } | |
| Future<void> _getFCMToken() async { | |
| FirebaseMessaging firebaseM = FirebaseMessaging.instance; | |
| await firebaseM.requestPermission( | |
| alert: true, | |
| announcement: false, | |
| badge: true, | |
| carPlay: false, | |
| criticalAlert: false, | |
| provisional: false, | |
| sound: true, | |
| ); | |
| _flutterLocalNotificationsPlugin | |
| .resolvePlatformSpecificImplementation< | |
| AndroidFlutterLocalNotificationsPlugin>() | |
| ?.requestPermission(); | |
| if (_fcmToken != '') { | |
| return; | |
| } | |
| if (kIsWeb) { | |
| _fcmToken = | |
| await FirebaseMessaging.instance.getToken(vapidKey: _vapidKey) ?? ''; | |
| } else { | |
| _fcmToken = await FirebaseMessaging.instance.getToken() ?? ''; | |
| } | |
| await UserController.saveFCM(kIsWeb ? true : false, _fcmToken); | |
| print('TOKEN: ${_fcmToken} :TOKEN'); | |
| return; | |
| } | |
| Future<void> _onTokenRefresh() async { | |
| FirebaseMessaging.instance.onTokenRefresh.listen((fcmToken) async { | |
| await UserController.saveFCM(kIsWeb ? true : false, _fcmToken); | |
| }).onError((err) { | |
| print('FCM ERROR'); | |
| // Error getting token. | |
| throw (err); | |
| }); | |
| } | |
| Future<void> onMessageListener() async { | |
| FirebaseMessaging.onMessage.listen((RemoteMessage message) async { | |
| RemoteNotification? notification = message.notification; | |
| AndroidNotification? android = message.notification?.android; | |
| if (notification != null && android != null) { | |
| _flutterLocalNotificationsPlugin.show( | |
| notification.hashCode, | |
| notification.title, | |
| notification.body, | |
| const NotificationDetails( | |
| android: AndroidNotificationDetails( | |
| 'channel.id', // If you have channel you can use without '', | |
| 'channel.name', // If you have channel you can use without '' | |
| icon: 'ic_stat_name', | |
| // other properties... | |
| ), | |
| )); | |
| } | |
| }); | |
| } | |
| final InitializationSettings _initializationSettings = | |
| const InitializationSettings( | |
| android: AndroidInitializationSettings('ic_stat_name')); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment