@@ -72,8 +72,18 @@ proxy_dict = {
72
72
}
73
73
fcm = FCMNotification(service_account_file = " <service-account-json-path>" , project_id = " <project-id>" , proxy_dict = proxy_dict)
74
74
75
+ # OR using credentials from environment variable
76
+ # Often you would save service account json in evironment variable
77
+ # Assuming GCP_CREDENTIALS contains the data (TIP: use "export GCP_CREDENTIALS=$(filename.json)" to quickly load the json)
78
+
79
+ from google.oauth2 import service_account
80
+ gcp_json_credentials_dict = json.loads(os.getenv(' GCP_CREDENTIALS' , None ))
81
+ credentials = service_account.Credentials.from_service_account_info(gcp_json_credentials_dict, scopes = [' https://www.googleapis.com/auth/firebase.messaging' ])
82
+ fcm = FCMNotification(service_account_file = None , credentials = credentials, project_id = " <project-id>" )
83
+
75
84
# Your service account file can be gotten from: https://console.firebase.google.com/u/0/project/_/settings/serviceaccounts/adminsdk
76
85
86
+ # Now you are ready to send notification
77
87
fcm_token = " <fcm token>"
78
88
notification_title = " Uber update"
79
89
notification_body = " Hi John, your order is on the way!"
@@ -106,6 +116,15 @@ result = fcm.notify(fcm_token=fcm_token, notification_body=notification_body, da
106
116
# To a single device
107
117
result = fcm.notify(fcm_token = fcm_token, data_payload = data_payload)
108
118
119
+ # Only string key and values are accepted. booleans, nested dicts are not supported
120
+ # To send nested dict, use something like
121
+ data_payload = {
122
+ " foo" : " bar" ,
123
+ " data" : json.dumps(data).
124
+ }
125
+ # For more info on format see https://firebase.google.com/docs/reference/fcm/rest/v1/projects.messages#Message
126
+ # and https://firebase.google.com/docs/cloud-messaging/http-server-ref#downstream-http-messages-json
127
+
109
128
# Use notification messages when you want FCM to handle displaying a notification on your app's behalf.
110
129
# Use data messages when you just want to process the messages only in your app.
111
130
# PyFCM can send a message including both notification and data payloads.
0 commit comments