Skip to content

feat(fcm): Added support for sending an image URL in notifications #332

New issue

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

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

Already on ? Sign in to your account

Merged
merged 8 commits into from
Sep 10, 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
19 changes: 16 additions & 3 deletions firebase_admin/_messaging_utils.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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):
Expand DownExpand Up@@ -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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need the same added to APNSFCMOptions. See firebase/firebase-admin-java#298

self.title = title
self.body = body
self.icon = icon
Expand All@@ -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):
Expand DownExpand Up@@ -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):
Expand DownExpand Up@@ -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')
Expand DownExpand Up@@ -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
Expand DownExpand Up@@ -851,13 +862,15 @@ 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):
raise ValueError('Message.notification must be an instance of Notification class.')
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)

Expand Down
8 changes: 6 additions & 2 deletions integration/test_messaging.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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(
Expand Down
10 changes: 8 additions & 2 deletions tests/test_messaging.py
Original file line numberDiff line numberDiff line change
Expand Up@@ -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'}},
})


Expand Down