java - 如何将 Java 项目导入 Firebase 并创建数据库?

标签 java mysql firebase firebase-realtime-database

我用 MySQL 数据库构建了 java 项目。我想将这个java项目导入到firebase中。请指导如何在firebase中创建数据库以及如何将java就绪项目导入firebase?

最佳答案

首次访问https://firebase.google.com/更多细节 然后根据您所需的平台创建一个新项目。

在服务器端创建一个表来存储用户 FCM key

Table: fcmkeys
Columns:
user varchar(10) PK 
fcmkey varchar(1045) 

生成 FCM_AUTH_KEY 创建一个 Java Helper 类,例如

public class FCMHelper {

    static Logger log = Logger.getLogger(FCMHelper.class);
    // Method to send Notifications from server to client end.

    public final static String AUTH_KEY_FCM = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

    public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";

    // userDeviceIdKey is the device id you will query from your database

    public static void pushFCMNotification(String userDeviceIdKey ,String NotificationString) throws Exception{

       String authKey = AUTH_KEY_FCM; // You FCM AUTH key
       String FMCurl = API_URL_FCM; 

       URL url = new URL(FMCurl);
       HttpURLConnection conn = (HttpURLConnection) url.openConnection();

       conn.setUseCaches(false);
       conn.setDoInput(true);
       conn.setDoOutput(true);

       conn.setRequestMethod("POST");
       conn.setRequestProperty("Authorization","key="+authKey);
       conn.setRequestProperty("Content-Type","application/json");

       JSONObject json = new JSONObject();
       json.put("to",userDeviceIdKey.trim());
       JSONObject info = new JSONObject();
      // info.put("title", "Alert!"); // Notification title
       info.put("body", NotificationString); // Notification body
       json.put("data", info);

       OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
       wr.write(json.toString());
       wr.flush();
       conn.getInputStream();
       System.out.println("Notification Send Success!");
       System.out.println( conn.getInputStream().toString());

       InputStream in = new BufferedInputStream(conn.getInputStream());
        BufferedReader streamReader = new BufferedReader(new InputStreamReader(in, "UTF-8"));
        StringBuilder responseStrBuilder = new StringBuilder();

        String inputStr;
        while ((inputStr = streamReader.readLine()) != null)
            responseStrBuilder.append(inputStr);
        String s = responseStrBuilder.toString();
         System.out.println( s);
         log.info(s);

    }
}

该Helper类可以向客户端发送FCM通知

关于java - 如何将 Java 项目导入 Firebase 并创建数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44382497/

相关文章:

java - 使用 HttpServlet 的 MS Word 文件下载问题

java - SimpleDateFormat 解析错误的日期

java - 如何使用java的模式匹配器匹配包含多行的字符串中的单词

mysql - 在两个 sql 字段中测试等效性的最佳方法

mysql - Rails Mysql - 查找以空格开头的所有值

android - 获取 Firebase 测试实验室中的可用设备列表

firebase - 云 Firestore : Filter based on object content (via Flutter)

java - java 中缺少用户定义的数据类型

javascript - 'firebase' 不是内部或外部命令,也不是可运行的程序或批处理文件

mysql - 如何使用 MySQL 主键作为变量