3
3
import logging
4
4
import math
5
5
import typing
6
- from enum import Enum
7
6
8
7
import humanize
9
8
import pytz
20
19
from apps .alerts .models import AlertGroup
21
20
from apps .base .utils import live_settings
22
21
from apps .mobile_app .alert_rendering import get_push_notification_subtitle
22
+ from apps .mobile_app .types import FCMMessageData , MessageType , Platform
23
23
from apps .schedules .models import ShiftSwapRequest
24
24
from apps .schedules .models .on_call_schedule import OnCallSchedule , ScheduleEvent
25
25
from apps .user_management .models import User
36
36
logger .setLevel (logging .DEBUG )
37
37
38
38
39
- class MessageType (str , Enum ):
40
- NORMAL = "oncall.message"
41
- CRITICAL = "oncall.critical_message"
42
- INFO = "oncall.info"
43
-
44
-
45
- class FCMMessageData (typing .TypedDict ):
46
- title : str
47
- subtitle : typing .Optional [str ]
48
- body : typing .Optional [str ]
49
-
50
-
51
39
def send_push_notification_to_fcm_relay (message : Message ) -> requests .Response :
52
40
"""
53
41
Send push notification to FCM relay on cloud instance: apps.mobile_app.fcm_relay.FCMRelayView
@@ -168,11 +156,8 @@ def _get_alert_group_escalation_fcm_message(
168
156
169
157
# APNS only allows to specify volume for critical notifications
170
158
apns_volume = mobile_app_user_settings .important_notification_volume if critical else None
171
- apns_sound_name = (
172
- mobile_app_user_settings .important_notification_sound_name
173
- if critical
174
- else mobile_app_user_settings .default_notification_sound_name
175
- ) + MobileAppUserSettings .IOS_SOUND_NAME_EXTENSION # iOS app expects the filename to have an extension
159
+ message_type = MessageType .IMPORTANT if critical else MessageType .DEFAULT
160
+ apns_sound_name = mobile_app_user_settings .get_notification_sound_name (message_type , Platform .IOS )
176
161
177
162
fcm_message_data : FCMMessageData = {
178
163
"title" : alert_title ,
@@ -183,18 +168,16 @@ def _get_alert_group_escalation_fcm_message(
183
168
# alert_group.status is an int so it must be casted...
184
169
"status" : str (alert_group .status ),
185
170
# Pass user settings, so the Android app can use them to play the correct sound and volume
186
- "default_notification_sound_name" : (
187
- mobile_app_user_settings .default_notification_sound_name
188
- + MobileAppUserSettings .ANDROID_SOUND_NAME_EXTENSION
171
+ "default_notification_sound_name" : mobile_app_user_settings .get_notification_sound_name (
172
+ MessageType .DEFAULT , Platform .ANDROID
189
173
),
190
174
"default_notification_volume_type" : mobile_app_user_settings .default_notification_volume_type ,
191
175
"default_notification_volume" : str (mobile_app_user_settings .default_notification_volume ),
192
176
"default_notification_volume_override" : json .dumps (
193
177
mobile_app_user_settings .default_notification_volume_override
194
178
),
195
- "important_notification_sound_name" : (
196
- mobile_app_user_settings .important_notification_sound_name
197
- + MobileAppUserSettings .ANDROID_SOUND_NAME_EXTENSION
179
+ "important_notification_sound_name" : mobile_app_user_settings .get_notification_sound_name (
180
+ MessageType .IMPORTANT , Platform .ANDROID
198
181
),
199
182
"important_notification_volume_type" : mobile_app_user_settings .important_notification_volume_type ,
200
183
"important_notification_volume" : str (mobile_app_user_settings .important_notification_volume ),
@@ -222,8 +205,6 @@ def _get_alert_group_escalation_fcm_message(
222
205
),
223
206
)
224
207
225
- message_type = MessageType .CRITICAL if critical else MessageType .NORMAL
226
-
227
208
return _construct_fcm_message (message_type , device_to_notify , thread_id , fcm_message_data , apns_payload )
228
209
229
210
@@ -268,8 +249,6 @@ def _get_youre_going_oncall_fcm_message(
268
249
thread_id = f"{ schedule .public_primary_key } :{ user .public_primary_key } :going-oncall"
269
250
270
251
mobile_app_user_settings , _ = MobileAppUserSettings .objects .get_or_create (user = user )
271
- info_notification_sound_name = mobile_app_user_settings .info_notification_sound_name
272
-
273
252
notification_title = _get_youre_going_oncall_notification_title (seconds_until_going_oncall )
274
253
notification_subtitle = _get_youre_going_oncall_notification_subtitle (
275
254
schedule , schedule_event , mobile_app_user_settings
@@ -278,7 +257,9 @@ def _get_youre_going_oncall_fcm_message(
278
257
data : FCMMessageData = {
279
258
"title" : notification_title ,
280
259
"subtitle" : notification_subtitle ,
281
- "info_notification_sound_name" : f"{ info_notification_sound_name } { MobileAppUserSettings .ANDROID_SOUND_NAME_EXTENSION } " ,
260
+ "info_notification_sound_name" : mobile_app_user_settings .get_notification_sound_name (
261
+ MessageType .INFO , Platform .ANDROID
262
+ ),
282
263
"info_notification_volume_type" : mobile_app_user_settings .info_notification_volume_type ,
283
264
"info_notification_volume" : str (mobile_app_user_settings .info_notification_volume ),
284
265
"info_notification_volume_override" : json .dumps (mobile_app_user_settings .info_notification_volume_override ),
@@ -290,7 +271,7 @@ def _get_youre_going_oncall_fcm_message(
290
271
alert = ApsAlert (title = notification_title , subtitle = notification_subtitle ),
291
272
sound = CriticalSound (
292
273
critical = False ,
293
- name = f" { info_notification_sound_name } { MobileAppUserSettings . IOS_SOUND_NAME_EXTENSION } " ,
274
+ name = mobile_app_user_settings . get_notification_sound_name ( MessageType . INFO , Platform . IOS ) ,
294
275
),
295
276
custom_data = {
296
277
"interruption-level" : "time-sensitive" ,
@@ -641,8 +622,6 @@ def _shift_swap_request_fcm_message(
641
622
device_to_notify : "FCMDevice" ,
642
623
mobile_app_user_settings : "MobileAppUserSettings" ,
643
624
) -> Message :
644
- from apps .mobile_app .models import MobileAppUserSettings
645
-
646
625
thread_id = f"{ shift_swap_request .public_primary_key } :{ user .public_primary_key } :ssr"
647
626
notification_title = "New shift swap request"
648
627
beneficiary_name = shift_swap_request .beneficiary .name or shift_swap_request .beneficiary .username
@@ -655,8 +634,8 @@ def _shift_swap_request_fcm_message(
655
634
"title" : notification_title ,
656
635
"subtitle" : notification_subtitle ,
657
636
"route" : route ,
658
- "info_notification_sound_name" : (
659
- mobile_app_user_settings . info_notification_sound_name + MobileAppUserSettings . ANDROID_SOUND_NAME_EXTENSION
637
+ "info_notification_sound_name" : mobile_app_user_settings . get_notification_sound_name (
638
+ MessageType . INFO , Platform . ANDROID
660
639
),
661
640
"info_notification_volume_type" : mobile_app_user_settings .info_notification_volume_type ,
662
641
"info_notification_volume" : str (mobile_app_user_settings .info_notification_volume ),
@@ -669,8 +648,7 @@ def _shift_swap_request_fcm_message(
669
648
alert = ApsAlert (title = notification_title , subtitle = notification_subtitle ),
670
649
sound = CriticalSound (
671
650
critical = False ,
672
- name = mobile_app_user_settings .info_notification_sound_name
673
- + MobileAppUserSettings .IOS_SOUND_NAME_EXTENSION ,
651
+ name = mobile_app_user_settings .get_notification_sound_name (MessageType .INFO , Platform .IOS ),
674
652
),
675
653
custom_data = {
676
654
"interruption-level" : "time-sensitive" ,
0 commit comments