Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix message is too big exception for mobile push notification #3556

Merged
merged 9 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Add error code for mobile push notification logs when device is not set up @Ferril ([#3554](https://github.com/grafana/oncall/pull/3554))

### Fixed

- Fix issue when mobile push notification message is too big @Ferril ([#3556](https://github.com/grafana/oncall/pull/3556)

## v1.3.77 (2023-12-11)

### Fixed
Expand Down
8 changes: 8 additions & 0 deletions engine/apps/mobile_app/alert_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,17 @@ def _render_for(self):


def get_push_notification_subtitle(alert_group):
MAX_ALERT_TITLE_LENGTH = 200
print(f"ALERTS COUNT: {alert_group.alerts.count}") # TODO: debugging test. remove after debugging
alert = alert_group.alerts.first()
print(f"ALERT TITLE: {alert.title}, DATA: {alert.raw_request_data}") # TODO: debugging test. remove after debugging
templated_alert = AlertMobileAppTemplater(alert).render()
print(f"TEMPLATED ALERT: {templated_alert}") # TODO: debugging test. remove after debugging
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove print statements

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 I'm debugging failing tests, that work locally

alert_title = str_or_backup(templated_alert.title, "Alert Group")
# limit alert title length to prevent FCM `message is too big` exception
# https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages
if len(alert_title) > MAX_ALERT_TITLE_LENGTH:
alert_title = f"{alert_title[:MAX_ALERT_TITLE_LENGTH]}..."

status_verbose = "Firing" # TODO: we should probably de-duplicate this text
if alert_group.resolved:
Expand Down
32 changes: 32 additions & 0 deletions engine/apps/mobile_app/tests/tasks/test_new_alert_group.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from apps.base.models import UserNotificationPolicy, UserNotificationPolicyLogRecord
from apps.mobile_app.alert_rendering import get_push_notification_subtitle
from apps.mobile_app.models import FCMDevice, MobileAppUserSettings
from apps.mobile_app.tasks.new_alert_group import _get_fcm_message, notify_user_about_new_alert_group

Expand Down Expand Up @@ -175,3 +176,34 @@ def test_fcm_message_user_settings_critical_override_dnd_disabled(
apns_sound = message.apns.payload.aps.sound
assert apns_sound.critical is False
assert message.apns.payload.aps.custom_data["interruption-level"] == "time-sensitive"


@pytest.mark.django_db
@pytest.mark.parametrize(
"alert_title",
[
"Some short title",
"Some long title" * 100,
],
)
def test_get_push_notification_subtitle(
alert_title,
make_organization_and_user,
make_alert_receive_channel,
make_alert_group,
make_alert,
):
MAX_ALERT_TITLE_LENGTH = 200
organization, user = make_organization_and_user()
alert_receive_channel = make_alert_receive_channel(organization=organization)
alert_group = make_alert_group(alert_receive_channel)
make_alert(alert_group=alert_group, title=alert_title, raw_request_data={"title": alert_title})
expected_alert_title = (
f"{alert_title[:MAX_ALERT_TITLE_LENGTH]}..." if len(alert_title) > MAX_ALERT_TITLE_LENGTH else alert_title
)
expected_result = (
f"#1 {expected_alert_title}\n" + f"via {alert_group.channel.short_name}" + "\nStatus: Firing, alerts: 1"
)
result = get_push_notification_subtitle(alert_group)
assert len(expected_alert_title) <= MAX_ALERT_TITLE_LENGTH + 3
assert result == expected_result
Loading