400 Error while MIgrating to HTTP v1 API

69 views
Skip to first unread message

Shivasaigoud dasari

unread,
Jun 20, 2024, 10:31:52 AMJun 20
to Firebase Google Group
This is my code 
package com.viprasi.util;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Arrays;
import java.util.List;

import com.google.auth.oauth2.GoogleCredentials;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import com.notnoop.apns.APNS;
import com.notnoop.apns.ApnsService;

@Service("teamLivePostPushSenderUtil")
public class TeamLivePostPushSenderUtil {

private static final Logger log = LogManager.getLogger(PushSenderUtil.class);

    private static final String SCOPES= "https://www.googleapis.com/auth/firebase.messaging";
public void sendFirebaseFlutterNotifications(String messageSubject, List<String> fcmToken, JSONObject clickAction){

if (log.isInfoEnabled()){
log.info(new StringBuilder("TeamLivePostPushSenderUtil: Sending push notification to usiing firebase whose token Id: ").append(fcmToken));
}

//final String apiKey = ApplicationProperties.getFirebaseServerToken();
        StringBuffer response = new StringBuffer();
        try {
       
            for(String deviceId: fcmToken) {
            //JSONArray devices = new JSONArray();
            //devices.put(deviceId);
            //devices.put(androidDeviceId);

                JSONObject payloadObj = new JSONObject();
                JSONObject messageObj = new JSONObject();
                    JSONObject notifyObj = new JSONObject();
                   //commented it as part of white lable mobile apps
                   notifyObj.put("title","SportsPlus");
                    notifyObj.put("body", messageSubject);
                    //notifyObj.put("sound", "default");

                    messageObj.put("token",deviceId);
                    messageObj.put("notification",notifyObj);
                    messageObj.put("data", clickAction);
                   // messageObj.put("priority", "high");
                   // messageObj.put("content_available", true);
                    payloadObj.put("message", messageObj);
           
                URL url = new URL("https://fcm.googleapis.com/v1/projects/sportsplus-dev/messages:send");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setRequestMethod("POST");
                conn.setRequestProperty("Authorization", "Bearer "+getAccessToken());
                conn.setRequestProperty("Content-Type", "application/json");
               
                conn.setDoOutput(true);

                OutputStream os = conn.getOutputStream();
                os.write(payloadObj.toString().getBytes());
                os.flush();
                os.close();

                int responseCode = conn.getResponseCode();
                System.out.println("\nSending 'POST' request to URL : " + url);
                System.out.println("Post parameters : " + payloadObj.toString());
                System.out.println("Response Code : " + responseCode);

                BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String inputLine;


                while ((inputLine = in.readLine()) != null) {
                    response.append(inputLine);
                }
                in.close();
               
                // print result
                log.info("TeamLivePostPushSenderUtil: the response message:"+response.toString());
            }
       
           
        } catch(Exception e) {
            e.printStackTrace();
            log.error("TeamLivePostPushSenderUtil: ** exception ** ossueered while sending push notification using firebase.", e);
        }

}
    private String getAccessToken() throws IOException {
    InputStream resourceInputStream = this.getClass().getClassLoader().getResourceAsStream("firebase/SportsPlus-Dev.json");
        GoogleCredentials googleCredentials = GoogleCredentials
                .fromStream(resourceInputStream)
                .createScoped(Arrays.asList(SCOPES));
        googleCredentials.refresh();
        System.out.println("This is the Token of new firebase : "+googleCredentials.getAccessToken().getTokenValue());
        return googleCredentials.getAccessToken().getTokenValue();
    }

}
Im unable to understand whats going wrong in this 
Reply all
Reply to author
Forward
0 new messages