Java EE FirebaseApp名称[DEFAULT]已存在 [英] Java EE FirebaseApp name [DEFAULT] already exists

查看:295
本文介绍了Java EE FirebaseApp名称[DEFAULT]已存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了与Firebase和Java EE相关的问题。



我正在为我的项目编写一些Java servlet,我第一次使用Firebase我想尝试新的东西。



我的实际问题如下:
我有一个servlet负责在用户数据库中交换iOS设备令牌。这对于将远程推送通知发送到设备是必需的。
我在google教程中完成了这个,但是我得到了以下异常:

  java。 lang.IllegalStateException:FirebaseApp名称[DEFAULT]已经存在! 

我访问Firebase数据库的方式是通过Java SDK。



我使用以下代码执行此操作:



连接方法

  //由servlet调用以配置Firebase 
public static void connect(){
try {
//用于身份验证目的
Map< String,Object> auth = new HashMap<>();
auth.put(uid,my-service-account);

//设置FirebaseOptions对象
//常量FIREBASE_DATABASE_URL =我的数据库的url
//常量FIREBASE_KEY_PATH =我的json密钥的路径
options = new FirebaseOptions.Builder()
.setDatabaseUrl(FIREBASE_DATABASE_URL)
.setServiceAccount(new FileInputStream(FIREBASE_KEY_PATH))
.setDatabaseAuthVariableOverride(auth)
.build();

FirebaseApp.initializeApp(选项);

//调用交换令牌的方法
exchangeIosDeviceToken(testmail@example.com,5bf53173c9ef0a37638f3ddaa59cf2c0687c14ca0dcd47ccf57f9f09bd6368ab);
} catch(FileNotFoundException ex){
ex.printStackTrace();
}
}

exchangeIosDeviceToken方法

  public static boolean exchangeIosDeviceToken(String email,String newDeviceToken){
FirebaseDatabase database = FirebaseDatabase.getInstance();

//获取对我的employee子项的引用
DatabaseReference employeeReference = database.getReference(/ employee);

Map< String,Object> employeeUpdates = new HashMap<>();
//使用employee的子iosDeviceToken更新设备令牌
employeeUpdates.put(email +/ iosDeviceToken,newDeviceToken);

//更新实际的孩子
employeeReference.updateChildren(employeeUpdates);
返回true;
}

有趣的部分是当我尝试独立执行此代码时主类(用main方法替换connect方法),代码正常工作。



在你说有很多与此相关的问题之前主题......它们几乎都与Android有关,与我的问题相关的问题很少得到解答。



问候



问题是:
我是每次请求传入时都会调用connect方法。



解决方案:
只调用一次connect方法。 (ServletContextListener)


I got a problem related to Firebase and Java EE.

I'm currently writing some Java servlets for my project and I'm using Firebase the first time because I wanted to try something new.

My actual problem is the following: I got a servlet which is responsible for exchanging an iOS device token in an user database. This is necessary for sending Remote Push Notifications to a device. I've done this like in the google tutorials, but I'm getting the following exception:

java.lang.IllegalStateException: FirebaseApp name [DEFAULT] already exists!

The way I'm accessing the Firebase Database is through the Java SDK.

I do this with the following code:

connect method

    // gets called by the servlet to configure Firebase
    public static void connect() {
      try {
        // for authentication purpose
        Map<String, Object> auth = new HashMap<>();
        auth.put("uid", "my-service-account");

        // Setting up the FirebaseOptions object
        // constant FIREBASE_DATABASE_URL = "url to my database"
        // constant FIREBASE_KEY_PATH = "path to my json key"
        options = new FirebaseOptions.Builder()
                  .setDatabaseUrl(FIREBASE_DATABASE_URL)
                  .setServiceAccount(new FileInputStream(FIREBASE_KEY_PATH))
                  .setDatabaseAuthVariableOverride(auth)
                  .build();

        FirebaseApp.initializeApp(options);

        // calling the method for exchanging the token
        exchangeIosDeviceToken("testmail@example.com", "5bf53173c9ef0a37638f3ddaa59cf2c0687c14ca0dcd47ccf57f9f09bd6368ab");
      } catch (FileNotFoundException ex) {
        ex.printStackTrace();
      }
    }

exchangeIosDeviceToken method

    public static boolean exchangeIosDeviceToken(String email, String newDeviceToken) {
      FirebaseDatabase database = FirebaseDatabase.getInstance();

      // getting a reference to my "employee" child
      DatabaseReference employeeReference = database.getReference("/employee");

      Map<String, Object> employeeUpdates = new HashMap<>();
      // updating the device token with child "iosDeviceToken" of "employee"
      employeeUpdates.put(email+"/iosDeviceToken", newDeviceToken);

      // update the actual children
      employeeReference.updateChildren(employeeUpdates);
      return true;
    }

The funny part is when I'm trying to execute this code in a standalone main class (replacing the connect method, with the main method), the code is working.

Before you're saying things like "there are tons of questions related to this topic"... They are nearly all related to Android and questions related to my problem seldom got answered.

Regards

解决方案

Solved the problem.

The problem was: I've called the connect method everytime a request was incoming.

Solution: Call the connect method only once. (ServletContextListener)

这篇关于Java EE FirebaseApp名称[DEFAULT]已存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆