랭귀지/python
HTTP v1 API를 사용하여 푸시 알림을 전송하는 샘플 코드
유키공
2024. 9. 11. 20:58
pip install firebase-admin
import firebase_admin
from firebase_admin import credentials
from firebase_admin import messaging
# Firebase Admin SDK 초기화
cred = credentials.Certificate('./path/to/your/serviceAccountKey.json')
firebase_admin.initialize_app(cred)
def send_fcm_messages(device_tokens, title, body, image_url, company_name):
# 푸시 알림 메시지 생성
message = messaging.MulticastMessage(
notification=messaging.Notification(
title=title,
body=body
),
data={
'image': image_url, # 이미지 URL
'click_action': 'FLUTTER_NOTIFICATION_CLICK', # 클릭 시 동작 정의
'companyName': company_name # 회사명 추가
},
tokens=device_tokens, # 여러 개의 토큰을 리스트로 전달
)
# 메시지 전송
try:
response = messaging.send_multicast(message)
print('Successfully sent messages:', response)
except Exception as e:
print('Error sending messages:', e)
# 예제 사용
if __name__ == "__main__":
device_tokens = ['TOKEN_1', 'TOKEN_2', 'TOKEN_3'] # 실제 장치 토큰 리스트로 대체
image_url = 'https://example.com/path/to/your/image.jpg' # 실제 이미지 URL로 대체
company_name = '회사명' # 실제 회사명으로 대체
send_fcm_messages(device_tokens, "안녕하세요", "FCM HTTP v1 푸시 알림 테스트입니다.", image_url, company_name)