gpt4 book ai didi

android - 如何在下层库中初始化Firebase Cloud Messaging?

转载 作者:行者123 更新时间:2023-11-30 00:20:22 26 4
gpt4 key购买 nike

我将 Firebase Cloud Messaging 移到了一个库中,这样我就可以将它导入到我的任何应用程序中并提供推送消息。因此,我创建了一个库并移动了所有内容 from this example到我的图书馆。

然后我在我的 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

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
}

导致完全相同的错误消息。

最佳答案

你的错误

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

在你的库中,你需要创建像

这样的函数
public static void Init(Context context) {
FirebaseApp.initializeApp(context);
}

并在使用 firebase 之前调用它。将其添加到您的应用程序的最佳方式示例:

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

关于android - 如何在下层库中初始化Firebase Cloud Messaging?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46409258/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com