From 6c81d2f84fd1d1f99a3e728fe1d9a37f2f236426 Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Thu, 15 Nov 2018 18:30:54 +0100 Subject: [PATCH 1/3] fix FcmErrorCode error type --- CHANGELOG.md | 4 +++- firebase_admin/messaging.py | 2 +- tests/test_messaging.py | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b6f745b05..0dd676cb8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Unreleased -- +- [fixed] Fixing error handling in FCM. The SDK now checks the key + type.googleapis.com/google.firebase.fcm.v1.FcmError to set error code. + # v2.13.0 diff --git a/firebase_admin/messaging.py b/firebase_admin/messaging.py index 84134a950..28e80935c 100644 --- a/firebase_admin/messaging.py +++ b/firebase_admin/messaging.py @@ -943,7 +943,7 @@ def _handle_fcm_error(self, error): error_dict = data.get('error', {}) server_code = None for detail in error_dict.get('details', []): - if detail.get('@type') == 'type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode': + if detail.get('@type') == 'type.googleapis.com/google.firebase.fcm.v1.FcmError': server_code = detail.get('errorCode') break if not server_code: diff --git a/tests/test_messaging.py b/tests/test_messaging.py index 31c08c856..5b3a17232 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -1120,7 +1120,7 @@ def test_send_fcm_error_code(self, status): 'message': 'test error', 'details': [ { - '@type': 'type.googleapis.com/google.firebase.fcm.v1.FcmErrorCode', + '@type': 'type.googleapis.com/google.firebase.fcm.v1.FcmError', 'errorCode': 'UNREGISTERED', }, ], From 5f5614bc56122740c78b5af488b28af7b6c78763 Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Sat, 1 Dec 2018 01:29:41 +0100 Subject: [PATCH 2/3] add channel_id to messaging.AndroidNotification --- CHANGELOG.md | 2 +- firebase_admin/messaging.py | 6 +++++- tests/test_messaging.py | 9 ++++++++- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dd676cb8..3f474a9ba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,9 @@ # Unreleased +- [added] `messaging.AndroidNotification`type now supports channel_id. - [fixed] Fixing error handling in FCM. The SDK now checks the key type.googleapis.com/google.firebase.fcm.v1.FcmError to set error code. - # v2.13.0 - [added] The `db.Reference` type now provides a `listen()` API for diff --git a/firebase_admin/messaging.py b/firebase_admin/messaging.py index 28e80935c..96b38295e 100644 --- a/firebase_admin/messaging.py +++ b/firebase_admin/messaging.py @@ -246,11 +246,12 @@ class AndroidNotification(object): title text (optional). 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). """ 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): + title_loc_args=None, channel_id=None): self.title = title self.body = body self.icon = icon @@ -262,6 +263,7 @@ def __init__(self, title=None, body=None, icon=None, color=None, sound=None, tag self.body_loc_args = body_loc_args self.title_loc_key = title_loc_key self.title_loc_args = title_loc_args + self.channel_id = channel_id class WebpushConfig(object): @@ -596,6 +598,8 @@ def encode_android_notification(cls, notification): 'AndroidNotification.title_loc_args', notification.title_loc_args), 'title_loc_key': _Validators.check_string( 'AndroidNotification.title_loc_key', notification.title_loc_key), + 'channel_id': _Validators.check_string( + 'AndroidNotification.channel_id', notification.channel_id), } result = cls.remove_null_values(result) color = result.get('color') diff --git a/tests/test_messaging.py b/tests/test_messaging.py index 5b3a17232..aa6101c87 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -335,6 +335,12 @@ def test_no_body_loc_key(self): expected = 'AndroidNotification.body_loc_key is required when specifying body_loc_args.' assert str(excinfo.value) == expected + @pytest.mark.parametrize('data', NON_STRING_ARGS) + def test_invalid_channek_id(self, data): + notification = messaging.AndroidNotification(channel_id=data) + excinfo = self._check_notification(notification) + assert str(excinfo.value) == 'AndroidNotification.channelk_id must be a string.' + def test_android_notification(self): msg = messaging.Message( topic='topic', @@ -342,7 +348,7 @@ def test_android_notification(self): notification=messaging.AndroidNotification( title='t', body='b', icon='i', color='#112233', sound='s', tag='t', click_action='ca', title_loc_key='tlk', body_loc_key='blk', - title_loc_args=['t1', 't2'], body_loc_args=['b1', 'b2'] + title_loc_args=['t1', 't2'], body_loc_args=['b1', 'b2'], channel_id='c' ) ) ) @@ -361,6 +367,7 @@ def test_android_notification(self): 'body_loc_key': 'blk', 'title_loc_args': ['t1', 't2'], 'body_loc_args': ['b1', 'b2'], + 'channel_id' : 'c', }, }, } From 7a7253329c8c3de0609d8e2e808dcdc72a04d37f Mon Sep 17 00:00:00 2001 From: Cyrille Hemidy Date: Sat, 1 Dec 2018 01:39:41 +0100 Subject: [PATCH 3/3] fix test --- tests/test_messaging.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_messaging.py b/tests/test_messaging.py index aa6101c87..a31f16447 100644 --- a/tests/test_messaging.py +++ b/tests/test_messaging.py @@ -339,7 +339,7 @@ def test_no_body_loc_key(self): def test_invalid_channek_id(self, data): notification = messaging.AndroidNotification(channel_id=data) excinfo = self._check_notification(notification) - assert str(excinfo.value) == 'AndroidNotification.channelk_id must be a string.' + assert str(excinfo.value) == 'AndroidNotification.channel_id must be a string.' def test_android_notification(self): msg = messaging.Message(