From 3cd584436b7362217d2be4a8c7b0740f408e9828 Mon Sep 17 00:00:00 2001 From: Tibor Bossanyi Date: Wed, 22 Feb 2023 22:20:11 +0100 Subject: [PATCH] v.1.0.10 --- README.md | 3 ++ lib/service/firebase_api.dart | 91 +++++++++++++++++++++-------------- pubspec.yaml | 2 +- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 2bbee65..bc43637 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ Workout Test and Diet 4 You Common Util Functions +Version 1.0.10 + Firebase FCM for web + Version 1.0.9 Firebase web config diff --git a/lib/service/firebase_api.dart b/lib/service/firebase_api.dart index fb0224e..f9e23ab 100644 --- a/lib/service/firebase_api.dart +++ b/lib/service/firebase_api.dart @@ -33,49 +33,70 @@ class FirebaseApi with logger.Logging { // Define an async function to initialize FlutterFire Future initializeFlutterFire() async { - try { - // Wait for Firebase to initialize and set `_initialized` state to true - if (kIsWeb) { - await Firebase.initializeApp( - options: const FirebaseOptions( - apiKey: "AIzaSyBLn7Bz73Z1hB-OhqphBDsskOyGmpI7J8E", - authDomain: "diet4you-cb540.firebaseapp.com", - projectId: "diet4you-cb540", - storageBucket: "diet4you-cb540.appspot.com", - messagingSenderId: "534189506381", - appId: "1:534189506381:web:e885436c9be71ab4998104", - measurementId: "G-9YY4B98J99" - ), - ); - } else { - await Firebase.initializeApp(); + try { + + if (kIsWeb) { + await Firebase.initializeApp( + options: const FirebaseOptions( + apiKey: "AIzaSyBLn7Bz73Z1hB-OhqphBDsskOyGmpI7J8E", + authDomain: "diet4you-cb540.firebaseapp.com", + projectId: "diet4you-cb540", + storageBucket: "diet4you-cb540.appspot.com", + messagingSenderId: "534189506381", + appId: "1:534189506381:web:e885436c9be71ab4998104", + measurementId: "G-9YY4B98J99" + ), + ); + } else { + await Firebase.initializeApp(); + } + } on Exception catch(e) { + Sentry.captureException(e); + log("Error in Firebase init: $e"); } appleSignInAvailable = await SignInWithApple.isAvailable(); + if (kIsWeb) { + final fcmToken = await FirebaseMessaging.instance.getToken(vapidKey: "BKqkxyTqlGGI6m4gFLa-fFu9kYflyCbLkDKRKihWLqhDyR8oy1ymGzbk9lGcSDW1fd7XZiN2XYA2sDF8yjHdFPg"); + FirebaseMessaging.instance.onTokenRefresh + .listen((fcmToken) { + - await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( - alert: true, // Required to display a heads up notification - badge: true, - sound: true, - ); - firebaseRegToken = await FirebaseMessaging.instance.getToken(); - Cache().firebaseMessageToken = firebaseRegToken; - log("FirebaseMessaging token $firebaseRegToken"); + // Note: This callback is fired at each app startup and whenever a new + // token is generated. + log('FCM token generated'); - FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); - FirebaseMessaging.onMessage.listen((RemoteMessage message) { - log('Got a message whilst in the foreground!'); - log('Message data: ${message.data}'); + }) + .onError((err) { + Sentry.captureException(err); + log("Error initializing Firebase Messaging for Web $err"); + }); + } else { + try { + await FirebaseMessaging.instance.setForegroundNotificationPresentationOptions( + alert: true, // Required to display a heads up notification + badge: true, + sound: true, + ); + firebaseRegToken = await FirebaseMessaging.instance.getToken(); + Cache().firebaseMessageToken = firebaseRegToken; + log("FirebaseMessaging token $firebaseRegToken"); - if (message.notification != null) { - log('Message also contained a notification: ${message.notification}'); + FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler); + + FirebaseMessaging.onMessage.listen((RemoteMessage message) { + log('Got a message whilst in the foreground!'); + log('Message data: ${message.data}'); + + if (message.notification != null) { + log('Message also contained a notification: ${message.notification}'); + } + }); + } catch (e) { + Sentry.captureException(e); + log("Error initializing Firebase Messaging $e"); } - }); - } catch (e) { - // Set `_error` state to true if Firebase initialization fails - Sentry.captureException(e); - log("Error initializing Firebase"); } } diff --git a/pubspec.yaml b/pubspec.yaml index b2e1677..139b67d 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: workouttest_util description: Workout Test app and web functions. -version: 1.0.8 +version: 1.0.10 homepage: environment: