Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //First you need to add a OkhttpCliente dependency on your App Gradle File.
- //You can find them in the next URL: https://github.com/square/okhttp
- private void sendNotification(String regToken) {
- final String regToken2 = regToken;
- final String LEGACY_SERVER_KEY = "AIzaSyD6QliPCOBwISMu35_r_MzxSY6uc0gLd7s";
- final MediaType JSON
- = MediaType.parse("application/json; charset=utf-8");
- new AsyncTask<Void,Void,Void>(){
- @Override
- protected Void doInBackground(Void... params) {
- try {
- OkHttpClient client = new OkHttpClient();
- JSONObject json=new JSONObject();
- JSONObject dataJson=new JSONObject();
- dataJson.put("body","Hi this is sent from device to device");
- dataJson.put("title","dummy title");
- dataJson.put("sound", "/uri/audio/listen.mp3");
- json.put("notification",dataJson);
- json.put("to",regToken2);
- RequestBody body = RequestBody.create(JSON, json.toString());
- Request request = new Request.Builder()
- .header("Authorization","key="+ LEGACY_SERVER_KEY)
- .url("https://fcm.googleapis.com/fcm/send")
- .post(body)
- .build();
- Response response = client.newCall(request).execute();
- String finalResponse = response.body().string();
- }catch (Exception e){
- //Log.d(TAG,e+"");
- }
- return null;
- }
- }.execute();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement