diff --git a/firebase_admin/_messaging_utils.py b/firebase_admin/_messaging_utils.py index 72e2acab3..34738b168 100644 --- a/firebase_admin/_messaging_utils.py +++ b/firebase_admin/_messaging_utils.py @@ -89,11 +89,13 @@ class Notification(object): Args: title: Title of the notification (optional). body: Body of the notification (optional). + image: Image url of the notification (optional) """ - def __init__(self, title=None, body=None): + def __init__(self, title=None, body=None, image=None): self.title = title self.body = body + self.image = image class AndroidConfig(object): @@ -153,11 +155,12 @@ class AndroidNotification(object): title_loc_args: A list of resource keys that will be used in place of the format specifiers in ``title_loc_key`` (optional). channel_id: channel_id of the notification (optional). + image: Image url of the notification (optional). """ def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag=None, click_action=None, body_loc_key=None, body_loc_args=None, title_loc_key=None, - title_loc_args=None, channel_id=None): + title_loc_args=None, channel_id=None, image=None): self.title = title self.body = body self.icon = icon @@ -170,6 +173,7 @@ def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag self.title_loc_key = title_loc_key self.title_loc_args = title_loc_args self.channel_id = channel_id + self.image = image class AndroidFCMOptions(object): @@ -419,10 +423,13 @@ class APNSFCMOptions(object): Args: analytics_label: contains additional options for features provided by the FCM iOS SDK (optional). + image: contains the URL of an image that is going to be displayed in a notification + (optional). """ - def __init__(self, analytics_label=None): + def __init__(self, analytics_label=None, image=None): self.analytics_label = analytics_label + self.image = image class FCMOptions(object): @@ -600,6 +607,9 @@ def encode_android_notification(cls, notification): 'AndroidNotification.title_loc_key', notification.title_loc_key), 'channel_id': _Validators.check_string( 'AndroidNotification.channel_id', notification.channel_id), + 'image': _Validators.check_string( + 'image', notification.image + ) } result = cls.remove_null_values(result) color = result.get('color') @@ -754,6 +764,7 @@ def encode_apns_fcm_options(cls, fcm_options): result = { 'analytics_label': _Validators.check_analytics_label( 'APNSFCMOptions.analytics_label', fcm_options.analytics_label), + 'image': _Validators.check_string('APNSFCMOptions.image', fcm_options.image) } result = cls.remove_null_values(result) return result @@ -851,6 +862,7 @@ def encode_aps_alert(cls, alert): @classmethod def encode_notification(cls, notification): + """Encodes an Notification instance into JSON.""" if notification is None: return None if not isinstance(notification, Notification): @@ -858,6 +870,7 @@ def encode_notification(cls, notification): result = { 'body': _Validators.check_string('Notification.body', notification.body), 'title': _Validators.check_string('Notification.title', notification.title), + 'image': _Validators.check_string('Notification.image', notification.image) } return cls.remove_null_values(result) diff --git a/integration/test_messaging.py b/integration/test_messaging.py index 7ebd5866a..b1caa09f9 100644 --- a/integration/test_messaging.py +++ b/integration/test_messaging.py @@ -27,12 +27,16 @@ def test_send(): msg = messaging.Message( topic='foo-bar', - notification=messaging.Notification('test-title', 'test-body'), + notification=messaging.Notification('test-title', 'test-body', + 'https://images.unsplash.com/photo-1494438639946' + '-1ebd1d20bf85?fit=crop&w=900&q=60'), android=messaging.AndroidConfig( restricted_package_name='com.google.firebase.demos', notification=messaging.AndroidNotification( title='android-title', - body='android-body' + body='android-body', + image='https://images.unsplash.com/' + 'photo-1494438639946-1ebd1d20bf85?fit=crop&w=900&q=60' ) ), apns=messaging.APNSConfig(payload=messaging.APNSPayload( diff --git a/tests/test_messaging.py b/tests/test_messaging.py index 0b8739195..4f7520045 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -197,13 +197,19 @@ def test_fcm_options(self): fcm_options=messaging.FCMOptions('message-label'), android=messaging.AndroidConfig( fcm_options=messaging.AndroidFCMOptions('android-label')), - apns=messaging.APNSConfig(fcm_options=messaging.APNSFCMOptions('apns-label')) + apns=messaging.APNSConfig(fcm_options= + messaging.APNSFCMOptions( + analytics_label='apns-label', + image='https://images.unsplash.com/photo-14944386399' + '46-1ebd1d20bf85?fit=crop&w=900&q=60')) ), { 'topic': 'topic', 'fcm_options': {'analytics_label': 'message-label'}, 'android': {'fcm_options': {'analytics_label': 'android-label'}}, - 'apns': {'fcm_options': {'analytics_label': 'apns-label'}}, + 'apns': {'fcm_options': {'analytics_label': 'apns-label', + 'image': 'https://images.unsplash.com/photo-14944386399' + '46-1ebd1d20bf85?fit=crop&w=900&q=60'}}, })