-
Notifications
You must be signed in to change notification settings - Fork 23
/
firebase_push_notification_sender.py
66 lines (56 loc) · 2.1 KB
/
firebase_push_notification_sender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json
import requests
import google.auth.transport.requests
from google.oauth2 import service_account
PROJECT_ID = '' #find it in firebase -> project settings -> General
SERVICE_ACCOUNT_FILE = 'service_account.json' #Download service account service file from project settings -> Service Accounts -> Generate new private key
FCM_API_URL = f"https://fcm.googleapis.com/v1/projects/{PROJECT_ID}/messages:send" # DON'T CHANGE THIS
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging'] # DON'T CHANGE THIS
def test_send_data_and_notification_message():
token = ""
data = {'key1': 'value2', 'key2': 'value2'}
title = "Test Notification Title from FCM"
body = "Test Notification Body message from FCM"
response = send_data_and_notification_message(token, title, body, data)
if response.status_code == 200:
print("Message sent successfully")
else:
print("Error sending message:", response.text)
def get_access_token():
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
return credentials.token
def send_data_and_notification_message(token, title, body, data):
access_token = get_access_token()
headers = {
'Authorization': f'Bearer {access_token}',
'Content-Type': 'application/json'
}
message = {
"message": {
"token": token,
'notification': {
'title': title,
'body': body
},
"data": data,
"apns": {
"headers": {
"apns-priority": "10"
},
"payload": {
"aps": {
"badge": 0,
# "content-available": 1
},
}
}
}
}
response = requests.post(FCM_API_URL, headers=headers, data=json.dumps(message))
return response
def main():
test_send_data_and_notification_message()
if __name__ == "__main__":
main()