1
我正在向我的android應用程序添加通知,但似乎無法通過我的java代碼發送通知。我從迴應中獲得200個http代碼,但沒有顯示出來。使用Java發送Firebase通知,而不是FCM控制檯
這適用於Firebase控制檯本身,但是一旦我嘗試使用我的Java代碼,事情就會發生。
這是我的代碼如下
package actions.authentication;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import com.opensymphony.xwork2.Action;
import edocs.actions.PublicAction;
import net.sf.json.JSONObject;
public class SendPushNotification extends PublicAction {
/**
*
*/
private static final long serialVersionUID = -8022560668279505764L;
// Method to send Notifications from server to client end.
public final static String AUTH_KEY_FCM = "SERVER_KEY_HERE";
public final static String API_URL_FCM = "https://fcm.googleapis.com/fcm/send";
public final static String DEVICE_ID = "DEVICE_TOKEN_HERE";
public String execute() {
String DeviceIdKey = DEVICE_ID;
String authKey = AUTH_KEY_FCM;
String FMCurl = API_URL_FCM;
try {
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");
System.out.println(DeviceIdKey);
JSONObject data = new JSONObject();
data.put("to", DeviceIdKey.trim());
JSONObject info = new JSONObject();
info.put("title", "FCM Notificatoin Title"); // Notification title
info.put("body", "Hello First Test notification"); // Notification body
data.put("data", info);
System.out.println(data.toString());
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(data.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();
return Action.SUCCESS;
}
catch(Exception e)
{
System.out.println(e);
}
return Action.SUCCESS;
}
}
線System.out.println("Response Code : " + responseCode);
打印出以下
響應代碼:200
任何人都有這個問題?任何幫助將不勝感激
是的!有效!這是有道理的,你必須將信息推入通知。非常感謝! – Pooshonk