2017-09-25 37 views
1

我將Firebase雲消息傳送到庫中,以便我可以將其導入到我的任何應用程序中並提供推送消息。所以我創建了一個庫,並把所有的東西都搬到了我的庫中。如何初始化較低庫中的Firebase雲消息傳遞?

然後我編譯這個庫在我的build.gradle並根據需要使用它。 到目前爲止是這種情況,當我實例火力地堡從應用層面:

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)  

附錄:我補充說,行到我的初始化

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

導致非常相同的錯誤消息。

回答

1

你的錯誤

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

在您的圖書館ü需要像

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

創建功能和使用火力點之前調用它。最好的辦法其添加到您的應用程序 例如:

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

請參閱上述附錄領域,它會導致同樣的錯誤消息 –

+0

做u谷歌JSON文件? 你有沒有在構建gradle classpath'com.google.gms:google-services:3.0.0'? – Peter

+0

根據這裏的評論:https://stackoverflow.com/questions/44229961/unknown-libraryvariants-property-gradle-will-not-sync我能夠在應用程序級別使用應用程序插件,而不是在lib水平? –

相關問題