package com.github.carlinhafuji.iotserver.infra;

import com.github.carlinhafuji.iotserver.domain.notification.Notification;
import com.github.carlinhafuji.iotserver.domain.notification.NotificationSender;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Scanner;

@Service
public class FirebaseCloudMessageNotificationSender implements NotificationSender {

    public String url() {
        return System.getenv("FCM_URL");
    }

    public String apiKey() {
        return System.getenv("FCM_API_KEY");
    }

    @Override
    public void send(Notification notification) {
        try {
            URL url = new URL(url());
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setRequestProperty("Authorization", "key=" + apiKey());
            conn.setRequestProperty("Content-Type", "application/json");
            conn.setDoOutput(true);

            OutputStream outputStream = conn.getOutputStream();
            outputStream.write(getPayload(notification).getBytes());

            /*TODO Implement ExponencialBackoff*/
            /*TODO Validate if message was sent successfully*/
            InputStream inputStream = conn.getInputStream();
            final String resp = convertStreamToString(inputStream);
            System.out.println(resp);
            /* --- */
        } catch (JSONException | IOException e) {
            e.printStackTrace();
        }
    }
    private String convertStreamToString(InputStream is) {
        Scanner s = new Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next().replace(",", ",\n") : "";
    }

    private String getPayload(Notification notification) {
        JSONObject payload = new JSONObject();
        JSONObject jNotification = new JSONObject();
        JSONObject jData = new JSONObject();

        jNotification.put("title", notification.title());
        jNotification.put("body", notification.body());
        jNotification.put("sound", "default");
        jNotification.put("badge", "1");
        jNotification.put("click_action", "OPEN_ACTIVITY_1");
        payload.put("priority", "high");
        payload.put("notification", jNotification);

        //jData.put("picture_url", "http://opsbug.com/static/google-io.jpg");
        payload.put("data", jData);

        JSONArray tokens = new JSONArray();
        tokens.put(notification.recipient().token());
        payload.put("registration_ids", tokens);

        return payload.toString();
    }
}

@@ -1251,27 +1251,25 @@
     
-outputStream.write(
+String payload = 
 getP
@@ -1288,16 +1288,122 @@
 ication)
+;%0A            System.out.println(%22Sending message --%3E %22 + payload);%0A            outputStream.write(payload
 .getByte
@@ -2095,32 +2095,74 @@
 w JSONObject();%0A
+        payload.put(%22priority%22, %22high%22);%0A%0A
         JSONObje
@@ -2202,54 +2202,8 @@
 ();%0A
-        JSONObject jData = new JSONObject();%0A%0A
     
@@ -2412,267 +2412,49 @@
     
-jNotification.put(%22click_action%22, %22OPEN_ACTIVITY_1%22);%0A        payload.put(%22priority%22, %22high%22);%0A        payload.put(%22notification%22, jNotification);%0A%0A        //jData.put(%22picture_url%22, %22http://opsbug.com/static/google-io.jpg%22);%0A        payload.put(%22data%22, jData
+payload.put(%22notification%22, jNotification
 );%0A%0A
@@ -2597,24 +2597,106 @@
 , tokens);%0A%0A
+        JSONObject jData = new JSONObject();%0A        payload.put(%22data%22, jData);%0A%0A
         retu

Add log