Skip to content

Commit

Permalink
Merge pull request #358 from J-Priebe/handle-403
Browse files Browse the repository at this point in the history
Explicitly handle SENDER_ID_MISMATCH 403 error response
  • Loading branch information
olucurious authored Aug 30, 2024
2 parents 051afa9 + c6e9821 commit d18ea23
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,7 @@ Unreleased
- Replace deprecated urllib3.Retry options v2

.. _Łukasz Rogowski: https://github.com/Rogalek

- Explicitly handle 403 SENDER_ID_MISMATCH response

.. _James Priebe: https://github.com/J-Priebe/
9 changes: 8 additions & 1 deletion pyfcm/baseapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
AuthenticationError,
InvalidDataError,
FCMError,
FCMSenderIdMismatchError,
FCMServerError,
FCMNotRegisteredError,
)
Expand Down Expand Up @@ -185,12 +186,14 @@ def parse_response(self, response):
Parses the json response sent back by the server and tries to get out the important return variables
Returns:
dict: name (str) - uThe identifier of the message sent, in the format of projects/*/messages/{message_id}
dict: name (str) - The identifier of the message sent, in the format of projects/*/messages/{message_id}
Raises:
FCMServerError: FCM is temporary not available
AuthenticationError: error authenticating the sender account
InvalidDataError: data passed to FCM was incorrecly structured
FCMSenderIdMismatchError: the authenticated sender is different from the sender registered to the token
FCMNotRegisteredError: device token is missing, not registered, or invalid
"""

if response.status_code == 200:
Expand All @@ -210,6 +213,10 @@ def parse_response(self, response):
)
elif response.status_code == 400:
raise InvalidDataError(response.text)
elif response.status_code == 403:
raise FCMSenderIdMismatchError(
"The authenticated sender ID is different from the sender ID for the registration token."
)
elif response.status_code == 404:
raise FCMNotRegisteredError("Token not registered")
else:
Expand Down
9 changes: 9 additions & 0 deletions pyfcm/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@ class FCMNotRegisteredError(FCMError):
pass


class FCMSenderIdMismatchError(FCMError):
"""
Sender is not allowed for the given device tokens
https://firebase.google.com/docs/reference/fcm/rest/v1/ErrorCode
"""

pass


class FCMServerError(FCMError):
"""
Internal server error or timeout error on Firebase cloud messaging server
Expand Down
11 changes: 6 additions & 5 deletions pyfcm/fcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ def notify(
timeout (int, optional): Set time limit for the request
Returns:
dict: Response from FCM server (`multicast_id`, `success`, `failure`, `canonical_ids`, `results`)
dict: name (str) - The identifier of the message sent, in the format of projects/*/messages/{message_id}
Raises:
AuthenticationError: If api_key is not set or provided or there is an error authenticating the sender.
FCMServerError: Internal server error or timeout error on Firebase cloud messaging server
InvalidDataError: Invalid data provided
InternalPackageError: Mostly from changes in the response of FCM, contact the project owner to resolve the issue
FCMServerError: FCM is temporary not available
AuthenticationError: error authenticating the sender account
InvalidDataError: data passed to FCM was incorrecly structured
FCMSenderIdMismatchError: the authenticated sender is different from the sender registered to the token
FCMNotRegisteredError: device token is missing, not registered, or invalid
"""
payload = self.parse_payload(
fcm_token=fcm_token,
Expand Down

0 comments on commit d18ea23

Please sign in to comment.