Skip to content

Commit

Permalink
Changed firebase-admin's deprecated send_all method for send_each (#715)
Browse files Browse the repository at this point in the history
  • Loading branch information
guspix authored Apr 28, 2024
1 parent 44977b1 commit 0f79181
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Dependencies
- For WebPush (WP), pywebpush 1.3.0+ is required (optional). py-vapid 1.3.0+ is required for generating the WebPush private key; however this
step does not need to occur on the application server.
- For Apple Push (APNS), apns2 0.3+ is required (optional).
- For FCM, firebase-admin 5+ is required (optional).
- For FCM, firebase-admin 6.2+ is required (optional).

Setup
-----
Expand Down
2 changes: 1 addition & 1 deletion push_notifications/gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ def send_message(
messages = [
_prepare_message(message, token) for token in chunk
]
responses = messaging.send_all(messages, dry_run=dry_run, app=app).responses
responses = messaging.send_each(messages, dry_run=dry_run, app=app).responses
ret.extend(responses)
_deactivate_devices_with_error_results(registration_ids, ret)
return messaging.BatchResponse(ret)
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ APNS =

WP = pywebpush>=1.3.0

FCM = firebase-admin>=5,<6
FCM = firebase-admin>=6.2


[options.packages.find]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def test_send_bulk_messages_action(self):
admin.message_user = mock.Mock()

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
admin.send_messages(request, queryset, bulk=True)

Expand Down Expand Up @@ -61,7 +61,7 @@ def test_send_single_message_action(self):
admin.message_user = mock.Mock()

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
admin.send_messages(request, queryset, bulk=False)

Expand Down Expand Up @@ -102,7 +102,7 @@ def test_send_bulk_messages_action_fail(self):
)

with mock.patch(
"firebase_admin.messaging.send_all", return_value=response
"firebase_admin.messaging.send_each", return_value=response
) as p:
admin.send_messages(request, queryset, bulk=True)

Expand Down
4 changes: 2 additions & 2 deletions tests/test_gcm_push_payload.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class GCMPushPayloadTest(TestCase):

def test_fcm_push_payload(self):
with mock.patch("firebase_admin.messaging.send_all", return_value=FCM_SUCCESS) as p:
with mock.patch("firebase_admin.messaging.send_each", return_value=FCM_SUCCESS) as p:
message = dict_to_fcm_message({"message": "Hello world"})

send_message("abc", message)
Expand All @@ -37,7 +37,7 @@ def test_fcm_push_payload(self):
self.assertEqual(message.android.notification.body, "Hello world")

def test_fcm_push_payload_many(self):
with mock.patch("firebase_admin.messaging.send_all", return_value=FCM_SUCCESS) as p:
with mock.patch("firebase_admin.messaging.send_each", return_value=FCM_SUCCESS) as p:
message = dict_to_fcm_message({"message": "Hello world"})

send_message(["abc", "123"], message)
Expand Down
36 changes: 18 additions & 18 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def test_can_create_save_device(self):
def test_fcm_send_message(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
device.send_message("Hello world")

Expand All @@ -65,7 +65,7 @@ def test_fcm_send_message(self):
def test_fcm_send_message_with_fcm_message(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
message_to_send = messaging.Message(
notification=messaging.Notification(
Expand Down Expand Up @@ -99,7 +99,7 @@ def test_fcm_send_message_with_fcm_message(self):
def test_fcm_send_message_extra_data(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
device.send_message("Hello world", extra={"foo": "bar"})

Expand All @@ -125,7 +125,7 @@ def test_fcm_send_message_extra_data(self):
def test_fcm_send_message_extra_options(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
device.send_message("Hello world", collapse_key="test_key", foo="bar")

Expand All @@ -152,7 +152,7 @@ def test_fcm_send_message_extra_options(self):
def test_fcm_send_message_extra_notification(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
device.send_message("Hello world", extra={"icon": "test_icon"}, title="test")

Expand Down Expand Up @@ -180,7 +180,7 @@ def test_fcm_send_message_extra_notification(self):
def test_fcm_send_message_extra_options_and_notification_and_data(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="FCM")
with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS
) as p:
device.send_message(
"Hello world",
Expand Down Expand Up @@ -215,7 +215,7 @@ def test_fcm_send_message_to_multiple_devices(self):
self._create_fcm_devices(["abc", "abc1"])

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
GCMDevice.objects.all().send_message("Hello world")

Expand Down Expand Up @@ -245,7 +245,7 @@ def test_fcm_send_message_to_multiple_devices_fcm_message(self):
self._create_fcm_devices(["abc", "abc1"])

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
message_to_send = messaging.Message(
notification=messaging.Notification(
Expand Down Expand Up @@ -283,7 +283,7 @@ def test_gcm_send_message_does_not_send(self):
device = GCMDevice.objects.create(registration_id="abc", cloud_message_type="GCM")

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
message_to_send = messaging.Message(
notification=messaging.Notification(
Expand All @@ -299,7 +299,7 @@ def test_gcm_send_multiple_message_does_not_send(self):
self._create_devices(["abc", "abc1"])

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
message_to_send = messaging.Message(
notification=messaging.Notification(
Expand All @@ -318,7 +318,7 @@ def test_fcm_send_message_active_devices(self):
GCMDevice.objects.create(registration_id="xyz", active=False, cloud_message_type="FCM")

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
GCMDevice.objects.all().send_message("Hello world")

Expand All @@ -344,7 +344,7 @@ def test_fcm_send_message_collapse_to_multiple_devices(self):
self._create_fcm_devices(["abc", "abc1"])

with mock.patch(
"firebase_admin.messaging.send_all", return_value=responses.FCM_SUCCESS_MULTIPLE
"firebase_admin.messaging.send_each", return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
GCMDevice.objects.all().send_message("Hello world", collapse_key="test_key")

Expand Down Expand Up @@ -386,7 +386,7 @@ def test_fcm_send_message_to_single_device_with_error(self):
[SendResponse(resp={"name": "..."}, exception=error)]
)
with mock.patch(
"firebase_admin.messaging.send_all", return_value=return_value
"firebase_admin.messaging.send_each", return_value=return_value
):
device = GCMDevice.objects.get(registration_id=devices[index])
device.send_message("Hello World!")
Expand All @@ -399,7 +399,7 @@ def test_fcm_send_message_to_single_device_with_error_mismatch(self):
[SendResponse(resp={"name": "..."}, exception=OSError())]
)
with mock.patch(
"firebase_admin.messaging.send_all",
"firebase_admin.messaging.send_each",
return_value=return_value
):
# these errors are not device specific, device is not deactivated
Expand All @@ -417,7 +417,7 @@ def test_fcm_send_message_to_multiple_devices_with_error(self):
SendResponse(resp={"name": "..."}, exception=InvalidArgumentError("Invalid registration")),
])
with mock.patch(
"firebase_admin.messaging.send_all", return_value=return_value
"firebase_admin.messaging.send_each", return_value=return_value
):
GCMDevice.objects.all().send_message("Hello World")
self.assertFalse(GCMDevice.objects.get(registration_id="abc").active)
Expand All @@ -436,7 +436,7 @@ def test_fcm_send_message_to_multiple_devices_with_error_b(self):
])

with mock.patch(
"firebase_admin.messaging.send_all", return_value=return_value
"firebase_admin.messaging.send_each", return_value=return_value
):
GCMDevice.objects.all().send_message("Hello World")
self.assertTrue(GCMDevice.objects.get(registration_id="abc").active)
Expand All @@ -448,14 +448,14 @@ def test_fcm_send_message_with_no_reg_ids(self):
self._create_fcm_devices(["abc", "abc1"])

with mock.patch(
"firebase_admin.messaging.send_all",
"firebase_admin.messaging.send_each",
return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
GCMDevice.objects.filter(registration_id="xyz").send_message("Hello World")
p.assert_not_called()

with mock.patch(
"firebase_admin.messaging.send_all",
"firebase_admin.messaging.send_each",
return_value=responses.FCM_SUCCESS_MULTIPLE
) as p:
reg_ids = [obj.registration_id for obj in GCMDevice.objects.all()]
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ deps =
pytest-django
pywebpush
djangorestframework
firebase-admin>=5,<6
firebase-admin>=6.2
dj22: Django>=2.2,<3.0
dj32: Django>=3.2,<3.3
dj40: Django>=4.0,<4.0.5
Expand Down

0 comments on commit 0f79181

Please sign in to comment.