3

I am trying to make an app based on Cordova that is able to send Notifications via FireBase Cloud Messaging

Here the MainActivity.java

  public class MainActivity extends CordovaActivity
{

@Override
public void onCreate(Bundle savedInstanceState)
{
    Intent i=new Intent(this,NotificationService.class);
    startService(i);
    super.onCreate(savedInstanceState);
    // Set by <content src="index.html" /> in config.xml
    Helper mhelper=new Helper(this,appView);
        FirebaseMessaging.getInstance().subscribeToTopic("Testing");
        Log.d("Message","Subscribed To Testing");


    Log.d("Token",FirebaseInstanceId.getInstance().getToken());
    loadUrl(launchUrl);


}
}

However I am getting a Illegal State Exception at Line 49:

    FirebaseMessaging.getInstance().subscribeToTopic("Testing");

The rest Manifest.xml etc. are the same as the sample provided here https://github.com/firebase/quickstart-android/tree/master/messaging

In the Logcat, the error it is showing is::

 FATAL EXCEPTION: main
                                                                   Process: careerage.jobseeker.app, PID: 5764
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{careerage.jobseeker.app/careerage.jobseeker.app.MainActivity}: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738)
                                                                       at android.app.ActivityThread.access$900(ActivityThread.java:135)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:136)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                       at java.lang.reflect.Method.invokeNative(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:515)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                       at dalvik.system.NativeStart.main(Native Method)
                                                                    Caused by: java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] doesn't exist. 
                                                                       at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                       at com.google.firebase.FirebaseApp.getInstance(Unknown Source)
                                                                       at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
                                                                       at com.google.firebase.messaging.FirebaseMessaging.getInstance(Unknown Source)
                                                                       at careerage.jobseeker.app.MainActivity$override.onCreate(MainActivity.java:49)
                                                                       at careerage.jobseeker.app.MainActivity$override.access$dispatch(MainActivity.java)
                                                                       at careerage.jobseeker.app.MainActivity.onCreate(MainActivity.java:0)
                                                                       at android.app.Activity.performCreate(Activity.java:5231)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                       at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3738) 
                                                                       at android.app.ActivityThread.access$900(ActivityThread.java:135) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:136) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                       at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                       at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                       at dalvik.system.NativeStart.main(Native Method) 

I tried java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] but still it doesn't recognize FirebaseDatabase Class

Community
  • 1
  • 1
Yash Jain
  • 376
  • 1
  • 3
  • 13
  • http://stackoverflow.com/questions/37346363/java-lang-illegalstateexception-firebaseapp-with-name-default – USKMobility May 28 '16 at 06:31
  • @USKMobility I tried that too. But I am not using Firebase Database. So there isn't any class present in my library – Yash Jain May 28 '16 at 17:24
  • I am not an expert in Cordova and its build system, but firebase sdk requires gradle androidmanifest-merge feature to add extra components (defined in the firebase libraries) to your androidmanifest.xml file. Can you check that your final AndroidManifest includes a content-provider created by Firebase ? – Diego Giorgini May 29 '16 at 18:27
  • I think I figured it out. I think Fcm and Gcm take some time to get tokens.. If I try to do anything with cloud messaging without the token being assigned I will get error – Yash Jain Jun 09 '16 at 13:56

2 Answers2

1

Try updating your dependency to:

compile 'com.google.firebase:firebase-messaging:9.0.2'

This bug was fixed in patches between 9.0.0 and 9.0.2

Anshu Dwibhashi
  • 4,617
  • 3
  • 28
  • 59
0

I think Fcm and Gcm take some time to get tokens.. If you try to do anything with cloud messaging without the token being assigned you will get error.

Yash Jain
  • 376
  • 1
  • 3
  • 13