Advertisement
urksiful

Notifications Sending between two devices

Aug 1st, 2018
540
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. //First you need to add a OkhttpCliente dependency on your App Gradle File.
  2. //You can find them in the next URL: https://github.com/square/okhttp
  3.  
  4.  
  5. private void sendNotification(String regToken) {
  6.         final String regToken2 = regToken;
  7.         final String LEGACY_SERVER_KEY = "AIzaSyD6QliPCOBwISMu35_r_MzxSY6uc0gLd7s";
  8.         final MediaType JSON
  9.                 = MediaType.parse("application/json; charset=utf-8");
  10.  
  11.             new AsyncTask<Void,Void,Void>(){
  12.                 @Override
  13.                 protected Void doInBackground(Void... params) {
  14.                     try {
  15.                         OkHttpClient client = new OkHttpClient();
  16.                         JSONObject json=new JSONObject();
  17.                         JSONObject dataJson=new JSONObject();
  18.                         dataJson.put("body","Hi this is sent from device to device");
  19.                         dataJson.put("title","dummy title");
  20.                         dataJson.put("sound", "/uri/audio/listen.mp3");
  21.                         json.put("notification",dataJson);
  22.  
  23.                         json.put("to",regToken2);
  24.                         RequestBody body = RequestBody.create(JSON, json.toString());
  25.                         Request request = new Request.Builder()
  26.                                 .header("Authorization","key="+ LEGACY_SERVER_KEY)
  27.                                 .url("https://fcm.googleapis.com/fcm/send")
  28.                                 .post(body)
  29.                                 .build();
  30.                         Response response = client.newCall(request).execute();
  31.                         String finalResponse = response.body().string();
  32.                     }catch (Exception e){
  33.                         //Log.d(TAG,e+"");
  34.                     }
  35.                     return null;
  36.                 }
  37.             }.execute();
  38.  
  39.  
  40.  
  41.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement