Skip to content

feat(fcm): Added support for arbitrary key-value pairs in messaging.ApsAlert #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 14, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion firebase_admin/_messaging_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,13 @@ class ApsAlert(object):
action_loc_key: Key of the text in the app's string resources to use to localize the
action button text (optional).
launch_image: Image for the notification action (optional).
custom_data: A dict of custom key-value pairs to be included in the ApsAlert dictionary
(optional)
"""

def __init__(self, title=None, subtitle=None, body=None, loc_key=None, loc_args=None,
title_loc_key=None, title_loc_args=None, action_loc_key=None, launch_image=None):
title_loc_key=None, title_loc_args=None, action_loc_key=None, launch_image=None,
custom_data=None):
self.title = title
self.subtitle = subtitle
self.body = body
Expand All @@ -407,6 +410,7 @@ def __init__(self, title=None, subtitle=None, body=None, loc_key=None, loc_args=
self.title_loc_args = title_loc_args
self.action_loc_key = action_loc_key
self.launch_image = launch_image
self.custom_data = custom_data


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

@classmethod
Expand Down
66 changes: 66 additions & 0 deletions tests/test_messaging.py
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,72 @@ def test_aps_alert(self):
}
check_encoding(msg, expected)

def test_aps_alert_custom_data_merge(self):
msg = messaging.Message(
topic='topic',
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
alert=messaging.ApsAlert(
title='t',
subtitle='st',
custom_data={'k1': 'v1', 'k2': 'v2'}
)
),
)
)
)
expected = {
'topic': 'topic',
'apns': {
'payload': {
'aps': {
'alert': {
'title': 't',
'subtitle': 'st',
'k1': 'v1',
'k2': 'v2'
},
},
}
},
}
check_encoding(msg, expected)

def test_aps_alert_custom_data_override(self):
msg = messaging.Message(
topic='topic',
apns=messaging.APNSConfig(
payload=messaging.APNSPayload(
aps=messaging.Aps(
alert=messaging.ApsAlert(
title='t',
subtitle='st',
launch_image='li',
custom_data={'launch-image': ['li1', 'li2']}
)
),
)
)
)
expected = {
'topic': 'topic',
'apns': {
'payload': {
'aps': {
'alert': {
'title': 't',
'subtitle': 'st',
'launch-image': [
'li1',
'li2'
]
},
},
}
},
}
check_encoding(msg, expected)

class TestTimeout(object):

Expand Down