diff --git a/README.md b/README.md index 507124e..811a5d1 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,22 @@ Use the native Firebase SDK in Axway Titanium. This repository is part of the [T Configure Firebase without additional parameters. +Android: returns false if it was already configured or if there was an error. Calling `deleteInstanceId()` can be used to re-configure it again. + ##### `configure(parameters)` -Configure Firebase without configuration parameters. +Configure Firebase with configuration parameters: + +| Name | Type | Component | Platform | +| - | - | - | - | +| `file` | String | | * + +By passing the `file` property, you can give a location to the Firebase plist file (usually named "GoogleService-Info.plist"), which contains all necessary properties for your Firebase project. This makes all other properties unnecessary. For Android: place the file in `/app/assets/android/` and pass just the filename. + +Or you can configure Firebase without a file by passing these parameters: | Name | Type | Component | Platform | | - | - | - | - | -| `file`* | String | | * | `googleAppID` | String | | * | `GCMSenderID` | String | Cloud Messaging | * | `APIKey` | String | Auth | * @@ -38,7 +47,6 @@ Configure Firebase without configuration parameters. | `deepLinkURLScheme` | String | | iOS | `applicationID` | String | Analytics | Android -\* By passing the `file` property, you can give a location to the Firebase plist file (usually named "GoogleService-Info.plist"), which contains all necessary properties for your Firebase project. This makes all other properties unnecessary. For Android: place the file in `/app/assets/android/` and pass just the filename. ##### `deleteInstanceId(callback)` @@ -46,8 +54,8 @@ Delete the current `instanceId` (invalidating all tokens). See the [Firebase doc The callback receives an object containing this fields: -| Key | Type |Description | -| - | - | - | +| Key | Type | Description | Platform | +| - | - | - | - | | `success` | Boolean | `true` if the deletion succeeded | * | `error` | String | The localized error message | * @@ -57,8 +65,8 @@ Delete the token of the provided `authorizedEntity` and `scope`. See the [Fireba The callback receives an object containing this fields: -| Key | Type | Description | -| - | - | - | +| Key | Type | Description | Platform | +| - | - | - | - | | `success` | Boolean | `true` if the deletion succeeded | * | `error` | String | The localized error message | * @@ -69,6 +77,11 @@ The callback receives an object containing this fields: var FirebaseCore = require('firebase.core'); // Configure your Firebase API's (only required once for all) + +FirebaseCore.configure(); // default google-services.json/GoogleService-Info.plist will be used + +// alternative way: + FirebaseCore.configure({ APIKey: "AIYasZBtfJh..........", projectID: "test-12345", diff --git a/android/manifest b/android/manifest index 77ed2d6..243f2dd 100644 --- a/android/manifest +++ b/android/manifest @@ -2,7 +2,7 @@ # this is your module manifest and used by Titanium # during compilation, packaging, distribution, etc. # -version: 2.3.0 +version: 2.3.1 apiversion: 4 architectures: arm64-v8a armeabi-v7a x86 description: titanium-firebase-core diff --git a/android/src/firebase/core/TitaniumFirebaseCoreModule.java b/android/src/firebase/core/TitaniumFirebaseCoreModule.java index e7db40b..aa9bfb9 100644 --- a/android/src/firebase/core/TitaniumFirebaseCoreModule.java +++ b/android/src/firebase/core/TitaniumFirebaseCoreModule.java @@ -29,6 +29,7 @@ import java.io.InputStream; import java.lang.Void; import java.util.HashMap; +import java.util.List; @Kroll.module(name = "TitaniumFirebaseCore", id = "firebase.core") public class TitaniumFirebaseCoreModule extends KrollModule @@ -44,91 +45,106 @@ public TitaniumFirebaseCoreModule() // Public APIs @Kroll.method - public void configure(@Kroll.argument(optional = true) KrollDict param) + public boolean configure(@Kroll.argument(optional = true) KrollDict param) { - if (param != null) { - String apiKey = ""; - String databaseURL = ""; - String projectID = ""; - String storageBucket = ""; - String applicationID = ""; - String GCMSenderID = ""; - FirebaseOptions.Builder options = new FirebaseOptions.Builder(); - - if (param.containsKey("file")) { - // open file and parse it - try { - JSONObject json = new JSONObject(loadJSONFromAsset(param.getString("file"))); - JSONObject projectInfo = json.getJSONObject("project_info"); - String packageName = TiApplication.getAppCurrentActivity().getPackageName(); + String filename = null; - if (projectInfo.has("storage_bucket")) { - storageBucket = projectInfo.getString("storage_bucket"); - } - if (projectInfo.has("firebase_url")) { - databaseURL = projectInfo.getString("firebase_url"); - } - if (projectInfo.has("project_number")) { - GCMSenderID = projectInfo.getString("project_number"); - } - if (projectInfo.has("project_id")) { - projectID = projectInfo.getString("project_id"); - } - if (json.has("client")) { - JSONArray clients = json.getJSONArray("client"); - for (int i = 0, len = clients.length(); i < len; i++) { - JSONObject client = clients.getJSONObject(i); - JSONObject clientInfo = client.getJSONObject("client_info"); - String pName = clientInfo.getJSONObject("android_client_info").getString("package_name"); - if (pName.equals(packageName)) { - applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id"); - apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key"); - } - } - } - } catch (JSONException e) { - Log.e(LCAT, "Error parsing file"); - } - } else { - // use parameters - if (param.containsKey("APIKey")) { - apiKey = param.getString("APIKey"); - } - if (param.containsKey("databaseURL")) { - databaseURL = param.getString("databaseURL"); + if (param == null) { + filename = "google-services.json"; + } else if (param.containsKey("file")) { + filename = param.getString("file"); + } + String apiKey = ""; + String databaseURL = ""; + String projectID = ""; + String storageBucket = ""; + String applicationID = ""; + String GCMSenderID = ""; + FirebaseOptions.Builder options = new FirebaseOptions.Builder(); + + if (filename != null) { + // open file and parse it + try { + JSONObject json = new JSONObject(loadJSONFromAsset(filename)); + JSONObject projectInfo = json.getJSONObject("project_info"); + String packageName = TiApplication.getAppCurrentActivity().getPackageName(); + + if (projectInfo.has("storage_bucket")) { + storageBucket = projectInfo.getString("storage_bucket"); } - if (param.containsKey("projectID")) { - projectID = param.getString("projectID"); + if (projectInfo.has("firebase_url")) { + databaseURL = projectInfo.getString("firebase_url"); } - if (param.containsKey("storageBucket")) { - storageBucket = param.getString("storageBucket"); + if (projectInfo.has("project_number")) { + GCMSenderID = projectInfo.getString("project_number"); } - if (param.containsKey("applicationID")) { - applicationID = param.getString("applicationID"); + if (projectInfo.has("project_id")) { + projectID = projectInfo.getString("project_id"); } - if (param.containsKey("GCMSenderID")) { - GCMSenderID = param.getString("GCMSenderID"); + if (json.has("client")) { + JSONArray clients = json.getJSONArray("client"); + for (int i = 0, len = clients.length(); i < len; i++) { + JSONObject client = clients.getJSONObject(i); + JSONObject clientInfo = client.getJSONObject("client_info"); + String pName = clientInfo.getJSONObject("android_client_info").getString("package_name"); + if (pName.equals(packageName)) { + applicationID = client.getJSONObject("client_info").getString("mobilesdk_app_id"); + apiKey = client.getJSONArray("api_key").getJSONObject(0).getString("current_key"); + } + } } + } catch (JSONException e) { + Log.e(LCAT, "Error parsing file"); + } + } else { + // use parameters + if (param.containsKey("APIKey")) { + apiKey = param.getString("APIKey"); } + if (param.containsKey("databaseURL")) { + databaseURL = param.getString("databaseURL"); + } + if (param.containsKey("projectID")) { + projectID = param.getString("projectID"); + } + if (param.containsKey("storageBucket")) { + storageBucket = param.getString("storageBucket"); + } + if (param.containsKey("applicationID")) { + applicationID = param.getString("applicationID"); + } + if (param.containsKey("GCMSenderID")) { + GCMSenderID = param.getString("GCMSenderID"); + } + } - options.setApiKey(apiKey); - options.setDatabaseUrl(databaseURL); - options.setProjectId(projectID); - options.setStorageBucket(storageBucket); - options.setApplicationId(applicationID); - options.setGcmSenderId(GCMSenderID); + options.setApiKey(apiKey); + options.setDatabaseUrl(databaseURL); + options.setProjectId(projectID); + options.setStorageBucket(storageBucket); + options.setApplicationId(applicationID); + options.setGcmSenderId(GCMSenderID); + + // check for existing firebaseApp + boolean hasBeenInitialized = false; + List fbsLcl = FirebaseApp.getApps(getActivity().getApplicationContext()); + for (FirebaseApp app : fbsLcl) { + if (app.getName().equals(FirebaseApp.DEFAULT_APP_NAME)) { + hasBeenInitialized = true; + } + } + if (!hasBeenInitialized) { try { FirebaseApp.initializeApp(getActivity().getApplicationContext(), options.build()); + return true; } catch (IllegalStateException e) { Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time."); + return false; } } else { - try { - FirebaseApp.initializeApp(getActivity().getApplicationContext()); - } catch (IllegalStateException e) { - Log.w(LCAT, "There was a problem initializing FirebaseApp or it was initialized a second time."); - } + Log.d(LCAT, "FirebaseApp is alraedy initialized."); + return false; } } @@ -136,7 +152,8 @@ public void configure(@Kroll.argument(optional = true) KrollDict param) public void deleteInstanceId(final KrollFunction callback) { new AsyncTask() { - protected IOException doInBackground(Void ... v) { + protected IOException doInBackground(Void... v) + { try { FirebaseInstanceId.getInstance().deleteInstanceId(); return null; @@ -145,7 +162,8 @@ protected IOException doInBackground(Void ... v) { return e; } } - protected void onPostExecute(IOException error) { + protected void onPostExecute(IOException error) + { if (callback != null) { HashMap args = new HashMap<>(); args.put("success", error == null); @@ -155,14 +173,16 @@ protected void onPostExecute(IOException error) { callback.call(getKrollObject(), args); } } - }.execute(); + } + .execute(); } @Kroll.method public void deleteToken(final String authorizedEntity, final String scope, final KrollFunction callback) { new AsyncTask() { - protected IOException doInBackground(Void ... v) { + protected IOException doInBackground(Void... v) + { try { FirebaseInstanceId.getInstance().deleteToken(authorizedEntity, scope); return null; @@ -171,7 +191,8 @@ protected IOException doInBackground(Void ... v) { return e; } } - protected void onPostExecute(IOException error) { + protected void onPostExecute(IOException error) + { if (callback != null) { HashMap args = new HashMap<>(); args.put("success", error == null); @@ -181,7 +202,8 @@ protected void onPostExecute(IOException error) { callback.call(getKrollObject(), args); } } - }.execute(); + } + .execute(); } public String loadJSONFromAsset(String filename) @@ -190,14 +212,13 @@ public String loadJSONFromAsset(String filename) try { String url = this.resolveUrl(null, filename); - Log.d(LCAT, "JSON Path: " + url); - InputStream inStream = TiFileFactory.createTitaniumFile(new String[] { url }, false).getInputStream(); byte[] buffer = new byte[inStream.available()]; inStream.read(buffer); inStream.close(); json = new String(buffer, "UTF-8"); } catch (IOException ex) { + Log.e(LCAT, "Error opening file: " + ex.getMessage()); return ""; } return json;