2017-09-25 4 views
1

私はFirebase Cloud Messagingをライブラリに移動しました。これを私のアプリケーションにインポートしてプッシュメッセージを提供することができます。だから私はライブラリを作成し、from this exampleをすべて私のライブラリに移動しました。Firebase Cloud Messagingを下位のライブラリで初期化するにはどうすればいいですか?

次に、このライブラリをbuild.gradleにコンパイルし、必要に応じて使用します。私はアプリレベルからFirebaseをインスタンス化するとき これは、これまでの作品:

FirebaseMessaging.getInstance().subscribeToTopic(topic) 

、私は自分のライブラリーにこれを下に移動を考えた:

fun initFirebaseMessaging(topic : String) : String 
{ 
    FirebaseMessaging.getInstance().subscribeToTopic(topic) 
    Timber.d("Push subscribeToTopic $topic") 
    val token = FirebaseInstanceId.getInstance().token!! 
    Timber.d("Push FirebaseinstanceId token $token") 
    sendRegistrationToServer(token) 
    return token 
} 

私はアプリレベルのコンテキストを引き渡すないので、私が取得:

E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.example.demo, PID: 8047 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demo/com.example.MainActivity}: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.demo. Make sure to call FirebaseApp.initializeApp(Context) first. 
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
                    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
                    at android.os.Handler.dispatchMessage(Handler.java:105) 
                    at android.os.Looper.loop(Looper.java:164) 
                    at android.app.ActivityThread.main(ActivityThread.java:6541) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 
                   Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.example.demo. Make sure to call FirebaseApp.initializeApp(Context) first. 
                    at com.google.firebase.FirebaseApp.getInstance(Unknown Source:58) 
                    at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source:0) 
                    at com.google.firebase.messaging.FirebaseMessaging.getInstance(Unknown Source:9) 
                    at com.example.network.fcm.exampleFirebaseInstanceIdService$Companion.initFirebaseMessaging(exampleFirebaseInstanceIdService.kt:14) 
                    at com.example.MainActivity.onCreate(MainActivity.kt:168) 
                    at android.app.Activity.performCreate(Activity.java:6975) 
                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) 
                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) 
                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  
                    at android.app.ActivityThread.-wrap11(Unknown Source:0)  
                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  
                    at android.os.Handler.dispatchMessage(Handler.java:105)  
                    at android.os.Looper.loop(Looper.java:164)  
                    at android.app.ActivityThread.main(ActivityThread.java:6541)  
                    at java.lang.reflect.Method.invoke(Native Method)  
                    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  

補遺:私は私のinitに

をその行を追加しました

と同じエラーメッセージが表示されます。あなたのライブラリーで

答えて

1

あなたのエラー

Make sure to call FirebaseApp.initializeApp(Context) first. 

uが

public static void Init(Context context) { 
FirebaseApp.initializeApp(context); 
} 

のような関数を作成し、firebaseを使用する前に、それを呼び出す必要があります。最良の方法は、そのアプリケーションに 例を追加します。上記の補遺フィールドに

... extends Application /*** code **/ 
onCreate() { 
YourLibrary.Init(this); 
} 
+0

を参照してください、それはuはグーグル-JSONないファイルを同じエラーメッセージ –

+0

につながりますか? あなたはgraleクラスパス 'com.google.gms:google-services:3.0.0'をビルドしていますか? – Peter

+0

コメントによると、https://stackoverflow.com/questions/44229961/unknown-libraryvariants-property-gradle-will-not-syncアプリのレベルでのみ適用プラグインを使用できますが、libでは使用できませんレベル? –

関連する問題