olucurious / PyFCM

Python client for FCM - Firebase Cloud Messaging (Android, iOS and Web)
http://olucurious.github.io/PyFCM/
MIT License
804 stars 205 forks source link

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

Open sgserg opened 3 months ago

sgserg commented 3 months ago

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'
olucurious commented 3 months ago

Please use v2.0.3

sgserg commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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 commented 3 months ago

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?

olucurious commented 3 months ago

Can you try the new version

sgserg commented 3 months ago

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

sgserg commented 3 months ago

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 commented 3 months ago

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

sgserg commented 3 months ago

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     ):
sathvik-grexit commented 3 months ago

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.

sgserg commented 3 months ago

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

tronku commented 2 months ago

@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?

dr0g0 commented 2 months ago

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)

criptocoko commented 2 months ago
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!

dhevendhiran-adt commented 1 week ago
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.