14

我想在我想在許多應用程序中用作SDK的庫中實現Friebase通知系統。在庫中實現Firebase

Firebase現在要求提供應用ID,但我正在庫中實現它,因此沒有應用ID。

我怎樣才能實現我的目標,能夠發送通知給我的應用程序使用我的圖書館?

在此先感謝。

+0

我會這樣想,通過從你的主應用程序庫中初始化庫,將App ID傳遞到庫中。 –

+0

嘿@Sekai如果我的答案確實解決了您的問題,請將其標記爲已接受(SO政策) –

回答

6

是的,你可以真正做到這一點,您的圖書館build.gradle把這個defaultConfig領域內

buildConfigField("String", "FIREBASE_APP_KEY", "\"${firebaseAppKey}\"") 

內。然後你的項目的gradle.properties

firebaseAppKey = <yourFirebaseAppSecret>;

對於每個項目/應用程序,你必須在你的gradle.properties上定義這個變量。

您必須爲每個項目創建一個Firebase應用,但您的圖書館現在可以擁有Firebase SDK。

當您想訪問此環境變量值時,請使用BuildConfig.FIREBASE_APP_KEY (例如實例化Firebase)。

+0

我一直希望有一個解決方案需要開發人員進行最少的操作。我們將嘗試這一點,如果它不工作,我們會嘗試實施套接字通知 – Sekai

+0

@Sekai我認爲只配置gradle文件是可以接受的。如果你像這樣預設你的圖書館,我作爲開發人員會很高興。您的「客戶」唯一需要做的就是創建他自己的Firebase應用程序(沒有辦法解決這個問題)並使用他自己的密鑰,這樣一來,如果您沒有上傳您的「gradle」,密鑰就不會暴露在任何地方。屬性「(你不應該)。 –

5

一種選擇是讓您的圖書館的用戶創建一個Firebase項目,然後將生成的google-services.json文件傳入他們的應用程序,然後您的圖書館可以依賴該項目。

0

據我所知,你不能。

每個Firebase項目都需要包ID以唯一標識其每個應用程序。

您還需要爲每個模塊配置其自己的google-services.json,這是爲每個ID專門生成的。

如果您可以對所有應用程序使用相同的軟件包,設備或Play商店就無法將其與另一個區分開來,因此無法做出選擇。

您可以將所有Firebase邏輯提取到您的庫中,但仍然需要配置每個應用程序才能提供唯一的包ID,並且您還需要向每個ID發送通知。

2

我知道這是一個接受答案的老問題,但所有的答案都有一個很大的缺點 - 他們要求你的圖書館的用戶除了將你的圖書館添加到他們的應用程序之外去做其他工作。有一種方法可以做到這一點如果您的圖書館是從Maven存儲庫下載的話,不會讓您的圖書館的用戶感到困擾

注意:這種方法是一種破解方式,不受Firebase支持。當被問及火力地堡的支持,我得到了如下答覆:

火力地堡的SDK不可用於庫工程。Firebase上提供的功能集成在應用程序級別,而不是基於每個模塊或每個庫的 ,因此,將此 集成到庫項目中的用例不可行或不受支持。

不過,我已經找到一種方法來做到這一點,baybe有人會發現它有用所以在這裏,它是:

這是使用實時數據庫的例子,但它應該爲所有的火力地堡的SDK工作。

在項目的主build.gradle添加mavenCentral庫:

allprojects { 
    repositories { 
     ... 
     mavenCentral() 
    } 
} 

在你的庫項目的build.gradle,加入谷歌播放服務(如依賴,而不是作爲一個插件):

compile 'com.google.android.gms:play-services-gcm:11.0.4' 

添加相關的Firebase SDK(與Google Play服務的版本相同):

compile 'com.google.firebase:firebase-core:11.0.4' 
compile 'com.google.firebase:firebase-database:11.0.4' 

在Firebas上註冊您的SDK作爲項目,下載它的google-services.json並用任何文本編輯器打開它。

在圖書館的strings.xml添加以下代碼行,並填寫這些線路的數據與來自google-services.json

<string name="gcm_defaultSenderId">project_number</string> 
<string name="google_api_key">current_key</string> 
<string name="google_app_id">mobilesdk_app_id</string> 
<string name="google_crash_reporting_api_key">current_key</string> 
<string name="google_storage_bucket">storage_bucket</string> 
<string name="firebase_database_url">firebase_url</string> 
<string name="default_web_client_id">client_id</string> 
<string name="project_id">project_id</string> 

這是它。您可以在libaray中使用Firebase實時數據庫,然後構建它並將其發佈到Maven(Maven發佈至關重要,否則您的庫的用戶將不得不手動添加依賴項)。從應用程序內部激活時,將使用您的數據庫。

請注意,如果您的圖書館的用戶使用Google Play服務或Firebase,則此方法可能會導致異常和意外行爲,所以您自擔風險使用

1

這些都是有點hacky或太多的工作,這是一個很好的ñ簡單的例子(雖然諷刺,因爲它是一個很長的職位 - 但值得)。

可以在您的圖書館項目中使用FireBase代碼,當然消費應用程序需要註冊該應用程序並獲取應用程序ID/google-services.json文件。

但是你的圖書館沒有,也不應該在意這一點,這是消費應用程序的工作,而不是你的圖書館。

下面是在庫項目中使用firebase-messaging模塊的簡要示例。

YourLibrary模塊的build.gradle

// Other typical library set up 
apply plugin: 'com.android.library' 

android { 
    compileSdkVersion 27 

    defaultConfig { 
     minSdkVersion 16 
     targetSdkVersion 27 
     versionCode 1 
     versionName '1.0' 

     // Don’t for get your library’s proguard file! 
     consumerProguardFiles 'proguard-rules.pro' 
    } 
} 

ext { 
    currentFirebaseVersion = "11.8.0" 
} 

dependencies { 
    /* 
    Here we depend on the firebase messaging dependency (via compileOnly), 
    allowing us to use the FireBase API within our library module. 

    I exclude that org.json module because it may cause build warnings, this 
    step isn’t totally necessary. 

    NOTE: You should use `compileOnly` here so the dependency is 
    not added to the build output You will be allowed to use the 
    dependency in your library. If the consuming app wants to use firebase 
    they’ll need to depend on it (using `implementation`). 
    */ 
    compileOnly("com.google.firebase:firebase-messaging:$currentFirebaseVersion") { 
     exclude group: 'org.json', module: 'json' 
    } 
} 

// Other typical library set up. But nothing else relating Firebase. 

這是你所需要的庫中的項目做。 請勿在此處應用gms插件,並且不要將google-services類路徑添加到庫build.gradle

現在,這裏是你如何設置你的消費應用:

MyClientApp頂尖級的build.gradle

// Top-level build file where you can add configuration options common to all sub-projects/modules. 

buildscript { 
    repositories { 
     google() // You know the drill... 
    } 
    // Any other set up you might have... 

    dependencies { 
     classpath 'com.android.tools.build:gradle:3.0.1' 
     /* 
     Here in your client app’s top-level build.gradle you add the 
     google-services to the app’s classpath. 
     */ 
     classpath 'com.google.gms:google-services:3.2.0' 
     // NOTE: Do not place your application dependencies here; they belong 
     // in the individual module build.gradle files 
    } 
} 
// Other basic stuff... 
allprojects { 
    apply plugin: 'maven' 
    apply plugin: 'maven-publish' 

    repositories { 
     jcenter() 
     google() 
    } 
} 

現在,我們需要建立消費應用模塊build.gradle,這很簡單。我們幾乎只需要應用插件,並依賴於我們創建的庫模塊,其中包含所有FireBase代碼。

MyClientApp的模塊級的build.gradle

buildscript { 
    repositories { 
     google() 
     mavenLocal() 
    } 
} 
apply plugin: 'com.android.application' 

android { 
    compileSdkVersion 27 
    defaultConfig { 
     applicationId "com.your.application.that.can.use.firebase" 
     minSdkVersion 16 
     targetSdkVersion 27 
     versionCode 1 
     versionName '1.0' 
    } 
    //other typical set up 
} 

ext { 
    currentFirebaseVersion = "11.8.0" 
} 

dependencies { 
    implementation('com.your.library:YourLibrary:[email protected]') { 
     transitive = true 
     // Use the consuming application's FireBase module, so exclude it 
     // from the dependency. (not totally necessary if you use compileOnly 
     // when declaring the dependency in the library project). 
     exclude group: 'com.google.firebase' 
     // Exclude the "plain java" json module to fix build warnings. 
     exclude group: 'org.json', module: 'json' 
    } 

    implementation("com.google.firebase:firebase-messaging:$currentFirebaseVersion") { 
     // Exclude the "plain java" json module to fix build warnings. 
     exclude group: 'org.json', module: 'json' 
    } 
} 

// Needs to be at the bottom of file. 
apply plugin: 'com.google.gms.google-services' 

需要注意以下幾點:

  • 必須在底部(僅在客戶端模塊build.gradle)申請谷歌的服務插件。
  • 取決於其中包含FireBase代碼的庫模塊,但不包括它的FireBase模塊的版本,而支持您自己的依賴版本。
  • 應用程序取決於它自己的FireBase版本。
  • classpath 'com.google.gms:google-services:3.1.1’只能在客戶端應用的頂層build.gradle
  • 當然,您需要註冊客戶端應用程序並將google-services.json放入您的客戶端應用程序的項目中。
  • 定義必要的FirebaseService在你的應用程序的清單(或使用清單合併以及從庫項目將它們合併)
  • google_play_services_version元數據標籤添加到您的客戶端應用程序的清單。
  • 當聲明FireBase依賴關係時,庫可以/應該使用compileOnly

現在,您將可以在您的應用中使用FireBase代碼,該代碼在您的庫中使用FireBase定義。或者你可以讓你的圖書館模塊做所有的FireBase工作!

當然,這通常用於內部庫,因爲像Firebase這樣的框架沒有被設計爲在庫模塊中實現,但有時您需要,所以這是一個簡單的非hacky/sane解決方案。它可以用於通過maven發佈的項目 - 我的圖書館使用它,並且它從來沒有引起任何問題。

更新:

你聲明瞭該程序模塊的Firebase依賴時,應使用compileOnly。通過這樣做,依賴不會被添加到構建輸出中。但是你會被允許在你的庫中使用依賴。如果消費應用程序想要使用Firebase,則需要手動使用它(使用implementation)。這將有助於減少應用程序中的不必要的依賴/膨脹,以及像這樣聲明依賴的「正確」方式。 注意:您可能需要執行運行時檢查以確保該庫在模塊中使用代碼之前可用。

相關問題