FCM ¼¹ö(Spring) ¿¬µ¿
1. ¾Û ±¸µ¿½Ã FCM SDK¿¡¼ ÅäÅ« ¹ßÇà ¹× ¼¹ö¿¡ ÀúÀå(android)
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = MyFirebaseInstanceIDService.class.getSimpleName();
// ÅäÅ« Àç»ý¼º
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String token = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "token = " + token);
sendTokenToServer(token);
}
private void sendTokenToServer(String token){
OkHttpClient client = new OkHttpClient();
RequestBody body = new FormBody.Builder()
.add("token", token)
.build();
Request request = new Request.Builder()
.url("¼¹öURL") /* ¼¹ö¿¡ ¹ßÇàµÈ ÅäÅ«À» ÀúÀå */
.post(body)
.build();
try {
client.newCall(request).execute();
} catch (IOException e) {
e.printStackTrace();
}
}
}
2. push.jsp / Ǫ½Ã¸Þ½ÃÁö Àü¼ÛÇÒ È¸é(spring / jsp)
<form action="/sendPush" method="post">
<input type="text" id="title" name="title"/>
<input type="text" id="content" name="content"/>
<input type="submit" value="push"/>
<input type="reset" value="cancel"/>
</form>
3. Ǫ½Ã¸Þ½ÃÁö Àü¼ÛÇϱâ(spring / controller)
@RequestMapping(value ="/sendPush", method = {RequestMethod.GET,RequestMethod.POST})
public String sendPush(HttpServletRequest request, Model model) {
System.out.println("sendPush.......");
String token = getPushToken(); // ¼¹ö¿¡ ÀúÀåµÈ ÅäÅ« °¡Á®¿À±â
String title = request.getParameter("title");
if(StringUtils.isEmpty(title)) title = "Push Default title";
String content = request.getParameter("content");
if(StringUtils.isEmpty(content)) title = "Push Default content";
try {
title = URLEncoder.encode(title ,"UTF-8"); // Çѱ۱úÁüÀ¸·Î URLÀÎÄÚµùÇؼ º¸³¿
content = URLEncoder.encode(content,"UTF-8");
System.out.println(String.format("title : %s, content : %s", title, content));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ResponseEntity<String> result = sendHttp(token, title, content);
model.addAttribute("serverTime", token);
model.addAttribute("result", result.getBody());
return "push";
}
private ResponseEntity<String> sendHttp(String token, String title, String content){
RestTemplate template = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.add("Authorization", "key="+API_KEY); // API_KEY : Firebase ³» ÇÁ·ÎÁ§Æ®ÀÇ ¼¹öÅ°
JSONObject json = new JSONObject();
json.put("to", token);
JSONObject data = new JSONObject();
data.put("title" , title);
data.put("content", content);
json.put("data" ,data);
HttpEntity entity = new HttpEntity(json.toJSONString(), headers);
return template.exchange("https://fcm.googleapis.com/fcm/send", HttpMethod.POST, entity, String.class);
}
4. MyFirebaseMessagingService.class(android)
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.i(TAG, "onMessageReceived..");
Map<String, String> data = remoteMessage.getData();
String title = data.get("title");
String content = data.get("content");
try {
if(!TextUtils.isEmpty(title)) title = URLDecoder.decode(title,"UTF-8"); // Çѱ۱úÁüÀ¸·Î ¼¹ö¿¡¼ URLEncodingÇؼ º¸³¿..
if(!TextUtils.isEmpty(content)) content = URLDecoder.decode(content,"UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Log.i(TAG, String.format("title : $s , content : %s",title,content));
sendNotification(title, content);
}
* https://fcm.googleapis.com/fcm/send·Î º¸³¾ ¶§ÀÇ json ÇüÅÂ
{
"to": "etBbtvcHFZ4:APA91bF7I4BvHftRRpZ8qzA3by9kJJBSV2nT4_5vLkvk00-oKyyAXtFKxu2ybyJeTiTqoy4rTYOZzO6kkK74zqLFd5XCgO...",
"data": {
"title" : "title³»¿ë",
"content" : "content³»¿ë"
}
}
* ƯÁ¤ ´Ü¸»¿¡¸¸ Ǫ½Ã º¸³¾ ¶§ : jsonÀÇ "to"¿¡ ´Ü¸» ÅäÅ« ¼³Á¤
* ¸ðµç ´Ü¸»¿¡ Ǫ½Ã º¸³¾ ¶§ : jsonÀÇ "to"¿¡ "/topics/all" ¼³Á¤
(´Ü, android¿¡¼ subscribeToTopic("all")·Î ¼³Á¤µÇ¾î ÀÖ¾î¾ß ÇÔ.)
=> MainActivity.class(android)¿¡¼
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FirebaseMessaging.getInstance().subscribeToTopic("all");
if(FirebaseInstanceId.getInstance().getToken() != null)
Log.d(TAG+"_token : ",FirebaseInstanceId.getInstance().getToken());
}
Ãâó: http://devmason.tistory.com/147 [With IT]