From f0f2f06ec2abd93de209cd917eebb20d4f41adf9 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Thu, 15 Apr 2021 13:34:49 +0200 Subject: [PATCH] Integrate Firebase --- android/app/build.gradle | 2 + android/app/src/beta/google-services.json | 39 ++++++++++++ android/app/src/main/google-services.json | 68 +++++++++++++++++++++ android/build.gradle | 3 + lib/generated_plugin_registrant.dart | 7 ++- lib/helpers/firebase_messaging_helper.dart | 26 ++++++++ lib/ui/routes/push_notifications_route.dart | 10 ++- pubspec.lock | 42 +++++++++++++ pubspec.yaml | 4 ++ 9 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 android/app/src/beta/google-services.json create mode 100644 android/app/src/main/google-services.json create mode 100644 lib/helpers/firebase_messaging_helper.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index 89be84c..7a7d796 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -104,3 +104,5 @@ dependencies { androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0' } + +apply plugin: 'com.google.gms.google-services' \ No newline at end of file diff --git a/android/app/src/beta/google-services.json b/android/app/src/beta/google-services.json new file mode 100644 index 0000000..3c36a3c --- /dev/null +++ b/android/app/src/beta/google-services.json @@ -0,0 +1,39 @@ +{ + "project_info": { + "project_number": "1007877315904", + "project_id": "comunic-ae92c", + "storage_bucket": "comunic-ae92c.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:1007877315904:android:d1db6417ef236d850aeea5", + "android_client_info": { + "package_name": "org.communiquons.beta" + } + }, + "oauth_client": [ + { + "client_id": "1007877315904-6l6lj4d93tf7btckjpjear0js45mvuj5.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAfhNbEhHkmVYPQK200rJGxH9hc7qHlcwU" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "1007877315904-6l6lj4d93tf7btckjpjear0js45mvuj5.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/android/app/src/main/google-services.json b/android/app/src/main/google-services.json new file mode 100644 index 0000000..0db6f18 --- /dev/null +++ b/android/app/src/main/google-services.json @@ -0,0 +1,68 @@ +{ + "project_info": { + "project_number": "1007877315904", + "project_id": "comunic-ae92c", + "storage_bucket": "comunic-ae92c.appspot.com" + }, + "client": [ + { + "client_info": { + "mobilesdk_app_id": "1:1007877315904:android:d1db6417ef236d850aeea5", + "android_client_info": { + "package_name": "org.communiquons.beta" + } + }, + "oauth_client": [ + { + "client_id": "1007877315904-6l6lj4d93tf7btckjpjear0js45mvuj5.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAfhNbEhHkmVYPQK200rJGxH9hc7qHlcwU" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "1007877315904-6l6lj4d93tf7btckjpjear0js45mvuj5.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + }, + { + "client_info": { + "mobilesdk_app_id": "1:1007877315904:android:c30eb760a5ffca750aeea5", + "android_client_info": { + "package_name": "org.communiquons.comunic" + } + }, + "oauth_client": [ + { + "client_id": "1007877315904-6l6lj4d93tf7btckjpjear0js45mvuj5.apps.googleusercontent.com", + "client_type": 3 + } + ], + "api_key": [ + { + "current_key": "AIzaSyAfhNbEhHkmVYPQK200rJGxH9hc7qHlcwU" + } + ], + "services": { + "appinvite_service": { + "other_platform_oauth_client": [ + { + "client_id": "1007877315904-6l6lj4d93tf7btckjpjear0js45mvuj5.apps.googleusercontent.com", + "client_type": 3 + } + ] + } + } + } + ], + "configuration_version": "1" +} \ No newline at end of file diff --git a/android/build.gradle b/android/build.gradle index 5dff385..a80972f 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -6,6 +6,9 @@ buildscript { dependencies { classpath 'com.android.tools.build:gradle:3.5.4' + + // Firebase + classpath 'com.google.gms:google-services:4.3.5' } } diff --git a/lib/generated_plugin_registrant.dart b/lib/generated_plugin_registrant.dart index ef6f366..cd3ce21 100644 --- a/lib/generated_plugin_registrant.dart +++ b/lib/generated_plugin_registrant.dart @@ -5,16 +5,19 @@ // ignore_for_file: lines_longer_than_80_chars import 'package:file_picker/src/file_picker_web.dart'; +import 'package:firebase_core_web/firebase_core_web.dart'; +import 'package:firebase_messaging_web/firebase_messaging_web.dart'; +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; import 'package:shared_preferences_web/shared_preferences_web.dart'; import 'package:url_launcher_web/url_launcher_web.dart'; import 'package:video_player_web/video_player_web.dart'; import 'package:wakelock_web/wakelock_web.dart'; -import 'package:flutter_web_plugins/flutter_web_plugins.dart'; - // ignore: public_member_api_docs void registerPlugins(Registrar registrar) { FilePickerWeb.registerWith(registrar); + FirebaseCoreWeb.registerWith(registrar); + FirebaseMessagingWeb.registerWith(registrar); SharedPreferencesPlugin.registerWith(registrar); UrlLauncherPlugin.registerWith(registrar); VideoPlayerPlugin.registerWith(registrar); diff --git a/lib/helpers/firebase_messaging_helper.dart b/lib/helpers/firebase_messaging_helper.dart new file mode 100644 index 0000000..be8da5d --- /dev/null +++ b/lib/helpers/firebase_messaging_helper.dart @@ -0,0 +1,26 @@ +import 'package:firebase_core/firebase_core.dart'; +import 'package:firebase_messaging/firebase_messaging.dart'; + +/// Firebase messaging helper +/// +/// @author Pierre Hubert + +bool _isFirebaseReady = false; + +class FirebaseMessagingHelper { + /// Pre configure notifications + static Future preConfigure() async { + if (!_isFirebaseReady) await Firebase.initializeApp(); + _isFirebaseReady = true; + + final settings = await FirebaseMessaging.instance.requestPermission(); + + if (settings.authorizationStatus != AuthorizationStatus.authorized) + throw new Exception("The user did not authorize notifications!"); + } + + /// Get a Firebase access token + static Future getToken() async { + return await FirebaseMessaging.instance.getToken(); + } +} diff --git a/lib/ui/routes/push_notifications_route.dart b/lib/ui/routes/push_notifications_route.dart index ee7440b..202e40b 100644 --- a/lib/ui/routes/push_notifications_route.dart +++ b/lib/ui/routes/push_notifications_route.dart @@ -1,3 +1,4 @@ +import 'package:comunic/helpers/firebase_messaging_helper.dart'; import 'package:comunic/helpers/independent_push_notifications_helper.dart'; import 'package:comunic/helpers/push_notifications_helper.dart'; import 'package:comunic/helpers/server_config_helper.dart'; @@ -111,13 +112,15 @@ class _PushNotificationsConfigurationRouteState Future _submit() async { try { + String firebaseToken = ""; + switch (currStatus) { case PushNotificationsStatus.DISABLED: break; case PushNotificationsStatus.FIREBASE: - // TODO: Handle this case. - throw new Exception("Firebase not supported yet!"); + await FirebaseMessagingHelper.preConfigure(); + firebaseToken = await FirebaseMessagingHelper.getToken(); break; case PushNotificationsStatus.INDEPENDENT: @@ -130,7 +133,8 @@ class _PushNotificationsConfigurationRouteState } await PushNotificationsHelper.clearLocalStatus(); - await PushNotificationsHelper.setNewStatus(currStatus); + await PushNotificationsHelper.setNewStatus(currStatus, + firebaseToken: firebaseToken); await PushNotificationsHelper.refreshLocalStatus(); Navigator.of(context).pop(); diff --git a/pubspec.lock b/pubspec.lock index 03909f6..9015b24 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -190,6 +190,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.4" + firebase_core: + dependency: "direct main" + description: + name: firebase_core + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + firebase_core_platform_interface: + dependency: transitive + description: + name: firebase_core_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" + firebase_core_web: + dependency: transitive + description: + name: firebase_core_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" + firebase_messaging: + dependency: "direct main" + description: + name: firebase_messaging + url: "https://pub.dartlang.org" + source: hosted + version: "9.0.0" + firebase_messaging_platform_interface: + dependency: transitive + description: + name: firebase_messaging_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.0" + firebase_messaging_web: + dependency: transitive + description: + name: firebase_messaging_web + url: "https://pub.dartlang.org" + source: hosted + version: "1.0.1" flutter: dependency: "direct main" description: flutter diff --git a/pubspec.yaml b/pubspec.yaml index 0db55f9..18322ee 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -126,6 +126,10 @@ dependencies: # Image cropper image_cropper: ^1.4.0 + # Firebase cloud messaging (for push notifications) + firebase_core: ^1.0.1 + firebase_messaging: ^9.0.0 + dev_dependencies: flutter_test: sdk: flutter