app - 백엔드 서버(Flask) - FCM 서버(FCM HTTP Legacy)
FCM서버와 HTTP 통신하는 방법은 두가지이다.
- FCM HTTP Legacy
- FCM HTTP v1
FCM HTTP v1가 보안, 유연성에 장점이 있다.(이 방법으로 시도하려면 참고)
기존 Legacy가 FCM의 서버키를 사용하는 대신에, 프로그래밍 방법으로 생성되는 access token을 통해 보안을 개선하고 조건에 따라 푸시 알람을 유연하게 보낼 수 있는 기능이 추가된 것으로 이해했다.
하지만 나는 이번 프로젝트에서 구현해야할 알림 기능이 단순하고, 빠른 구현이 중점이기 때문에 Legacy 버전으로 구현한다.
구현 단계
1. FCM 서버에 메세지 전송 -> 앱에서 수신
테스트 방법 : Postman 사용
2. 백엔드 서버에서 ID값에 해당되는 fcm token 값과 message title, body를 FCM 서버로 전송(FCM 서버 프로토콜)
3. 이벤트 발생시 2번 실행
알림 메세지의 json 포맷은 다음과 같음
참고로 전송 옵션은 여러가지가 있으며, 공식문서에 설명이 잘 나와있음
{
"to":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
HTTP 요청 endpoint :
POST https://fcm.googleapis.com/fcm/send
Header :
Authorization: key=<서버키>
서버키 : firebase console > 설정 > 클라우드 메세징 탭
사용 예 :
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{"to": "<FCM_TOKEN>", "notification": {"title": "message title", "body": "message body" }}
Python code
@app.route("/api/push_notification", methods=["POST"])
@need_apikey()
def push_notification():
farm_id = request.json.get("farm_id", None)
title = request.json.get("title", None)
body = request.json.get("body", None)
fcm_api_url = "https://fcm.googleapis.com/fcm/send"
fcm_serverkey = "서버키"
user_data = FARM_USER.get_or_none(FARM_USER.FARM_ID == int(farm_id))
if user_data is None:
return jsonify({"msg": res.text}), 401 # No user matches with farm_id
fcm_token = user_data.FCM_TOKEN
headers = {"Content-Type": "application/json", "Authorization": f"key={fcm_serverkey}"}
push_message_dict = {
"to": fcm_token,
"notification": {"title": title, "body": body},
}
try:
res = requests.post(fcm_api_url, data=json.dumps(push_message_dict), headers=headers)
return jsonify({"msg": res.text}), 200
except:
return jsonify({"msg": res.text}), 400 # Problem with json format
References
https://soulduse.tistory.com/94(이분 블로그가 설명이 잘되어있으니 참고!)
FCM Push Notification with Postman Part1
안드로이드에서 푸시 알람을 처리하기 위한 방식으로 보통 FCM을 사용하게 됩니다. 앱에서 Push 개발을 하는건 크게 문제 없지만 내가 만든 코드가 잘 동작하는지 확인을 해볼 필요가 있는데 이번
soulduse.tistory.com
https://soulduse.tistory.com/95
FCM Push Notification(HTTP v1) with OAuth 2.0 Playground / Postman / Terminal - Part2
이전 글에서는 Firebase Console + FCM Legacy API로 푸시를 발송해보는 내용이었다면, 이번 Part2에서는 구글에서 권장하고 있는 FCM HTTP v1 API을 사용(현재 기준 가장 최신)하여 푸시를 발송해보는 내용을
soulduse.tistory.com
https://swiftymind.tistory.com/136
FCM(Firebase Cloud Messaging) (3) - 전송
해당 글에서는 FCM 서버로 메시지를 전송하는 protocol에 대해서만 살펴볼 것이며, SDK를 이용한 클라이언트 작업은 https://firebase.google.com/docs/cloud-messaging 에서 자세히 확인할 수 있다. FCM Provider..
swiftymind.tistory.com
https://firebase.google.com/docs/cloud-messaging/migrate-v1
기존 HTTP에서 HTTP v1로 마이그레이션 | Firebase Documentation
Join Firebase at Google I/O 2022 live from Shoreline Amphitheatre and online May 11-12. Register now 의견 보내기 기존 HTTP에서 HTTP v1로 마이그레이션 FCM의 기존 HTTP API를 사용하는 앱은 이 가이드의 안내에 따라 HTTP v1 AP
firebase.google.com
https://eunjin3786.tistory.com/281
[Django] FCM 서버를 통해 앱에 푸쉬보내기
[Flutter] Firebase Cloud Messaging 연동 + 파베 콘솔에서 푸쉬보내기 에서 푸쉬를 받는 클라이언트(iOS, 안드로이드) 쪽 설정을 해줬는데요 이제 푸쉬를 보내는 서버쪽 작업을 해주겠습니다. 문서
eunjin3786.tistory.com
GitHub - firebase/quickstart-python
Contribute to firebase/quickstart-python development by creating an account on GitHub.
github.com
'Flutter' 카테고리의 다른 글
[Flutter / GetX] GetXcontroller & StateMixin : onLoading, onError 처리 (0) | 2022.04.02 |
---|---|
[Flutter / 일반] 앱 아이콘 변경하기 (0) | 2022.03.26 |
[Flutter / GetX / 일반] firebase cloud messaging으로 push notification 구현(1) : android, iOS 사전 세팅 (0) | 2022.03.21 |
[Flutter / 일반] 앱이 사용중인 데이터 유형 알려주기(mobile network) (0) | 2022.03.21 |
[Flutter / 일반 / GetX] 사용자가 앱을 사용하지 않을때(백그라운드) 앱 상태 관리 (0) | 2022.03.21 |