-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Trigger function on version and deleted docuemtn insertion - Send FCM notification if right document - FirebaseNotification class wrapper around FCM requests
- Loading branch information
1 parent
c1c4848
commit 82d93dc
Showing
5 changed files
with
143 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import json | ||
|
||
import google.auth.transport.requests | ||
import requests | ||
from google.oauth2 import service_account | ||
|
||
|
||
class FirebaseNotification: | ||
def __init__(self, cert, project_id): | ||
self.cert = cert | ||
self.url = f"https://fcm.googleapis.com/v1/projects/{project_id}/messages:send" | ||
self.token = self._get_access_token() | ||
|
||
def _get_access_token(self) -> str: | ||
"""Retrieve a valid access token to authorize requests. | ||
:return: Access token. | ||
""" | ||
credentials = service_account.Credentials.from_service_account_file( | ||
self.cert, | ||
scopes=[ | ||
"https://www.googleapis.com/auth/cloud-platform", | ||
"https://www.googleapis.com/auth/firebase", | ||
], | ||
) | ||
request = google.auth.transport.requests.Request() | ||
credentials.refresh(request) | ||
return credentials.token | ||
|
||
@property | ||
def headers(self): | ||
"""Get headers for authorized requests.""" | ||
return { | ||
"Authorization": "Bearer " + self.token, | ||
"Content-Type": "application/json; UTF-8", | ||
} | ||
|
||
def send_to_topic(self, topic: str, data: dict = None) -> requests.Response: | ||
"""Send a message to a topic.""" | ||
return requests.post( | ||
self.url, | ||
headers=self.headers, | ||
data=json.dumps( | ||
{ | ||
"message": { | ||
"topic": topic, | ||
"data": data, | ||
} | ||
} | ||
), | ||
) | ||
|
||
def send_to_token(self, token: str, data: dict = None) -> requests.Response: | ||
"""Send a message to a token.""" | ||
return requests.post( | ||
self.url, | ||
headers=self.headers, | ||
data=json.dumps( | ||
{ | ||
"message": { | ||
"token": token, | ||
"data": data, | ||
} | ||
} | ||
), | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters