# What this PR does
Adds mobile app settings support to OnCall backend.
- Adds a new Django model `MobileAppUserSettings` to store push
notification settings
- Adds a new endpoint `/mobile_app/v1/user_settings` to fetch/update
settings from the mobile app
Some additional info on implementation: at first I wanted to extend the
messaging backend system to allow storing / retrieving per-user data and
implement mobile app settings based on those changes. After some thought
I decided not to extend the messaging backend system and have this as
functionality specific to the `mobile_app` Django app. Currently the
messaging backend system is used by the backend and plugin UI, but
mobile app settings are specific only to the mobile app and not
configurable in the plugin UI.
**tldr: wanted to extend messaging backend system, but decided not to do
that**
# Usage
## Get settings via API
`GET /mobile_app/v1/user_settings`
Example response:
```json
{
"default_notification_sound_name": "default_sound", # sound name without file extension
"default_notification_volume_type": "constant",
"default_notification_volume": 0.8,
"default_notification_volume_override": false,
"important_notification_sound_name": "default_sound_important", # sound name without file extension
"important_notification_volume_type": "constant",
"important_notification_volume": 0.8,
"important_notification_override_dnd": true
}
```
## Update settings via API
`PUT /mobile_app/v1/user_settings` - see example response above for
payload shape.
Note that sound names must be passed without file extension. When
sending push notifications, the backend will add `.mp3` to sound names
and pass it to push notification data for Android. For iOS, sound names
will be suffixed with `.aiff` to be used by APNS.
## Get settings from notification data for Android
All the settings from example response will be available in push
notification data (along with `orgId`, `alertGroupId`, `title`, etc.).
Fields `default_notification_volume`,
`default_notification_volume_override` and
`important_notification_volume` , `important_notification_override_dnd`
will be converted to strings due to FCM limitations.
Fields `default_notification_sound_name` and
`important_notification_sound_name` will be suffixed with `.mp3` in push
notification data.
## iOS limitations
While Android push notifications are handled purely on the mobile app
side, iOS notifications are sent via APNS which imposes some
limitations.
- Notification volume cannot be overridden for non-critical
notifications (so fields `default_notification_volume_override` and
`default_notification_volume` will be disregarded for iOS notifications)
- It's not possible to control volume type (i.e. "constant" vs
"intensifying") via APNS. A possible workaround is to have different
sound files for "constant" and "intensifying" and pass that as
`default_notification_sound_name` / `important_notification_sound_name`.
# Which issue(s) this PR fixes
Related to https://github.com/grafana/oncall-private/issues/1602
# Checklist
- [x] Tests updated
- [x] No changelog updates since the changes are not user-facing
51 lines
1.9 KiB
Python
51 lines
1.9 KiB
Python
import pytest
|
|
from django.urls import reverse
|
|
from rest_framework import status
|
|
from rest_framework.test import APIClient
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_user_settings_get(make_organization_and_user_with_mobile_app_auth_token):
|
|
organization, user, auth_token = make_organization_and_user_with_mobile_app_auth_token()
|
|
|
|
client = APIClient()
|
|
url = reverse("mobile_app:user_settings")
|
|
|
|
response = client.get(url, HTTP_AUTHORIZATION=auth_token)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
# Check the default values are correct
|
|
assert response.json() == {
|
|
"default_notification_sound_name": "default_sound",
|
|
"default_notification_volume_type": "constant",
|
|
"default_notification_volume": 0.8,
|
|
"default_notification_volume_override": False,
|
|
"important_notification_sound_name": "default_sound_important",
|
|
"important_notification_volume_type": "constant",
|
|
"important_notification_volume": 0.8,
|
|
"important_notification_override_dnd": True,
|
|
}
|
|
|
|
|
|
@pytest.mark.django_db
|
|
def test_user_settings_put(make_organization_and_user_with_mobile_app_auth_token):
|
|
organization, user, auth_token = make_organization_and_user_with_mobile_app_auth_token()
|
|
|
|
client = APIClient()
|
|
url = reverse("mobile_app:user_settings")
|
|
data = {
|
|
"default_notification_sound_name": "test_default",
|
|
"default_notification_volume_type": "intensifying",
|
|
"default_notification_volume": 1,
|
|
"default_notification_volume_override": True,
|
|
"important_notification_sound_name": "test_important",
|
|
"important_notification_volume_type": "intensifying",
|
|
"important_notification_volume": 1,
|
|
"important_notification_override_dnd": False,
|
|
}
|
|
|
|
response = client.put(url, data=data, format="json", HTTP_AUTHORIZATION=auth_token)
|
|
assert response.status_code == status.HTTP_200_OK
|
|
|
|
# Check the values are updated correctly
|
|
assert response.json() == data
|