Last active
October 9, 2018 10:54
-
-
Save malikkurosaki/7e442aa72635e216cf6b6f951c25c105 to your computer and use it in GitHub Desktop.
malikkurosaki kirim brita membuat objeck untuk mengirim notification secara langsung
This file contains hidden or 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
package com.malik.rajanepikep; | |
import android.os.AsyncTask; | |
import android.util.Log; | |
import android.widget.Toast; | |
import org.json.JSONObject; | |
import java.io.OutputStreamWriter; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class KirimBerita extends AsyncTask<Void,Void,Void> { | |
String tkn; | |
public KirimBerita(String tkn) { | |
this.tkn = tkn; | |
} | |
@Override | |
protected Void doInBackground(Void... voids) { | |
Log.d("TOKEN",tkn); | |
try { | |
URL url = new URL("https://fcm.googleapis.com/fcm/send"); | |
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); | |
conn.setUseCaches(false); | |
conn.setDoInput(true); | |
conn.setDoOutput(true); | |
conn.setRequestMethod("POST"); | |
conn.setRequestProperty("Authorization","key=AIzaSyAzqtlyNBKC3OxbhNUliaKZ9fMUuvqeXbI"); | |
conn.setRequestProperty("Content-Type", "application/json"); | |
JSONObject json = new JSONObject(); | |
json.put("to","/topics/macan"); | |
JSONObject info = new JSONObject(); | |
info.put("title", "TechnoWeb"); // Notification title | |
info.put("body", "Hello Test notification"); // Notification body | |
json.put("notification", info); | |
JSONObject datanya = new JSONObject(); | |
datanya.put("body","ini bodynya"); | |
datanya.put("title","ini title data"); | |
json.put("data",datanya); | |
Log.d("JSON_TO_SEND","jsonnya adalah = "+json); | |
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); | |
wr.write(json.toString()); | |
wr.flush(); | |
conn.getInputStream(); | |
} | |
catch (Exception e) | |
{ | |
Log.d("Error",""+e); | |
} | |
return null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment