3

我正在尝试制作一个基于 Cordova 的应用程序,该应用程序能够通过 FireBase Cloud Messaging 发送通知

这里的 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);


}
}

但是,我在第 49 行遇到了非法状态异常:

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

其余 Manifest.xml 等与此处提供的示例相同 https://github.com/firebase/quickstart-android/tree/master/messaging

在 Logcat 中,它显示的错误是:

 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) 

我尝试了 java.lang.IllegalStateException: FirebaseApp with name [DEFAULT] 但它仍然无法识别 FirebaseDatabase 类

4

2 回答 2

1

尝试将您的依赖项更新为:

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

此错误已在 9.0.0 和 9.0.2 之间的补丁中修复

于 2016-07-03T07:05:22.380 回答
0

我认为 Fcm 和 Gcm 需要一些时间来获取令牌。如果您在没有分配令牌的情况下尝试使用云消息传递进行任何操作,您将收到错误消息。

于 2016-06-09T13:57:21.520 回答