2017-06-06 77 views
0

我已经用MySQL数据库构建了java项目。我想将这个Java项目导入到Firebase中。请指导如何在Firebase中创建数据库以及如何将Java准备项目导入到Firebase中?如何将Java项目导入Firebase并创建数据库?

+0

查看评论[这里](https://stackoverflow.com/q/43855772/4625829)(具体来说,弗兰克van puffelen的)。 –

回答

0

第一次访问https://firebase.google.com/了解更多详情 然后根据您所需的平台创建一个新项目。

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

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

生成FCM_AUTH_KEY 创建像

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); 

    } 
} 

这个辅助类的Java Helper类可以发送FCM通知到客户端