Advertisement
stevekamau

Untitled

Apr 19th, 2017
657
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.16 KB | None | 0 0
  1. public class SendPushNotifications {
  2.  
  3.     public void triggerFirebaseNotificationRequest(String time_code, String business_id,
  4.                                                    String name, String quantity, String attendant_name,
  5.                                                    String attendant_id) {
  6.         JSONObject json = new JSONObject();
  7.         JSONObject manJson = new JSONObject();
  8.         try {
  9.             manJson.put("message", "New Sales");
  10.             manJson.put("type", "sale");
  11.             manJson.put("time_code", time_code);
  12.             manJson.put("business_id", business_id);
  13.             manJson.put("name", name);
  14.             manJson.put("quantity", quantity);
  15.             manJson.put("attendant_name", attendant_name);
  16.             manJson.put("attendant_id", attendant_id);
  17.             json.put("to", "/topics/sales-updates");
  18.             json.put("data", manJson);
  19.  
  20.  
  21.         } catch (JSONException e) {
  22.             e.printStackTrace();
  23.         }
  24.         String json_str = json.toString();
  25.         Log.d("jsorn_to_firebase", json_str);
  26.         try {
  27.             HttpClient client = new DefaultHttpClient();
  28.             HttpPost httpPost = new HttpPost("https://fcm.googleapis.com/fcm/send");
  29.             httpPost.setEntity(new StringEntity(json_str));
  30. //            httpPost.setHeader("Accept", "application/json");
  31.             httpPost.setHeader("Authorization", "key=AIzaSyAIIT2f8nMXh6HDHNmX3rI7EOcYaBzsV3U");
  32.             httpPost.setHeader("Content-type", "application/json");
  33.             Log.d("firebase_notif_request", "true");
  34.             HttpResponse response = client.execute(httpPost);
  35.             String responseStr = EntityUtils.toString(response.getEntity());
  36.  
  37.             Log.d("request_made7", "reached");
  38.             Log.d("firebase_notif_resp", responseStr);
  39.  
  40.             Log.d("firebase_notif_resp", "true");
  41.  
  42.         } catch (UnsupportedEncodingException e) {
  43.             e.printStackTrace();
  44.         } catch (ClientProtocolException e) {
  45.  
  46.             //sync failed so we rest the syncing icon
  47.             e.printStackTrace();
  48.         } catch (IOException e) {
  49.             e.printStackTrace();
  50.         }
  51.     }
  52.  
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement