Skip to content

Commit dc4a6b5

Browse files
committed
feat(fcm): Enabled the direct_boot_ok parameter for FCM Android Config. (#734)
* feat(fcm): Enabled direct_boot_ok Android Config parameter. * Added tests. * fix: add to correct config. * fix: Validator label
1 parent 9182b83 commit dc4a6b5

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

firebase_admin/_messaging_encoder.py

+11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,15 @@ def check_analytics_label(cls, label, value):
160160
raise ValueError('Malformed {}.'.format(label))
161161
return value
162162

163+
@classmethod
164+
def check_boolean(cls, label, value):
165+
"""Checks if the given value is boolean."""
166+
if value is None:
167+
return None
168+
if not isinstance(value, bool):
169+
raise ValueError('{0} must be a boolean.'.format(label))
170+
return value
171+
163172
@classmethod
164173
def check_datetime(cls, label, value):
165174
"""Checks if the given value is a datetime."""
@@ -196,6 +205,8 @@ def encode_android(cls, android):
196205
'AndroidConfig.restricted_package_name', android.restricted_package_name),
197206
'ttl': cls.encode_ttl(android.ttl),
198207
'fcm_options': cls.encode_android_fcm_options(android.fcm_options),
208+
'direct_boot_ok': _Validators.check_boolean(
209+
'AndroidConfig.direct_boot_ok', android.direct_boot_ok),
199210
}
200211
result = cls.remove_null_values(result)
201212
priority = result.get('priority')

firebase_admin/_messaging_utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,20 @@ class AndroidConfig:
4949
strings. When specified, overrides any data fields set via ``Message.data``.
5050
notification: A ``messaging.AndroidNotification`` to be included in the message (optional).
5151
fcm_options: A ``messaging.AndroidFCMOptions`` to be included in the message (optional).
52+
direct_boot_ok: A boolean indicating whether messages will be allowed to be delivered to
53+
the app while the device is in direct boot mode (optional).
5254
"""
5355

5456
def __init__(self, collapse_key=None, priority=None, ttl=None, restricted_package_name=None,
55-
data=None, notification=None, fcm_options=None):
57+
data=None, notification=None, fcm_options=None, direct_boot_ok=None):
5658
self.collapse_key = collapse_key
5759
self.priority = priority
5860
self.ttl = ttl
5961
self.restricted_package_name = restricted_package_name
6062
self.data = data
6163
self.notification = notification
6264
self.fcm_options = fcm_options
65+
self.direct_boot_ok = direct_boot_ok
6366

6467

6568
class AndroidNotification:

tests/test_messaging.py

+22-3
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
NON_OBJECT_ARGS = [list(), tuple(), dict(), 'foo', 0, 1, True, False]
3434
NON_LIST_ARGS = ['', tuple(), dict(), True, False, 1, 0, [1], ['foo', 1]]
3535
NON_UINT_ARGS = ['1.23s', list(), tuple(), dict(), -1.23]
36+
NON_BOOL_ARGS = ['', list(), tuple(), dict(), 1, 0, [1], ['foo', 1], {1: 'foo'}, {'foo': 1}]
3637
HTTP_ERROR_CODES = {
3738
400: exceptions.InvalidArgumentError,
3839
403: exceptions.PermissionDeniedError,
@@ -249,7 +250,8 @@ def test_fcm_options(self):
249250
topic='topic',
250251
fcm_options=messaging.FCMOptions('message-label'),
251252
android=messaging.AndroidConfig(
252-
fcm_options=messaging.AndroidFCMOptions('android-label')),
253+
fcm_options=messaging.AndroidFCMOptions('android-label'),
254+
direct_boot_ok=False),
253255
apns=messaging.APNSConfig(fcm_options=
254256
messaging.APNSFCMOptions(
255257
analytics_label='apns-label',
@@ -259,7 +261,8 @@ def test_fcm_options(self):
259261
{
260262
'topic': 'topic',
261263
'fcm_options': {'analytics_label': 'message-label'},
262-
'android': {'fcm_options': {'analytics_label': 'android-label'}},
264+
'android': {'fcm_options': {'analytics_label': 'android-label'},
265+
'direct_boot_ok': False},
263266
'apns': {'fcm_options': {'analytics_label': 'apns-label',
264267
'image': 'https://images.unsplash.com/photo-14944386399'
265268
'46-1ebd1d20bf85?fit=crop&w=900&q=60'}},
@@ -317,6 +320,20 @@ def test_invalid_data(self, data):
317320
check_encoding(messaging.Message(
318321
topic='topic', android=messaging.AndroidConfig(data=data)))
319322

323+
@pytest.mark.parametrize('data', NON_STRING_ARGS)
324+
def test_invalid_analytics_label(self, data):
325+
with pytest.raises(ValueError):
326+
check_encoding(messaging.Message(
327+
topic='topic', android=messaging.AndroidConfig(
328+
fcm_options=messaging.AndroidFCMOptions(analytics_label=data))))
329+
330+
@pytest.mark.parametrize('data', NON_BOOL_ARGS)
331+
def test_invalid_direct_boot_ok(self, data):
332+
with pytest.raises(ValueError):
333+
check_encoding(messaging.Message(
334+
topic='topic', android=messaging.AndroidConfig(direct_boot_ok=data)))
335+
336+
320337
def test_android_config(self):
321338
msg = messaging.Message(
322339
topic='topic',
@@ -326,7 +343,8 @@ def test_android_config(self):
326343
priority='high',
327344
ttl=123,
328345
data={'k1': 'v1', 'k2': 'v2'},
329-
fcm_options=messaging.AndroidFCMOptions('analytics_label_v1')
346+
fcm_options=messaging.AndroidFCMOptions('analytics_label_v1'),
347+
direct_boot_ok=True,
330348
)
331349
)
332350
expected = {
@@ -343,6 +361,7 @@ def test_android_config(self):
343361
'fcm_options': {
344362
'analytics_label': 'analytics_label_v1',
345363
},
364+
'direct_boot_ok': True,
346365
},
347366
}
348367
check_encoding(msg, expected)

0 commit comments

Comments
 (0)