-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmessaging.py
126 lines (106 loc) · 3.68 KB
/
messaging.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
"""Server Side FCM sample.
Firebase Cloud Messaging (FCM) can be used to send messages to clients on iOS,
Android and Web.
This sample uses FCM to send two types of messages to clients that are subscribed
to the `news` topic. One type of message is a simple notification message (display message).
The other is a notification message (display notification) with platform specific
customizations. For example, a badge is added to messages that are sent to iOS devices.
"""
import argparse
import json
import requests
import google.auth.transport.requests
from google.oauth2 import service_account
PROJECT_ID = '<YOUR-PROJECT-ID>'
BASE_URL = 'https://fcm.googleapis.com'
FCM_ENDPOINT = 'v1/projects/' + PROJECT_ID + '/messages:send'
FCM_URL = BASE_URL + '/' + FCM_ENDPOINT
SCOPES = ['https://www.googleapis.com/auth/firebase.messaging']
# [START retrieve_access_token]
def _get_access_token():
"""Retrieve a valid access token that can be used to authorize requests.
:return: Access token.
"""
credentials = service_account.Credentials.from_service_account_file(
'service-account.json', scopes=SCOPES)
request = google.auth.transport.requests.Request()
credentials.refresh(request)
return credentials.token
# [END retrieve_access_token]
def _send_fcm_message(fcm_message):
"""Send HTTP request to FCM with given message.
Args:
fcm_message: JSON object that will make up the body of the request.
"""
# [START use_access_token]
headers = {
'Authorization': 'Bearer ' + _get_access_token(),
'Content-Type': 'application/json; UTF-8',
}
# [END use_access_token]
resp = requests.post(FCM_URL, data=json.dumps(fcm_message), headers=headers)
if resp.status_code == 200:
print('Message sent to Firebase for delivery, response:')
print(resp.text)
else:
print('Unable to send message to Firebase')
print(resp.text)
def _build_common_message():
"""Construct common notifiation message.
Construct a JSON object that will be used to define the
common parts of a notification message that will be sent
to any app instance subscribed to the news topic.
"""
return {
'message': {
'topic': 'news',
'notification': {
'title': 'FCM Notification',
'body': 'Notification from FCM'
}
}
}
def _build_override_message():
"""Construct common notification message with overrides.
Constructs a JSON object that will be used to customize
the messages that are sent to iOS and Android devices.
"""
fcm_message = _build_common_message()
apns_override = {
'payload': {
'aps': {
'badge': 1
}
},
'headers': {
'apns-priority': '10'
}
}
android_override = {
'notification': {
'click_action': 'android.intent.action.MAIN'
}
}
fcm_message['message']['android'] = android_override
fcm_message['message']['apns'] = apns_override
return fcm_message
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--message')
args = parser.parse_args()
if args.message and args.message == 'common-message':
common_message = _build_common_message()
print('FCM request body for message using common notification object:')
print(json.dumps(common_message, indent=2))
_send_fcm_message(common_message)
elif args.message and args.message == 'override-message':
override_message = _build_override_message()
print('FCM request body for override message:')
print(json.dumps(override_message, indent=2))
_send_fcm_message(override_message)
else:
print('''Invalid command. Please use one of the following commands:
python messaging.py --message=common-message
python messaging.py --message=override-message''')
if __name__ == '__main__':
main()