Advertisement
rachmadi

FCM Send Message

Oct 18th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.16 KB | None | 0 0
  1. import android.os.AsyncTask;
  2. import android.util.Log;
  3. import android.widget.Toast;
  4.  
  5. import org.json.JSONArray;
  6. import org.json.JSONException;
  7. import org.json.JSONObject;
  8.  
  9. import java.io.IOException;
  10.  
  11. import okhttp3.MediaType;
  12. import okhttp3.OkHttpClient;
  13. import okhttp3.Request;
  14. import okhttp3.RequestBody;
  15. import okhttp3.Response;
  16.  
  17. import static android.content.ContentValues.TAG;
  18.  
  19. /**
  20.  * Created by rachmadi on 10/17/16.
  21.  */
  22.  
  23. public class FcmSendMessage {
  24.  
  25.     public static final String FCM_MESSAGE_URL = "https://fcm.googleapis.com/fcm/send";
  26.     OkHttpClient mClient = new OkHttpClient();
  27.     String SERVER_KEY = "";
  28.     String TAG = "FcmSendMessage";
  29.  
  30.     public void sendMessage(final JSONArray recipients, final String title, final String body,
  31.                             final String icon, final String message) {
  32.  
  33.         new AsyncTask<String, String, String>() {
  34.             @Override
  35.             protected String doInBackground(String... params) {
  36.                 try {
  37.                     JSONObject root = new JSONObject();
  38.                     JSONObject notification = new JSONObject();
  39.                     notification.put("body", body);
  40.                     notification.put("title", title);
  41.                     notification.put("icon", icon);
  42.  
  43.                     JSONObject data = new JSONObject();
  44.                     data.put("message", message);
  45.                     root.put("notification", notification);
  46.                     root.put("data", data);
  47.                     root.put("registration_ids", recipients);
  48.  
  49.                     String result = postToFCM(root.toString());
  50.                     Log.d(TAG, "Result: " + result);
  51.                     return result;
  52.                 } catch (Exception ex) {
  53.                     ex.printStackTrace();
  54.                 }
  55.                 return null;
  56.             }
  57.  
  58.             @Override
  59.             protected void onPostExecute(String result) {
  60.                 try {
  61.                     JSONObject resultJson = new JSONObject(result);
  62.                     int success, failure;
  63.                     success = resultJson.getInt("success");
  64.                     failure = resultJson.getInt("failure");
  65. //                    Toast.makeText(getCurrentActivity(), "Message Success: " + success + "Message Failed: " +
  66. //                      failure, Toast.LENGTH_LONG).show();
  67.                 } catch (JSONException e) {
  68.                     e.printStackTrace();
  69. //                    Toast.makeText(getCurrentActivity(), "Message Failed, Unknown error occurred.",
  70. //                      Toast.LENGTH_LONG).show();
  71.                 }
  72.             }
  73.         }.execute();
  74.     }
  75.  
  76.     String postToFCM(String bodyString) throws IOException {
  77.         MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  78.         RequestBody body = RequestBody.create(JSON, bodyString);
  79.         Request request = new Request.Builder()
  80.                 .url(FCM_MESSAGE_URL)
  81.                 .post(body)
  82.                 .addHeader("Authorization", "key=" + SERVER_KEY)
  83.                 .build();
  84.         Response response = mClient.newCall(request).execute();
  85.         return response.body().string();
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement