안녕하세요.
앱을 설치하고 초기에 받은 토큰을 어플서버 DB에 저장합니다.
그런 다음 jsp페이지에서 공지사항을 입력하고 서블릿에서 Firebase 서버로 푸쉬메세지를 요청하는 걸
하고 있습니다.
일단 특정 토큰(사용자) 하나는 보내지게 했습니다.
아래에 첨부한 소스는 푸쉬를 요청하는 부분인데요.
보시고 조언 좀 부탁드립니다.
public void sendServerToFirebase(String token, String title, String content) {
try {
URL url = new URL(FCM_URL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setUseCaches( false );
conn.setDoInput( true );
conn.setDoOutput( true );
conn.setRequestMethod( "POST" );
conn.setRequestProperty( "Authorization" ,"key="+ API_KEY );
conn.setRequestProperty( "Content-Type" ,"application/json" );
JSONObject info = new JSONObject();
info.put( "title" , title ); // Notification title
info.put( "body" , content ); // Notification body
info.put("icon", "ic_message");
JSONObject json = new JSONObject();
json.put( "to" , token.trim());
json.put( "data" , info );// json.put( "notification" , info );
json.put("click_action", "OPEN_ACTIVITY");
System. out .println("보낼 JSON 데이터 :"+json.toString());
OutputStreamWriter wr = new OutputStreamWriter(conn .getOutputStream(), "utf-8");
wr.write( json.toString());
wr.flush();
// wr.close();
//응답오류 처리
if (conn.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new RuntimeException("Failed : HTTP error code : " + conn.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String output="";
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println("메세지를 보내고 받은 데이터 : "+output);
}
conn.disconnect();
} catch (IOException e ) {
e.printStackTrace();
}
}
[질문]
1. 해당 앱을 받은 모든 유저들에게 보낼려면 소스를 어떻게 바꿔야 할까요?
=> 위소스에서 json.put( "to" , token.trim()); 이 부분은 특정 사용자 한명의 토큰인데 이걸 전체나 일부 여러명에게 보내려면 어떻게 해야하는지 궁금합니다.
2. 특정 유저 여러명에게 보낼때는 각 유저마다 토큰을 가져와서 유저 숫자만큼 푸쉬요청 메소드를 호출하는 방식으로 해야하나요? 내용이 같은데 여러번 반복하는게 좋은 방법인지요?
3. 처음 어플을 깔때 해당 어플 토픽을 ALL로 정해줘야 전체 공지사항을 보낼 수 있는건가요?
4. 앱설정에서 유저 한사람이 여러가지 토픽(공지사항, 뉴스, 취미 등)을 선택할 수 있나요?
5. 보통 게시판 앱 같은 경우 내 글에 댓글이 달리면 알림을 보내주잔아요? 이건 댓글을 등록하는 과정에서
해당 글 유저정보를 가지고 토큰을 얻어와서 해당유저에거 보내주는 방식으로 구현하면 되는건가요?
2018년 새해도 벌써 3일이 지나가고 있네요.
펍님들 모두 새해 복많이 받으시고 건강한 한해 되세요~^^^