我需要将通知从java服务器推送到移动设备。 我尝试使用Firebase,但在我看来,它需要开发一个移动应用程序。 相反,我不想开发移动应用程序。 我在下面写了java服务器代码,我想(希望)收件人 我的notisifications设备只需安装Firebase移动应用程序并注册到我在Firebase上创建的项目,以便接收通知。 事实并非如此,因为如果没有编写合适的应用程序我无法检索用户令牌ID,我错了吗?
有些东西按照我描述的方式工作吗?
由于
public class TESTCLASS {
// Method to send Notifications from server to client end.
// AUTH_KEY_FCM obtained by the Firebase project creation
public final static String AUTH_KEY_FCM = "AAAAmenHuaY:APA9....BSmNry7BWL";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public final static String USER_DEVICE_TOKEN = "???????";
private void sendAndroidNotification(String deviceToken) 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 obj = new JSONObject();
JSONObject msgObject = new JSONObject();
msgObject.put("body", "Hello First Test notification");
msgObject.put("title", "FCM Notificatoin Title");
obj.put("to", deviceToken);
obj.put("notification",msgObject);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(obj.toString());
wr.flush();
wr.close();
int responseCode = conn.getResponseCode();
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
System.out.println("---- main");
TESTCLASS obj = new TESTCLASS();
try {
obj.sendAndroidNotification(USER_DEVICE_TOKEN);
} catch (Exception ex) {
System.out.println("---- " + ex.getMessage());
}
}
}