diff --git a/engine/apps/mobile_app/fcm_relay.py b/engine/apps/mobile_app/fcm_relay.py index 060d39f638..925d9fa601 100644 --- a/engine/apps/mobile_app/fcm_relay.py +++ b/engine/apps/mobile_app/fcm_relay.py @@ -2,7 +2,6 @@ from celery.utils.log import get_task_logger from django.conf import settings -from firebase_admin.exceptions import FirebaseError from firebase_admin.messaging import AndroidConfig, APNSConfig, APNSPayload, Aps, ApsAlert, CriticalSound, Message from rest_framework import status from rest_framework.permissions import IsAuthenticated @@ -12,6 +11,7 @@ from apps.auth_token.auth import ApiTokenAuthentication from apps.mobile_app.models import FCMDevice +from apps.mobile_app.utils import send_message_to_fcm_device from common.custom_celery_tasks import shared_dedicated_queue_retry_task task_logger = get_task_logger(__name__) @@ -54,14 +54,9 @@ def post(self, request): autoretry_for=(Exception,), retry_backoff=True, max_retries=1 if settings.DEBUG else 5 ) def fcm_relay_async(token, data, apns, android=None): - message = _get_message_from_request_data(token, data, apns, android) - - # https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream - response = FCMDevice(registration_id=token).send_message(message) - task_logger.debug(f"FCM response: {response}") - - if isinstance(response, FirebaseError): - raise response + send_message_to_fcm_device( + FCMDevice(registration_id=token), _get_message_from_request_data(token, data, apns, android) + ) def _get_message_from_request_data(token, data, apns, android): diff --git a/engine/apps/mobile_app/utils.py b/engine/apps/mobile_app/utils.py index b5bb3755ba..69092bdb9f 100644 --- a/engine/apps/mobile_app/utils.py +++ b/engine/apps/mobile_app/utils.py @@ -36,6 +36,25 @@ def _send_push_notification_to_fcm_relay(message: Message) -> requests.Response: return response +def send_message_to_fcm_device(device: "FCMDevice", message: Message) -> None: + """ + https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream + """ + response = device.send_message(message) + logger.debug(f"FCM response: {response}") + + if isinstance(response, FirebaseError): + logger.exception( + f"FCM error occured in mobile_app.utils.send_message_to_fcm_device\n" + f"FCMDevice info: {device}\n" + f"FirebaseError code: {response._code}\n" + f"FirebaseError cause: {response._cause}\n" + f"FirebaseError http_response: {response._http_response}\n" + ) + + raise response + + def send_push_notification( device_to_notify: "FCMDevice", message: Message, error_cb: typing.Optional[typing.Callable[..., None]] = None ) -> None: @@ -68,12 +87,7 @@ def _error_cb(): else: raise else: - # https://firebase.google.com/docs/cloud-messaging/http-server-ref#interpret-downstream - response = device_to_notify.send_message(message) - logger.debug(f"FCM response: {response}") - - if isinstance(response, FirebaseError): - raise response + send_message_to_fcm_device(device_to_notify, message) def construct_fcm_message(