Skip to content

Commit 0ed372e

Browse files
cchammhiranya911
authored andcommitted
feat(fcm): Added support for sending an image URL in notifications (#332)
* Add "image" field to AndroidNotification and Notification * Add Doc String to _messaging_utils.encode_notification * Shorten Line Width * Add "image" field to APNSFCMOptions. * APNSFCMOptions: Shorten line
1 parent 5d4d6cb commit 0ed372e

File tree

3 files changed

+30
-7
lines changed

3 files changed

+30
-7
lines changed

firebase_admin/_messaging_utils.py

+16-3
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ class Notification(object):
8989
Args:
9090
title: Title of the notification (optional).
9191
body: Body of the notification (optional).
92+
image: Image url of the notification (optional)
9293
"""
9394

94-
def __init__(self, title=None, body=None):
95+
def __init__(self, title=None, body=None, image=None):
9596
self.title = title
9697
self.body = body
98+
self.image = image
9799

98100

99101
class AndroidConfig(object):
@@ -153,11 +155,12 @@ class AndroidNotification(object):
153155
title_loc_args: A list of resource keys that will be used in place of the format specifiers
154156
in ``title_loc_key`` (optional).
155157
channel_id: channel_id of the notification (optional).
158+
image: Image url of the notification (optional).
156159
"""
157160

158161
def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag=None,
159162
click_action=None, body_loc_key=None, body_loc_args=None, title_loc_key=None,
160-
title_loc_args=None, channel_id=None):
163+
title_loc_args=None, channel_id=None, image=None):
161164
self.title = title
162165
self.body = body
163166
self.icon = icon
@@ -170,6 +173,7 @@ def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag
170173
self.title_loc_key = title_loc_key
171174
self.title_loc_args = title_loc_args
172175
self.channel_id = channel_id
176+
self.image = image
173177

174178

175179
class AndroidFCMOptions(object):
@@ -419,10 +423,13 @@ class APNSFCMOptions(object):
419423
Args:
420424
analytics_label: contains additional options for features provided by the FCM iOS SDK
421425
(optional).
426+
image: contains the URL of an image that is going to be displayed in a notification
427+
(optional).
422428
"""
423429

424-
def __init__(self, analytics_label=None):
430+
def __init__(self, analytics_label=None, image=None):
425431
self.analytics_label = analytics_label
432+
self.image = image
426433

427434

428435
class FCMOptions(object):
@@ -600,6 +607,9 @@ def encode_android_notification(cls, notification):
600607
'AndroidNotification.title_loc_key', notification.title_loc_key),
601608
'channel_id': _Validators.check_string(
602609
'AndroidNotification.channel_id', notification.channel_id),
610+
'image': _Validators.check_string(
611+
'image', notification.image
612+
)
603613
}
604614
result = cls.remove_null_values(result)
605615
color = result.get('color')
@@ -754,6 +764,7 @@ def encode_apns_fcm_options(cls, fcm_options):
754764
result = {
755765
'analytics_label': _Validators.check_analytics_label(
756766
'APNSFCMOptions.analytics_label', fcm_options.analytics_label),
767+
'image': _Validators.check_string('APNSFCMOptions.image', fcm_options.image)
757768
}
758769
result = cls.remove_null_values(result)
759770
return result
@@ -851,13 +862,15 @@ def encode_aps_alert(cls, alert):
851862

852863
@classmethod
853864
def encode_notification(cls, notification):
865+
"""Encodes an Notification instance into JSON."""
854866
if notification is None:
855867
return None
856868
if not isinstance(notification, Notification):
857869
raise ValueError('Message.notification must be an instance of Notification class.')
858870
result = {
859871
'body': _Validators.check_string('Notification.body', notification.body),
860872
'title': _Validators.check_string('Notification.title', notification.title),
873+
'image': _Validators.check_string('Notification.image', notification.image)
861874
}
862875
return cls.remove_null_values(result)
863876

integration/test_messaging.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@
2727
def test_send():
2828
msg = messaging.Message(
2929
topic='foo-bar',
30-
notification=messaging.Notification('test-title', 'test-body'),
30+
notification=messaging.Notification('test-title', 'test-body',
31+
'https://images.unsplash.com/photo-1494438639946'
32+
'-1ebd1d20bf85?fit=crop&w=900&q=60'),
3133
android=messaging.AndroidConfig(
3234
restricted_package_name='com.google.firebase.demos',
3335
notification=messaging.AndroidNotification(
3436
title='android-title',
35-
body='android-body'
37+
body='android-body',
38+
image='https://images.unsplash.com/'
39+
'photo-1494438639946-1ebd1d20bf85?fit=crop&w=900&q=60'
3640
)
3741
),
3842
apns=messaging.APNSConfig(payload=messaging.APNSPayload(

tests/test_messaging.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,19 @@ def test_fcm_options(self):
197197
fcm_options=messaging.FCMOptions('message-label'),
198198
android=messaging.AndroidConfig(
199199
fcm_options=messaging.AndroidFCMOptions('android-label')),
200-
apns=messaging.APNSConfig(fcm_options=messaging.APNSFCMOptions('apns-label'))
200+
apns=messaging.APNSConfig(fcm_options=
201+
messaging.APNSFCMOptions(
202+
analytics_label='apns-label',
203+
image='https://images.unsplash.com/photo-14944386399'
204+
'46-1ebd1d20bf85?fit=crop&w=900&q=60'))
201205
),
202206
{
203207
'topic': 'topic',
204208
'fcm_options': {'analytics_label': 'message-label'},
205209
'android': {'fcm_options': {'analytics_label': 'android-label'}},
206-
'apns': {'fcm_options': {'analytics_label': 'apns-label'}},
210+
'apns': {'fcm_options': {'analytics_label': 'apns-label',
211+
'image': 'https://images.unsplash.com/photo-14944386399'
212+
'46-1ebd1d20bf85?fit=crop&w=900&q=60'}},
207213
})
208214

209215

0 commit comments

Comments
 (0)