gpt4 book ai didi

java - 在 Openshift 上发布网站后,无法在 Android 应用程序上获取 FCM 有效负载消息

转载 作者:太空宇宙 更新时间:2023-11-04 11:33:57 25 4
gpt4 key购买 nike

我有一个 JSP Servlet Web 应用程序,当客户使用 Web 应用程序输入一个条目时,该应用程序会向 Android 应用程序生成 FCM 有效负载消息。当我在本地环境中测试时一切正常,但现在我在 OPENSHIFT 上发布了网站并尝试使用网站输入一个条目。现在我无法在 Android 应用程序上获取 FCM 有效负载消息。可能是什么问题呢?我的 Android 应用程序尚未在 Google Play 商店上发布。我使用了所有必需的代码,即

公共(public)最终静态字符串AUTH_KEY_FCM =“xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx”; 公共(public)最终静态字符串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":************,"success":1,"failure":1,"canoni‌cal_ids":0,"results"‌​:[{"error":"NotRegist‌​tered"},{"message_id‌​":"0:************"}]}

在 fcmServerResponse.toString() 下面的代码中是来自 FCM 的响应。将其打印在网页中,以便可以看到错误或成功消息。

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

关于java - 在 Openshift 上发布网站后,无法在 Android 应用程序上获取 FCM 有效负载消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43487417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com