PyFCM icon indicating copy to clipboard operation
PyFCM copied to clipboard

How should we send notifications to multiple tokens with the updated API?

Open sgserg opened this issue 1 year ago • 16 comments

Attempting to use async_notify_multiple_devices yields

Traceback (most recent call last):
   ...
   ...
  File "/usr/local/lib/python3.6/dist-packages/pyfcm/fcm.py", line 81, in async_notify_multiple_devices
    return self.send_async_request(payloads=payloads, timeout=timeout)
TypeError: send_async_request() got an unexpected keyword argument 'payloads'

sgserg avatar Jun 20 '24 12:06 sgserg

Please use v2.0.3

olucurious avatar Jun 20 '24 12:06 olucurious

Thanks for the quick response! I've just updated the package with pip install --upgrade pyfcm and it ended up installing 2.0.0.

I'm on Python 3.6.9. Could that be the reason?

sgserg avatar Jun 20 '24 12:06 sgserg

Tried pip-installing 2.0.3 from github, but it failed with google-auth being too old (2.22.0 being the latest for Python 3.6 according to this).

Sending notification to a single device works fine, so there is no problem with auth in itself.

Is there a way we could make it work with older google-auth?

sgserg avatar Jun 20 '24 13:06 sgserg

Unfortunately I’m stuck on an older version (at most 2.22.0), on which pyfcm 2.0.0 installed and seemingly works fine (except the multiple device issue).

On 20 Jun 2024, at 16:11, Emmanuel O. Adegbite @.***> wrote:

Did you mean to say "make it work with newer google-auth?"

— Reply to this email directly, view it on GitHub https://github.com/olucurious/PyFCM/issues/338#issuecomment-2180647682, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABJQCZYQGAYAZJB6TYSSYG3ZILIGZAVCNFSM6AAAAABJT3E4TKVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOBQGY2DONRYGI. You are receiving this because you authored the thread.

sgserg avatar Jun 20 '24 13:06 sgserg

Changing the requirement to 2.22.0 and installing aiohttp makes our tests pass on 2.0.3.

What might break due to this requirement change?

sgserg avatar Jun 20 '24 13:06 sgserg

Can you try the new version

olucurious avatar Jun 20 '24 20:06 olucurious

Could you please update setup.py too? (reference 2.22.0 instead of 2.29.0)

sgserg avatar Jun 25 '24 11:06 sgserg

Also, annotations import fails on 3.6.9:

  File "/usr/src/app/coral/PyFCM/pyfcm/__init__.py", line 14, in <module>
    from .fcm import FCMNotification
  File "/usr/src/app/coral/PyFCM/pyfcm/fcm.py", line 1, in <module>
    from .baseapi import BaseAPI
  File "/usr/src/app/coral/PyFCM/pyfcm/baseapi.py", line 1
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

sgserg avatar Jun 25 '24 11:06 sgserg

(also, aiohttp (3.8.6) dependency still needs to be manually installed)

sgserg avatar Jun 26 '24 08:06 sgserg

one more thing: the following comma in pyfcm/baseapi.py seems to cause troubles for both iOS and Android:

 34         proxy_dict=None,
 35         env=None,
 36         json_encoder=None,
 37         adapter=None >>> , <<<<
 38     ):

sgserg avatar Jun 26 '24 10:06 sgserg

Can someone please help me with the structure of the payload that needs to be passed to async_notify_multiple_devices() I have fcm_token, notification_title, notification_body, data_payload params that can be passed to the above function.

sathvik-grexit avatar Jun 26 '24 14:06 sathvik-grexit

@sathvik-grexit it's apparently the same as for a single notify you just make an array of those.

sgserg avatar Jun 26 '24 15:06 sgserg

@sgserg can you please add an example in readme or here? older -

pushService.notify_multiple_devices(
        registration_ids=tokens,
        data_message=data
    )

what should be the new way?

serializer avatar Jul 18 '24 20:07 serializer

Attempting to use async_notify_multiple_devices yields

Traceback (most recent call last):
   ...
   ...
  File "/usr/local/lib/python3.6/dist-packages/pyfcm/fcm.py", line 81, in async_notify_multiple_devices
    return self.send_async_request(payloads=payloads, timeout=timeout)
TypeError: send_async_request() got an unexpected keyword argument 'payloads'

push_service = FCMNotification(service_account_file="", project_id="")

fcm_tokens = [] notification_title = "Notification Title" notification_body = "Notification Body" data_payload = {}

params_list =[{"fcm_token":fcm_token,"notification_title":notification_title,"notification_body":notification_body,"notification_image":'https://synak.pro/static/img/about-1.png',"data_payload":data_payload} for fcm_token in fcm_tokens]

result = push_service.async_notify_multiple_devices(params_list=params_list, timeout=5)

dr0g0 avatar Jul 21 '24 14:07 dr0g0

params_list =[{"fcm_token":fcm_token,"notification_title":notification_title,"notification_body":notification_body,"notification_image":'https://synak.pro/static/img/about-1.png',"data_payload":data_payload} for fcm_token in fcm_tokens]

result = push_service.async_notify_multiple_devices(params_list=params_list, timeout=5)

Why are we repeating the same data for each key instead of just sending a list of registration_ids as we did in older versions? This approach seems redundant and might complicate the process. Could we simplify this by going back to the previous versions (sending the registration_ids)

Thanks!

criptocoko avatar Aug 01 '24 16:08 criptocoko

params_list =[{"fcm_token":fcm_token,"notification_title":notification_title,"notification_body":notification_body,"notification_image":'https://synak.pro/static/img/about-1.png',"data_payload":data_payload} for fcm_token in fcm_tokens]

result = push_service.async_notify_multiple_devices(params_list=params_list, timeout=5)

Why are we repeating the same data for each key instead of just sending a list of registration_ids as we did in older versions? This approach seems redundant and might complicate the process. Could we simplify this by going back to the previous versions (sending the registration_ids)

Thanks!

Actually this is solving the another issue, If we have to send different notification messages to difference users at the same time. Comparing with multiple single notify() call in the loop, this is better.

dhevendhiran-adt avatar Sep 24 '24 11:09 dhevendhiran-adt