Skip to content

Commit

Permalink
Merge pull request #145 from Hanaro-trip-together-bank/feature/dues_paid
Browse files Browse the repository at this point in the history
refactor:fcm fix
  • Loading branch information
ny2060 authored Jun 7, 2024
2 parents 6568bd9 + 1367860 commit d9144c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,36 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.FirebaseOptions;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.util.ResourceUtils;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;

@Configuration
public class FirebaseConfig {
@Bean
public FirebaseApp firebaseApp() throws IOException {

InputStream resource = new ClassPathResource("./triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream();
String jsonString = new String(resource.readAllBytes(), StandardCharsets.UTF_8);
InputStream resource = new ClassPathResource("triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream();

// \n을 실제 줄 바꿈으로 변환
String formattedJson = jsonString.replace("\\n", "\n");
JsonReader jsonReader = new JsonReader(new InputStreamReader(resource));
jsonReader.setLenient(true);

InputStream formattedJsonStream = new ByteArrayInputStream(formattedJson.getBytes(StandardCharsets.UTF_8));
// JsonParser를 사용하여 JSON 데이터를 파싱
var jsonElement = JsonParser.parseReader(jsonReader);
String jsonString = jsonElement.toString();

// 파싱된 JSON 데이터를 다시 InputStream으로 변환
InputStream jsonInputStream = new ByteArrayInputStream(jsonString.getBytes(StandardCharsets.UTF_8));

FirebaseOptions options = FirebaseOptions
.builder()
.setCredentials(GoogleCredentials.fromStream(formattedJsonStream))
.setCredentials(GoogleCredentials.fromStream(jsonInputStream))
.build();
return FirebaseApp.initializeApp(options);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hanaro.triptogether.common.firebase;

import org.junit.jupiter.api.Test;
import org.springframework.core.io.ClassPathResource;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;

import static org.junit.jupiter.api.Assertions.*;

class FirebaseConfigTest {

@Test
public void test() throws IOException {

InputStream resource = new ClassPathResource("./triptogether-e7bac-firebase-adminsdk-peiki-127517aa66.json").getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(resource, StandardCharsets.UTF_8));

String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}



}

0 comments on commit d9144c5

Please sign in to comment.