0
我已經用MySQL數據庫構建了java項目。我想將這個Java項目導入到Firebase中。請指導如何在Firebase中創建數據庫以及如何將Java準備項目導入到Firebase中?如何將Java項目導入Firebase並創建數據庫?
我已經用MySQL數據庫構建了java項目。我想將這個Java項目導入到Firebase中。請指導如何在Firebase中創建數據庫以及如何將Java準備項目導入到Firebase中?如何將Java項目導入Firebase並創建數據庫?
第一次訪問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通知到客戶端
查看評論[這裏](https://stackoverflow.com/q/43855772/4625829)(具體來說,弗蘭克van puffelen的)。 –