From 6f13cc7654ff5efc7a87dc9a49e4c7b0caf76c33 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Sun, 1 Mar 2020 22:42:39 +0100 Subject: [PATCH 1/7] Added dynamic firebase api configuration Changes: `FirebaseMessagingPlugin.java`: - Moved `FirebaseApp.initializeApp` inside the `onMethodCall` method in the "configure" branch - Checking if `call.arguments` is provided and it is an instance of `Map`. - If true create a FirebaseOptions object from the map and use it. - Otherwise initialize Firebase as before `FLTFirebaseMessagingPlugin.m`: - Moved `[FIRApp configure]` etc. inside the `handleMethodCall` method in the "configure" branch. - Checking if `call.arguments` is provided and it is an instance of `NSDictionary`. - If true create a `FIROptions` object and init it with the googleAppId and gcmSenderId and pass it to `[FIRApp configureWithOptions:options]` to configure Firebase - Otherwise initialize Firebase as before `firebase_messaging.dart`: - Added the `FirebaseOptions` class, which represents the configuration - Invoke the "configure" channel method with the options (if they are not null) `firebase_messaging_test.dart`: - Added a test for the new method `README.md`: - Added the "Advanced usage" section --- packages/firebase_messaging/README.md | 52 +++++++++++++++++ .../FirebaseMessagingPlugin.java | 29 +++++++++- .../ios/Classes/FLTFirebaseMessagingPlugin.m | 58 +++++++++++++++++-- .../lib/firebase_messaging.dart | 42 +++++++++++++- .../test/firebase_messaging_test.dart | 16 +++++ 5 files changed, 189 insertions(+), 8 deletions(-) diff --git a/packages/firebase_messaging/README.md b/packages/firebase_messaging/README.md index 5bf421d03177..8473c9648255 100644 --- a/packages/firebase_messaging/README.md +++ b/packages/firebase_messaging/README.md @@ -286,6 +286,58 @@ Future> sendAndRetrieveMessage() async { } ``` +## Advanced usage + +### Dynamic firebase configuration +If you need to configure firebase at runtime (instead of using `google-service.json` / `GoogleService-Info.plist`) +you can pass an options object to the configure call: +```dart +import 'dart:io' show Platform; +final firebase = FirebaseMessaging(); + +var options = Platform.isIOS + ? FirebaseOptions( // iOS options + clientId: "...", + apiKey: "...", + gcmSenderId: "...", + bundleId: "...", + projectId: "...", + storageBucket: "...", + googleAppId: "...", + databaseUrl: "...", + ) + : FirebaseOptions( // Android options + clientId: "...", + apiKey: "...", + gcmSenderId: "...", + projectId: "...", + storageBucket: "...", + googleAppId: "...", + databaseUrl: "...", + ); + +firebase.configure( + options: options, +); +``` + +### On Android: +You need to disable the `FirebaseInitProvider`. +To do this, put this into your `AndroidManifest.xml` inside the `` tag: +```xml + +``` + +To hide the error message `Missing google_app_id. Firebase Analytics disabled.` put the following lines into your `res/strings.xml` of your app: +```xml + +1:0000000000000:android:0000000000000000 +``` + + ## Issues and feedback Please file Flutterfire specific issues, bugs, or feature requests in our [issue tracker](https://github.com/FirebaseExtended/flutterfire/issues/new). diff --git a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java index 7176daa75796..858092f2bcdb 100644 --- a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java +++ b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java @@ -54,7 +54,6 @@ public static void registerWith(Registrar registrar) { private void onAttachedToEngine(Context context, BinaryMessenger binaryMessenger) { this.applicationContext = context; - FirebaseApp.initializeApp(applicationContext); channel = new MethodChannel(binaryMessenger, "plugins.flutter.io/firebase_messaging"); final MethodChannel backgroundCallbackChannel = new MethodChannel(binaryMessenger, "plugins.flutter.io/firebase_messaging_background"); @@ -182,6 +181,34 @@ public void onMethodCall(final MethodCall call, final Result result) { FlutterFirebaseMessagingService.onInitialized(); result.success(true); } else if ("configure".equals(call.method)) { + if(call.arguments instanceof Map) + { + @SuppressWarnings("unchecked") + Map arguments = ((Map) call.arguments); + + String apiKey, googleAppId; + if ((apiKey = arguments.get("apiKey")) == null) { + result.error("apiKey", "apiKey must not be empty!", null); + return; + } + if ((googleAppId = arguments.get("googleAppId")) == null) { + result.error("googleAppId", "googleAppId must not be empty!", null); + return; + } + + FirebaseOptions options = new FirebaseOptions.Builder() + .setApiKey(apiKey) + .setGcmSenderId(arguments.get("gcmSenderId")) + .setProjectId(arguments.get("projectId")) + .setStorageBucket(arguments.get("storageBucket")) + .setApplicationId(googleAppId) + .setDatabaseUrl(arguments.get("databaseUrl")) + .setGaTrackingId(arguments.get("gaTrackingId")) + .build(); + FirebaseApp.initializeApp(applicationContext, options); + } else { + FirebaseApp.initializeApp(applicationContext); + } FirebaseInstanceId.getInstance() .getInstanceId() .addOnCompleteListener( diff --git a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index c763f293c700..d7a27d27fd36 100644 --- a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -51,12 +51,6 @@ - (instancetype)initWithChannel:(FlutterMethodChannel *)channel { if (self) { _channel = channel; _resumingFromBackground = NO; - if (![FIRApp appNamed:@"__FIRAPP_DEFAULT"]) { - NSLog(@"Configuring the default Firebase app..."); - [FIRApp configure]; - NSLog(@"Configured the default Firebase app %@.", [FIRApp defaultApp].name); - } - [FIRMessaging messaging].delegate = self; } return self; } @@ -137,6 +131,58 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result result([NSNumber numberWithBool:YES]); } } else if ([@"configure" isEqualToString:method]) { + if (![FIRApp appNamed:@"__FIRAPP_DEFAULT"]) { + NSLog(@"Configuring the default Firebase app..."); + + if(call.arguments != nil && [call.arguments isKindOfClass:[NSDictionary class]]) { + NSDictionary *arguments = call.arguments; + NSString + *googleAppId, + *gcmSenderId, + *clientId, + *apiKey, + *bundleId, + *projectId, + *storageBucket, + *databaseUrl; + + if((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { + result([FlutterError errorWithCode:@"googleAppId" message:@"googleAppId must not be empty" details:nil]); + return; + } + if((gcmSenderId = [arguments objectForKey:@"gcmSenderId"]) == nil) { + result([FlutterError errorWithCode:@"gcmSenderId" message:@"gcmSenderId must not be empty" details:nil]); + return; + } + + FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:googleAppId GCMSenderID:gcmSenderId]; + + if((clientId = [arguments objectForKey:@"clientId"]) == nil) { + [options setClientID:clientId]; + } + if((apiKey = [arguments objectForKey:@"apiKey"]) != nil) { + [options setAPIKey:apiKey]; + } + if((bundleId = [arguments objectForKey:@"bundleId"]) != nil) { + [options setBundleID:bundleId]; + } + if((projectId = [arguments objectForKey:@"projectId"]) != nil) { + [options setProjectID:projectId]; + } + if((storageBucket = [arguments objectForKey:@"storageBucket"]) != nil) { + [options setStorageBucket:storageBucket]; + } + if((databaseUrl = [arguments objectForKey:@"databaseUrl"]) != nil) { + [options setDatabaseURL:databaseUrl]; + } + [FIRApp configureWithOptions:options]; + } else { + [FIRApp configure]; + } + [FIRMessaging messaging].delegate = self; + NSLog(@"Configured the default Firebase app %@.", [FIRApp defaultApp].name); + } + [FIRMessaging messaging].shouldEstablishDirectChannel = true; [[UIApplication sharedApplication] registerForRemoteNotifications]; if (_launchNotification != nil) { diff --git a/packages/firebase_messaging/lib/firebase_messaging.dart b/packages/firebase_messaging/lib/firebase_messaging.dart index e17206f18c52..d28b9155afd2 100644 --- a/packages/firebase_messaging/lib/firebase_messaging.dart +++ b/packages/firebase_messaging/lib/firebase_messaging.dart @@ -73,6 +73,7 @@ class FirebaseMessaging { MessageHandler _onBackgroundMessage; MessageHandler _onLaunch; MessageHandler _onResume; + FirebaseOptions _options; /// On iOS, prompts the user for notification permissions the first time /// it is called. @@ -106,12 +107,14 @@ class FirebaseMessaging { MessageHandler onBackgroundMessage, MessageHandler onLaunch, MessageHandler onResume, + FirebaseOptions options, }) { _onMessage = onMessage; _onLaunch = onLaunch; _onResume = onResume; + _options = options; _channel.setMethodCallHandler(_handleMethod); - _channel.invokeMethod('configure'); + _channel.invokeMethod('configure', _options?.asMap()); if (onBackgroundMessage != null) { _onBackgroundMessage = onBackgroundMessage; final CallbackHandle backgroundSetupHandle = @@ -236,3 +239,40 @@ class IosNotificationSettings { @override String toString() => 'PushNotificationSettings ${toMap()}'; } + +class FirebaseOptions { + final String clientId; + final String apiKey; + final String gcmSenderId; + final String bundleId; + final String projectId; + final String storageBucket; + final String googleAppId; + final String databaseUrl; + + /// Creates a new [FirebaseOptions] object + FirebaseOptions({ + this.clientId, + this.apiKey, + this.gcmSenderId, + this.bundleId, + this.projectId, + this.storageBucket, + this.googleAppId, + this.databaseUrl, + }); + + /// Converts the options to a map for passing it through the platform channel + Map asMap() { + return { + "clientId": clientId, + "apiKey": apiKey, + "gcmSenderId": gcmSenderId, + "bundleId": bundleId, + "projectId": projectId, + "storageBucket": storageBucket, + "googleAppId": googleAppId, + "databaseUrl": databaseUrl, + }; + } +} \ No newline at end of file diff --git a/packages/firebase_messaging/test/firebase_messaging_test.dart b/packages/firebase_messaging/test/firebase_messaging_test.dart index ef7d489cfb06..dba850c8773c 100644 --- a/packages/firebase_messaging/test/firebase_messaging_test.dart +++ b/packages/firebase_messaging/test/firebase_messaging_test.dart @@ -68,6 +68,22 @@ void main() { verify(mockChannel.invokeMethod('configure')); }); + test('configure with options', () { + var firebaseOptions = FirebaseOptions( + clientId: "1", + apiKey: "2", + gcmSenderId: "3", + bundleId: "4", + projectId: "5", + storageBucket: "6", + googleAppId: "7", + databaseUrl: "8", + ); + firebaseMessaging.configure(options: firebaseOptions); + verify(mockChannel.setMethodCallHandler(any)); + verify(mockChannel.invokeMethod('configure', firebaseOptions.asMap())); + }); + test('incoming token', () async { firebaseMessaging.configure(); final dynamic handler = From c5fb52efee7172908c886b51bb65b1911108da51 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Sun, 1 Mar 2020 22:53:36 +0100 Subject: [PATCH 2/7] pubspec.yaml version + CHANGELOG.md updated --- packages/firebase_messaging/CHANGELOG.md | 4 ++++ packages/firebase_messaging/pubspec.yaml | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/packages/firebase_messaging/CHANGELOG.md b/packages/firebase_messaging/CHANGELOG.md index b66c4ba32c33..9bb66c388777 100644 --- a/packages/firebase_messaging/CHANGELOG.md +++ b/packages/firebase_messaging/CHANGELOG.md @@ -1,3 +1,7 @@ +## 6.1.11 + +* Add the `FirebaseOptions options` parameter to the `FirebaseMessaging.configure()` call to set the firebase configuration at runtime. + ## 6.0.11 * Make the pedantic dev_dependency explicit. diff --git a/packages/firebase_messaging/pubspec.yaml b/packages/firebase_messaging/pubspec.yaml index d3362443d9f7..c134ae8aa0a9 100644 --- a/packages/firebase_messaging/pubspec.yaml +++ b/packages/firebase_messaging/pubspec.yaml @@ -2,7 +2,7 @@ name: firebase_messaging description: Flutter plugin for Firebase Cloud Messaging, a cross-platform messaging solution that lets you reliably deliver messages on Android and iOS. homepage: https://github.com/FirebaseExtended/flutterfire/tree/master/packages/firebase_messaging -version: 6.0.11 +version: 6.1.0 flutter: plugin: From 9a4c09b75b8dd71514bad2f2c68eaffe9f82a970 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Mon, 2 Mar 2020 07:36:54 +0100 Subject: [PATCH 3/7] Fixed build errors --- packages/firebase_messaging/CHANGELOG.md | 2 +- .../plugins/firebasemessaging/FirebaseMessagingPlugin.java | 1 + packages/firebase_messaging/lib/firebase_messaging.dart | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/packages/firebase_messaging/CHANGELOG.md b/packages/firebase_messaging/CHANGELOG.md index 9bb66c388777..3433df5e6e91 100644 --- a/packages/firebase_messaging/CHANGELOG.md +++ b/packages/firebase_messaging/CHANGELOG.md @@ -1,4 +1,4 @@ -## 6.1.11 +## 6.1.0 * Add the `FirebaseOptions options` parameter to the `FirebaseMessaging.configure()` call to set the firebase configuration at runtime. diff --git a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java index 858092f2bcdb..9c5f24dc99a6 100644 --- a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java +++ b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java @@ -16,6 +16,7 @@ import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.FirebaseApp; +import com.google.firebase.FirebaseOptions; import com.google.firebase.iid.FirebaseInstanceId; import com.google.firebase.iid.InstanceIdResult; import com.google.firebase.messaging.FirebaseMessaging; diff --git a/packages/firebase_messaging/lib/firebase_messaging.dart b/packages/firebase_messaging/lib/firebase_messaging.dart index d28b9155afd2..f2848bb4fad9 100644 --- a/packages/firebase_messaging/lib/firebase_messaging.dart +++ b/packages/firebase_messaging/lib/firebase_messaging.dart @@ -275,4 +275,4 @@ class FirebaseOptions { "databaseUrl": databaseUrl, }; } -} \ No newline at end of file +} From 073d0451823cbb6c1ebdc30d611befb4c06460a6 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Mon, 2 Mar 2020 08:18:37 +0100 Subject: [PATCH 4/7] Fixed formatting --- .../ios/Classes/FLTFirebaseMessagingPlugin.m | 88 +++++++++---------- .../test/firebase_messaging_test.dart | 22 ++--- 2 files changed, 52 insertions(+), 58 deletions(-) diff --git a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index d7a27d27fd36..3e7e1a911591 100644 --- a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -132,55 +132,47 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result } } else if ([@"configure" isEqualToString:method]) { if (![FIRApp appNamed:@"__FIRAPP_DEFAULT"]) { - NSLog(@"Configuring the default Firebase app..."); - - if(call.arguments != nil && [call.arguments isKindOfClass:[NSDictionary class]]) { - NSDictionary *arguments = call.arguments; - NSString - *googleAppId, - *gcmSenderId, - *clientId, - *apiKey, - *bundleId, - *projectId, - *storageBucket, - *databaseUrl; - - if((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { - result([FlutterError errorWithCode:@"googleAppId" message:@"googleAppId must not be empty" details:nil]); - return; - } - if((gcmSenderId = [arguments objectForKey:@"gcmSenderId"]) == nil) { - result([FlutterError errorWithCode:@"gcmSenderId" message:@"gcmSenderId must not be empty" details:nil]); - return; - } - - FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:googleAppId GCMSenderID:gcmSenderId]; - - if((clientId = [arguments objectForKey:@"clientId"]) == nil) { - [options setClientID:clientId]; - } - if((apiKey = [arguments objectForKey:@"apiKey"]) != nil) { - [options setAPIKey:apiKey]; - } - if((bundleId = [arguments objectForKey:@"bundleId"]) != nil) { - [options setBundleID:bundleId]; - } - if((projectId = [arguments objectForKey:@"projectId"]) != nil) { - [options setProjectID:projectId]; - } - if((storageBucket = [arguments objectForKey:@"storageBucket"]) != nil) { - [options setStorageBucket:storageBucket]; - } - if((databaseUrl = [arguments objectForKey:@"databaseUrl"]) != nil) { - [options setDatabaseURL:databaseUrl]; - } - [FIRApp configureWithOptions:options]; - } else { - [FIRApp configure]; + NSLog(@"Configuring the default Firebase app..."); + + if(call.arguments != nil && [call.arguments isKindOfClass:[NSDictionary class]]) { + NSDictionary *arguments = call.arguments; + NSString *googleAppId, *gcmSenderId, *clientId, *apiKey, *bundleId, *projectId, *storageBucket, *databaseUrl; + + if((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { + result([FlutterError errorWithCode:@"googleAppId" message:@"googleAppId must not be empty" details:nil]); + return; + } + if((gcmSenderId = [arguments objectForKey:@"gcmSenderId"]) == nil) { + result([FlutterError errorWithCode:@"gcmSenderId" message:@"gcmSenderId must not be empty" details:nil]); + return; + } + + FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:googleAppId GCMSenderID:gcmSenderId]; + + if((clientId = [arguments objectForKey:@"clientId"]) == nil) { + [options setClientID:clientId]; + } + if((apiKey = [arguments objectForKey:@"apiKey"]) != nil) { + [options setAPIKey:apiKey]; } - [FIRMessaging messaging].delegate = self; - NSLog(@"Configured the default Firebase app %@.", [FIRApp defaultApp].name); + if((bundleId = [arguments objectForKey:@"bundleId"]) != nil) { + [options setBundleID:bundleId]; + } + if((projectId = [arguments objectForKey:@"projectId"]) != nil) { + [options setProjectID:projectId]; + } + if((storageBucket = [arguments objectForKey:@"storageBucket"]) != nil) { + [options setStorageBucket:storageBucket]; + } + if((databaseUrl = [arguments objectForKey:@"databaseUrl"]) != nil) { + [options setDatabaseURL:databaseUrl]; + } + [FIRApp configureWithOptions:options]; + } else { + [FIRApp configure]; + } + [FIRMessaging messaging].delegate = self; + NSLog(@"Configured the default Firebase app %@.", [FIRApp defaultApp].name); } [FIRMessaging messaging].shouldEstablishDirectChannel = true; diff --git a/packages/firebase_messaging/test/firebase_messaging_test.dart b/packages/firebase_messaging/test/firebase_messaging_test.dart index dba850c8773c..f8bc2e73ecaf 100644 --- a/packages/firebase_messaging/test/firebase_messaging_test.dart +++ b/packages/firebase_messaging/test/firebase_messaging_test.dart @@ -4,8 +4,8 @@ import 'dart:async'; -import 'package:flutter/services.dart'; import 'package:firebase_messaging/firebase_messaging.dart'; +import 'package:flutter/services.dart'; import 'package:flutter_test/flutter_test.dart' show TestWidgetsFlutterBinding; import 'package:mockito/mockito.dart'; import 'package:platform/platform.dart'; @@ -70,18 +70,20 @@ void main() { test('configure with options', () { var firebaseOptions = FirebaseOptions( - clientId: "1", - apiKey: "2", - gcmSenderId: "3", - bundleId: "4", - projectId: "5", - storageBucket: "6", - googleAppId: "7", - databaseUrl: "8", + clientId: "1", + apiKey: "2", + gcmSenderId: "3", + bundleId: "4", + projectId: "5", + storageBucket: "6", + googleAppId: "7", + databaseUrl: "8", ); firebaseMessaging.configure(options: firebaseOptions); verify(mockChannel.setMethodCallHandler(any)); - verify(mockChannel.invokeMethod('configure', firebaseOptions.asMap())); + verify( + mockChannel.invokeMethod('configure', firebaseOptions.asMap()), + ); }); test('incoming token', () async { From 179aa2a0d25d61ccecac997b1b25c7f8442a49de Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Tue, 3 Mar 2020 08:07:12 +0100 Subject: [PATCH 5/7] Fixed formatting and prevent double initialization on Android --- .../FirebaseMessagingPlugin.java | 56 ++++++++++--------- .../ios/Classes/FLTFirebaseMessagingPlugin.m | 45 +++++++++------ 2 files changed, 60 insertions(+), 41 deletions(-) diff --git a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java index 9c5f24dc99a6..74672caa02b3 100644 --- a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java +++ b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java @@ -45,6 +45,7 @@ public class FirebaseMessagingPlugin extends BroadcastReceiver private MethodChannel channel; private Context applicationContext; private Activity mainActivity; + private boolean isInitialized = false; public static void registerWith(Registrar registrar) { FirebaseMessagingPlugin instance = new FirebaseMessagingPlugin(); @@ -182,33 +183,38 @@ public void onMethodCall(final MethodCall call, final Result result) { FlutterFirebaseMessagingService.onInitialized(); result.success(true); } else if ("configure".equals(call.method)) { - if(call.arguments instanceof Map) + if (!isInitialized) { - @SuppressWarnings("unchecked") - Map arguments = ((Map) call.arguments); - - String apiKey, googleAppId; - if ((apiKey = arguments.get("apiKey")) == null) { - result.error("apiKey", "apiKey must not be empty!", null); - return; + if (call.arguments instanceof Map) + { + @SuppressWarnings("unchecked") + Map arguments = ((Map) call.arguments); + + String apiKey, googleAppId; + if ((apiKey = arguments.get("apiKey")) == null) { + result.error("apiKey", "apiKey must not be empty!", null); + return; + } + if ((googleAppId = arguments.get("googleAppId")) == null) { + result.error("googleAppId", "googleAppId must not be empty!", null); + return; + } + + FirebaseOptions options = + new FirebaseOptions.Builder() + .setApiKey(apiKey) + .setGcmSenderId(arguments.get("gcmSenderId")) + .setProjectId(arguments.get("projectId")) + .setStorageBucket(arguments.get("storageBucket")) + .setApplicationId(googleAppId) + .setDatabaseUrl(arguments.get("databaseUrl")) + .setGaTrackingId(arguments.get("gaTrackingId")) + .build(); + FirebaseApp.initializeApp(applicationContext, options); + } else { + FirebaseApp.initializeApp(applicationContext); } - if ((googleAppId = arguments.get("googleAppId")) == null) { - result.error("googleAppId", "googleAppId must not be empty!", null); - return; - } - - FirebaseOptions options = new FirebaseOptions.Builder() - .setApiKey(apiKey) - .setGcmSenderId(arguments.get("gcmSenderId")) - .setProjectId(arguments.get("projectId")) - .setStorageBucket(arguments.get("storageBucket")) - .setApplicationId(googleAppId) - .setDatabaseUrl(arguments.get("databaseUrl")) - .setGaTrackingId(arguments.get("gaTrackingId")) - .build(); - FirebaseApp.initializeApp(applicationContext, options); - } else { - FirebaseApp.initializeApp(applicationContext); + isInitialized = true; } FirebaseInstanceId.getInstance() .getInstanceId() diff --git a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index 3e7e1a911591..d2d09aa7ea6b 100644 --- a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -134,37 +134,50 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result if (![FIRApp appNamed:@"__FIRAPP_DEFAULT"]) { NSLog(@"Configuring the default Firebase app..."); - if(call.arguments != nil && [call.arguments isKindOfClass:[NSDictionary class]]) { + if (call.arguments != nil && [call.arguments isKindOfClass:[NSDictionary class]]) { NSDictionary *arguments = call.arguments; - NSString *googleAppId, *gcmSenderId, *clientId, *apiKey, *bundleId, *projectId, *storageBucket, *databaseUrl; - - if((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { - result([FlutterError errorWithCode:@"googleAppId" message:@"googleAppId must not be empty" details:nil]); + NSString + *googleAppId, + *gcmSenderId, + *clientId, + *apiKey, + *bundleId, + *projectId, + *storageBucket, + *databaseUrl; + + if ((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { + result([FlutterError errorWithCode:@"googleAppId" + message:@"googleAppId must not be empty" + details:nil]); return; } - if((gcmSenderId = [arguments objectForKey:@"gcmSenderId"]) == nil) { - result([FlutterError errorWithCode:@"gcmSenderId" message:@"gcmSenderId must not be empty" details:nil]); + if ((gcmSenderId = [arguments objectForKey:@"gcmSenderId"]) == nil) { + result([FlutterError errorWithCode:@"gcmSenderId" + message:@"gcmSenderId must not be empty" + details:nil]); return; } - - FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:googleAppId GCMSenderID:gcmSenderId]; - - if((clientId = [arguments objectForKey:@"clientId"]) == nil) { + + FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:googleAppId + GCMSenderID:gcmSenderId]; + + if ((clientId = [arguments objectForKey:@"clientId"]) == nil) { [options setClientID:clientId]; } - if((apiKey = [arguments objectForKey:@"apiKey"]) != nil) { + if ((apiKey = [arguments objectForKey:@"apiKey"]) != nil) { [options setAPIKey:apiKey]; } - if((bundleId = [arguments objectForKey:@"bundleId"]) != nil) { + if ((bundleId = [arguments objectForKey:@"bundleId"]) != nil) { [options setBundleID:bundleId]; } - if((projectId = [arguments objectForKey:@"projectId"]) != nil) { + if ((projectId = [arguments objectForKey:@"projectId"]) != nil) { [options setProjectID:projectId]; } - if((storageBucket = [arguments objectForKey:@"storageBucket"]) != nil) { + if ((storageBucket = [arguments objectForKey:@"storageBucket"]) != nil) { [options setStorageBucket:storageBucket]; } - if((databaseUrl = [arguments objectForKey:@"databaseUrl"]) != nil) { + if ((databaseUrl = [arguments objectForKey:@"databaseUrl"]) != nil) { [options setDatabaseURL:databaseUrl]; } [FIRApp configureWithOptions:options]; From 502a8426912c99e51462bebc3ca74a63480df504 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Tue, 3 Mar 2020 08:23:18 +0100 Subject: [PATCH 6/7] Fixed formatting again --- .../FirebaseMessagingPlugin.java | 6 ++---- .../ios/Classes/FLTFirebaseMessagingPlugin.m | 15 ++++----------- 2 files changed, 6 insertions(+), 15 deletions(-) diff --git a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java index 48451ae2ef03..b9b57c50fda7 100644 --- a/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java +++ b/packages/firebase_messaging/android/src/main/java/io/flutter/plugins/firebasemessaging/FirebaseMessagingPlugin.java @@ -182,10 +182,8 @@ public void onMethodCall(final MethodCall call, final Result result) { FlutterFirebaseMessagingService.onInitialized(); result.success(true); } else if ("configure".equals(call.method)) { - if (!isInitialized) - { - if (call.arguments instanceof Map) - { + if (!isInitialized) { + if (call.arguments instanceof Map) { @SuppressWarnings("unchecked") Map arguments = ((Map) call.arguments); diff --git a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index d2d09aa7ea6b..be359f6734c9 100644 --- a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -136,15 +136,8 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result if (call.arguments != nil && [call.arguments isKindOfClass:[NSDictionary class]]) { NSDictionary *arguments = call.arguments; - NSString - *googleAppId, - *gcmSenderId, - *clientId, - *apiKey, - *bundleId, - *projectId, - *storageBucket, - *databaseUrl; + NSString *googleAppId, *gcmSenderId, *clientId, *apiKey, *bundleId, *projectId, + *storageBucket, *databaseUrl; if ((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { result([FlutterError errorWithCode:@"googleAppId" @@ -158,10 +151,10 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result details:nil]); return; } - + FIROptions *options = [[FIROptions alloc] initWithGoogleAppID:googleAppId GCMSenderID:gcmSenderId]; - + if ((clientId = [arguments objectForKey:@"clientId"]) == nil) { [options setClientID:clientId]; } From 67914ab1c2644d64cc35f612bc122a1725904bf8 Mon Sep 17 00:00:00 2001 From: Julian Finkler Date: Tue, 3 Mar 2020 08:31:09 +0100 Subject: [PATCH 7/7] Removed whitespaces of empty line --- .../firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m index be359f6734c9..a223fad4614d 100644 --- a/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m +++ b/packages/firebase_messaging/ios/Classes/FLTFirebaseMessagingPlugin.m @@ -138,7 +138,7 @@ - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result NSDictionary *arguments = call.arguments; NSString *googleAppId, *gcmSenderId, *clientId, *apiKey, *bundleId, *projectId, *storageBucket, *databaseUrl; - + if ((googleAppId = [arguments objectForKey:@"googleAppId"]) == nil) { result([FlutterError errorWithCode:@"googleAppId" message:@"googleAppId must not be empty"