Search code examples
pythonflutternotificationsfirebase-cloud-messaging

How to send Notification to Multiple device


I'm trying to do crypto app that need to notify all user in same time. Sending notification for single device worked for me. But how to send multiple device notification using python?

This Code send notification to single device only

import requests
import json   
serverToken = 'token here'
                
deviceToken = 'token here'

headers = {
        'Content-Type': 'application/json',
        'Authorization': 'key=' + serverToken,
      }

body = {
          'notification': {'title': 'Sending push form python script',
                            'body': 'OMG lets goo1'
                            },
          'to':
              deviceToken,
          'priority': 'high',
        #   'data': dataPayLoad,
        }
response = requests.post("https://fcm.googleapis.com/fcm/send",headers = headers, data=json.dumps(body))
print(response.status_code)

print(response.json())

(I just want to sent notification all user, who having flutter app)

Edit I can make list of all device token but, is there anyway that sending notification who has my app.(without listing all device token)


Solution

  • You really need to provide more context. Karl Knechtel's answer is important to consider. However, I will say that in very general terms, it seems asynchronous programming might be a solution. You might call an asynchronous function to alert all your members while running your functions. This would allow the program to continue running as normal, not slowed down by notifying multiple users, while running a separate asynchronous function to notify as many people you want.

    Again: this is an extremely general solution to a problem that could very well not be the one you're experiencing, because you really need to provide sample code, context, and be clearer about what you need help with.

    Edit:

    Adding more for clarity and specificity.

    If the issue is you can't have your program delayed because it's notifying a large volume of recipients:

    Consider a structure like:

    async def alertMembers()
    
    async def program()
    

    In which you can call or await your program to alert all your members without disrupting the flow of the actual program.

    If the issue is that you don't know how to alert more than one member using whatever alert method you are:

    Consider a recipients list, read from a CSV or JSON, that your program iterates in a for loop to get to all the recipients. Like so:

    recipients = pd.read_csv('recipients.csv') # Using Pandas and CSV
    
    with open('recipients.json', 'r') as f: # Using inbuilt JSON parsing
        recipientsJSON = json.load(f)
    
    # Sample recipientsJSON dictionary:
    recipients = {
        "John": ["19280120987"],
        "Mary": ["19283192832"]
    }
    
    # Executing with a for loop
    for recipient in recipientsJSON:
        sms.send_message(
            "number": recipientsJSON[recipient][0]
            "content": 'something' # Your alert
        )
    

    This type of a structure would execute quite efficiently and can handle any number of recipients.

    These are some potential solutions to a very vague and unknown problem, so please edit, rephrase, and add context to your question so it can be answered appropriately.