Skip to content

Commit 5408fbc

Browse files
viktoraslhiranya911
authored andcommitted
Add support for arbitrary key-value pairs in messaging.ApsAlert (#322)
1 parent bbfa5e8 commit 5408fbc

File tree

2 files changed

+79
-1
lines changed

2 files changed

+79
-1
lines changed

firebase_admin/_messaging_utils.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,13 @@ class ApsAlert(object):
394394
action_loc_key: Key of the text in the app's string resources to use to localize the
395395
action button text (optional).
396396
launch_image: Image for the notification action (optional).
397+
custom_data: A dict of custom key-value pairs to be included in the ApsAlert dictionary
398+
(optional)
397399
"""
398400

399401
def __init__(self, title=None, subtitle=None, body=None, loc_key=None, loc_args=None,
400-
title_loc_key=None, title_loc_args=None, action_loc_key=None, launch_image=None):
402+
title_loc_key=None, title_loc_args=None, action_loc_key=None, launch_image=None,
403+
custom_data=None):
401404
self.title = title
402405
self.subtitle = subtitle
403406
self.body = body
@@ -407,6 +410,7 @@ def __init__(self, title=None, subtitle=None, body=None, loc_key=None, loc_args=
407410
self.title_loc_args = title_loc_args
408411
self.action_loc_key = action_loc_key
409412
self.launch_image = launch_image
413+
self.custom_data = custom_data
410414

411415

412416
class APNSFcmOptions(object):
@@ -835,6 +839,14 @@ def encode_aps_alert(cls, alert):
835839
if result.get('title-loc-args') and not result.get('title-loc-key'):
836840
raise ValueError(
837841
'ApsAlert.title_loc_key is required when specifying title_loc_args.')
842+
if alert.custom_data is not None:
843+
if not isinstance(alert.custom_data, dict):
844+
raise ValueError('ApsAlert.custom_data must be a dict.')
845+
for key, val in alert.custom_data.items():
846+
_Validators.check_string('ApsAlert.custom_data key', key)
847+
# allow specifying key override because Apple could update API so that key
848+
# could have unexpected value type
849+
result[key] = val
838850
return cls.remove_null_values(result)
839851

840852
@classmethod

tests/test_messaging.py

+66
Original file line numberDiff line numberDiff line change
@@ -1209,6 +1209,72 @@ def test_aps_alert(self):
12091209
}
12101210
check_encoding(msg, expected)
12111211

1212+
def test_aps_alert_custom_data_merge(self):
1213+
msg = messaging.Message(
1214+
topic='topic',
1215+
apns=messaging.APNSConfig(
1216+
payload=messaging.APNSPayload(
1217+
aps=messaging.Aps(
1218+
alert=messaging.ApsAlert(
1219+
title='t',
1220+
subtitle='st',
1221+
custom_data={'k1': 'v1', 'k2': 'v2'}
1222+
)
1223+
),
1224+
)
1225+
)
1226+
)
1227+
expected = {
1228+
'topic': 'topic',
1229+
'apns': {
1230+
'payload': {
1231+
'aps': {
1232+
'alert': {
1233+
'title': 't',
1234+
'subtitle': 'st',
1235+
'k1': 'v1',
1236+
'k2': 'v2'
1237+
},
1238+
},
1239+
}
1240+
},
1241+
}
1242+
check_encoding(msg, expected)
1243+
1244+
def test_aps_alert_custom_data_override(self):
1245+
msg = messaging.Message(
1246+
topic='topic',
1247+
apns=messaging.APNSConfig(
1248+
payload=messaging.APNSPayload(
1249+
aps=messaging.Aps(
1250+
alert=messaging.ApsAlert(
1251+
title='t',
1252+
subtitle='st',
1253+
launch_image='li',
1254+
custom_data={'launch-image': ['li1', 'li2']}
1255+
)
1256+
),
1257+
)
1258+
)
1259+
)
1260+
expected = {
1261+
'topic': 'topic',
1262+
'apns': {
1263+
'payload': {
1264+
'aps': {
1265+
'alert': {
1266+
'title': 't',
1267+
'subtitle': 'st',
1268+
'launch-image': [
1269+
'li1',
1270+
'li2'
1271+
]
1272+
},
1273+
},
1274+
}
1275+
},
1276+
}
1277+
check_encoding(msg, expected)
12121278

12131279
class TestTimeout(object):
12141280

0 commit comments

Comments
 (0)