88import java .net .http .HttpRequest ;
99import java .net .http .HttpResponse ;
1010
11+ import io .github .cdimascio .dotenv .Dotenv ;
1112import org .json .JSONObject ;
1213
1314public class NtfyClient {
14-
15+ private final String baseUrl ;
1516 private final String topic ;
1617 private final HttpClient client ;
1718
1819 public NtfyClient (String topic ) {
20+
21+ Dotenv dotenv = Dotenv .load ();
22+ String backendUrl = "https://ntfy.sh/defaulttopic" ;
23+ this .baseUrl = dotenv .get ("BACKEND_URL" );
24+ System .out .println ("Backend URL: " + backendUrl );
25+
26+ try {
27+ dotenv = Dotenv .load ();
28+ if (dotenv .get ("BACKEND_URL" ) != null ) {
29+ backendUrl = dotenv .get ("BACKEND_URL" );
30+ }
31+ } catch (Exception e ) {
32+ System .err .println ("⚠️ Could not load .env file, using default URL." );
33+ }
34+
1935 this .topic = topic ;
2036 this .client = HttpClient .newHttpClient ();
2137 }
2238
23- // ---- SEND ----
39+
2440 public void sendMessage (String username , String message ) {
2541 try {
2642 String json = String .format ("""
@@ -43,10 +59,10 @@ public void sendMessage(String username, String message) {
4359 }
4460 }
4561
46- // ---- RECEIVE ----
62+
4763 public void subscribe (MessageHandler handler ) {
4864 new Thread (() -> {
49- while (true ) { // reconnect loop for robustness
65+ while (true ) {
5066 try {
5167 HttpRequest request = HttpRequest .newBuilder ()
5268 .uri (URI .create ("https://ntfy.sh/" + topic + "/json" ))
0 commit comments