Last active
December 30, 2018 18:14
-
-
Save idurucz/8807d7294960c30aa2af443c01bb8e9f to your computer and use it in GitHub Desktop.
Java Http POST to send JSON push notification
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.io.OutputStreamWriter; | |
import javax.net.ssl.HttpsURLConnection; | |
import java.net.URL; | |
import org.apache.sling.commons.json.JSONException; | |
import org.apache.sling.commons.json.JSONObject; | |
public class SendFCMPushMessage { | |
public static void main(String[] args) throws IOException, JSONException { | |
URL url = new URL("https://fcm.googleapis.com/fcm/send"); | |
HttpsURLConnection client; | |
client = (HttpsURLConnection) url.openConnection(); | |
client.setDoOutput(true); | |
client.setDoInput(true); | |
client.setRequestMethod("POST"); | |
client.setRequestProperty("Content-Type","application/json"); | |
client.setRequestProperty("Authorization","key=fcm_server_key_comes_here"); | |
client.setRequestProperty("Accept", "application/json"); | |
JSONObject message = new JSONObject(); | |
JSONObject pushRequest = new JSONObject(); | |
message.put("title", "message_title_comes_here"); | |
message.put("body", "message_body_comes_here"); | |
pushRequest.put("notification", message); | |
pushRequest.put("to", "client_token_comes_here"); | |
OutputStreamWriter wr = new OutputStreamWriter(client.getOutputStream()); | |
wr.write(pushRequest.toString()); | |
wr.flush(); | |
client.getResponseCode(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment