Skip to content

Commit b563895

Browse files
committed
feat(fcm): Support proxy field in FCM AndroidNotification
1 parent 3c86208 commit b563895

File tree

4 files changed

+33
-3
lines changed

4 files changed

+33
-3
lines changed

firebase_admin/_messaging_encoder.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,9 @@ def encode_android_notification(cls, notification):
319319
'visibility': _Validators.check_string(
320320
'AndroidNotification.visibility', notification.visibility, non_empty=True),
321321
'notification_count': _Validators.check_number(
322-
'AndroidNotification.notification_count', notification.notification_count)
322+
'AndroidNotification.notification_count', notification.notification_count),
323+
'proxy': _Validators.check_string(
324+
'AndroidNotification.proxy', notification.proxy, non_empty=True)
323325
}
324326
result = cls.remove_null_values(result)
325327
color = result.get('color')
@@ -363,6 +365,13 @@ def encode_android_notification(cls, notification):
363365
'AndroidNotification.vibrate_timings_millis', msec)
364366
vibrate_timing_strings.append(formated_string)
365367
result['vibrate_timings'] = vibrate_timing_strings
368+
369+
proxy = result.get('proxy')
370+
if proxy:
371+
if proxy not in ('allow', 'deny', 'if_priority_lowered'):
372+
raise ValueError(
373+
'AndroidNotification.proxy must be "allow", "deny" or "if_priority_lowered".')
374+
result['proxy'] = proxy.upper()
366375
return result
367376

368377
@classmethod

firebase_admin/_messaging_utils.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ class AndroidNotification:
145145
want the count here to represent the number of total new messages. If zero or
146146
unspecified, systems that support badging use the default, which is to increment a
147147
number displayed on the long-press menu each time a new notification arrives.
148+
proxy: Sets if the notification may be proxied. Must be one of ``allow``, ``deny``, or
149+
``if_priority_lowered``. If unspecified, defaults to ``if_priority_lowered``.
148150
149151
150152
"""
@@ -154,7 +156,8 @@ def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag
154156
title_loc_args=None, channel_id=None, image=None, ticker=None, sticky=None,
155157
event_timestamp=None, local_only=None, priority=None, vibrate_timings_millis=None,
156158
default_vibrate_timings=None, default_sound=None, light_settings=None,
157-
default_light_settings=None, visibility=None, notification_count=None):
159+
default_light_settings=None, visibility=None, notification_count=None,
160+
proxy=None):
158161
self.title = title
159162
self.body = body
160163
self.icon = icon
@@ -180,6 +183,7 @@ def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag
180183
self.default_light_settings = default_light_settings
181184
self.visibility = visibility
182185
self.notification_count = notification_count
186+
self.proxy = proxy
183187

184188

185189
class LightSettings:

integration/test_messaging.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def test_send():
5555
light_off_duration_millis=200,
5656
light_on_duration_millis=300
5757
),
58-
notification_count=1
58+
notification_count=1,
59+
proxy='if_priority_lowered',
5960
)
6061
),
6162
apns=messaging.APNSConfig(payload=messaging.APNSPayload(

tests/test_messaging.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,20 @@ def test_invalid_visibility(self, visibility):
534534
else:
535535
expected = 'AndroidNotification.visibility must be a non-empty string.'
536536
assert str(excinfo.value) == expected
537+
538+
@pytest.mark.parametrize('proxy', NON_STRING_ARGS + ['foo'])
539+
def test_invalid_proxy(self, proxy):
540+
notification = messaging.AndroidNotification(proxy=proxy)
541+
excinfo = self._check_notification(notification)
542+
if isinstance(proxy, str):
543+
if not proxy:
544+
expected = 'AndroidNotification.proxy must be a non-empty string.'
545+
else:
546+
expected = ('AndroidNotification.proxy must be "allow", "deny" or'
547+
' "if_priority_lowered".')
548+
else:
549+
expected = 'AndroidNotification.proxy must be a non-empty string.'
550+
assert str(excinfo.value) == expected
537551

538552
@pytest.mark.parametrize('vibrate_timings', ['', 1, True, 'msec', ['500', 500], [0, 'abc']])
539553
def test_invalid_vibrate_timings_millis(self, vibrate_timings):
@@ -580,6 +594,7 @@ def test_android_notification(self):
580594
light_off_duration_millis=300,
581595
),
582596
default_light_settings=False, visibility='public', notification_count=1,
597+
proxy='if_priority_lowered',
583598
)
584599
)
585600
)
@@ -620,6 +635,7 @@ def test_android_notification(self):
620635
'default_light_settings': False,
621636
'visibility': 'PUBLIC',
622637
'notification_count': 1,
638+
'proxy': 'IF_PRIORITY_LOWERED'
623639
},
624640
},
625641
}

0 commit comments

Comments
 (0)