title: How to initialize Firebase Cloud Messaging in a lower library?
body: <p>I moved Firebase Cloud Messaging to a library, so that I can import this into any of my apps and provide push messages. So therefore I created a library and moved everything <a href="https://www.codementor.io/flame3/send-push-notifications-to-android-with-firebase-du10860kb" rel="nofollow noreferrer">from this example</a> to my library.</p>
  
  <p>Then I compile this library in my <code>build.gradle</code> and use it as desired.
  This works so far when I instantiate Firebase from app level:</p>
  
  <pre><code>FirebaseMessaging.getInstance().subscribeToTopic(topic)
  </code></pre>
  
  <p>Now, I thought of moving this down into my library:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>Since I do not hand over the app level context, I get:</p>
  
  <pre><code>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) 
  </code></pre>
  
  <p>ADDENDUM: I added that line to my init</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>leads to very same error message.</p>