我有一个JSP Servlet Web应用程序,当客户使用Web应用程序输入一个条目时,应用程序将FCM有效负载消息生成到Android应用程序。当我在本地环境上测试它时,一切都很好,但是现在我在OpenShift上发布了该网站,并尝试使用网站输入一个条目。现在,我无法在我的Android应用程序上获取FCM有效载荷消息。可能是什么问题呢?我的Android应用程序尚未在Google Play商店发布。我使用了所有必需的代码,即
公共最终静态字符串auth_key_fcm =" xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx x"; 公共最终静态字符串api_url_fcm =" https://fcm.googleapis.com/fcm/send";
它在任何机器上都可以正常工作,但是在OpenShift上发布网站后,它不会向Android应用发送任何消息。
// This class is created to send data payload to the FCM to Android app....
public class SendDataPayload {
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
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 void pushFCMNotification(String userDeviceIdKey, int y_Id, String f_name, String l_name, String m_number,
String puja_name, String puja_date, String puja_time, String p_address, String p_landmark, String p_taluka,
String p_district, String p_pincode) throws Exception {
String authKey = AUTH_KEY_FCM; // Your 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()); // device token...
// JSONObject info = new JSONObject();
// info.put("title", "xyz"); // Notification title
// info.put("body", "my message body is here!!!");
// // Notification body
String timeStamp = new SimpleDateFormat("dd/MM/yyyy hh:mm a").format(Calendar.getInstance().getTime());
String date = timeStamp;
JSONObject data = new JSONObject();
String first_name_ = URLEncoder.encode(f_name, "UTF-8");
String last_name_ = URLEncoder.encode(l_name, "UTF-8");
String santrika_sandesh = URLEncoder.encode("msg-MY", "UTF-8");
String M_number = URLEncoder.encode(m_number, "UTF-8");
String Puja_name = URLEncoder.encode(puja_name, "UTF-8");
String Puja_date = URLEncoder.encode(puja_date, "UTF-8");
String Puja_time = URLEncoder.encode(puja_time, "UTF-8");
String P_address = URLEncoder.encode(p_address, "UTF-8");
String P_landmark = URLEncoder.encode(p_landmark, "UTF-8");
String P_taluka = URLEncoder.encode(p_taluka, "UTF-8");
String P_district = URLEncoder.encode(p_district, "UTF-8");
data.put("key1", santrika_sandesh); // DataPayload
data.put("key2", date); // DataPayload
data.put("y_Id", y_Id);
data.put("f_name", first_name_); // Yajman first name
data.put("l_name", last_name_);
data.put("m_number", M_number);
data.put("puja_name", Puja_name);
data.put("puja_date", Puja_date);
data.put("puja_time", Puja_time);
data.put("puja_add", P_address);
data.put("puja_landmark", P_landmark);
data.put("puja_tal", P_taluka);
data.put("puja_dist", P_district);
data.put("puja_pincode", p_pincode);
// send notification and payload data to FCM....
// json.put("notification", info);
json.put("data", data);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(json.toString());
wr.flush();
conn.getInputStream();
}
}
请参阅https://firebase.google.com/docs/cloud-messaging/send-message#http_response
FCM将带回成功或错误的HTTP响应。
FCM响应示例{" MultiCast_Id":************,"成功":1,"失败":1," CanoniCal_ids":0,","结果" :[{{{" error":" notregis tered"},{" message_id":" 0:**************}}}
在fcmserverresponse.tostring((下方的代码中,是从fcmm中响应的网页,因此可以看到错误或成功消息。
StringBuilder fcmServerResponse = new StringBuilder();
BufferedReader in = new BufferedReader(
new InputStreamReader(conn.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) {
fcmServerResponse.append(inputLine);
}
fcmServerResponse.toString()//This is response from FCM