Skip to content

Commit

Permalink
Improve FCMDevice.send_message logging (#3527)
Browse files Browse the repository at this point in the history
# What this PR does

Add more logging around `FCMDevice.send_message` in an effort to fix
grafana/oncall-private#1820
  • Loading branch information
joeyorlando authored Dec 6, 2023
1 parent e548172 commit 6953c3f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
13 changes: 4 additions & 9 deletions engine/apps/mobile_app/fcm_relay.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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__)
Expand Down Expand Up @@ -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):
Expand Down
26 changes: 20 additions & 6 deletions engine/apps/mobile_app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 6953c3f

Please sign in to comment.