Firebase Cloud Messaging with Flask: How can I send fcm_options link with the message from flask server?

17 views Asked by At

Firebase documentation says that link is in the WebpushFcmOptions object, and that object is within the WebpushConfig object.

Here's app.py of flask server:

from firebase_admin import messaging
@app.route('/send', methods=['GET'])
def send_push():
    if request.method == "GET":
        # get registration token from wherever you've stored it
        registration_token = ''

        pushlink = "https://myurl.com" # must be https

        message = messaging.Message(
            data={
                'score': '850',
                'time': '2:45',
            },
            token= registration_token,
            webpush = messaging.WebpushConfig(
                fcm_options = messaging.WebpushFcmOptions(
                    link = pushlink
                )
            ),
            notification=messaging.Notification(
                title='New Event',
                body='You have a new event!'
            ),
        )
        
        # send a message to the device corresponding to the provided registration token
        response = messaging.send(message)
        print('Successfully sent message:', response) 
    
    return 'ok'

This repeatedly returns the error: messaging doesn't have 'WebpushFcmOptions' attribute.

Tried different variations of the various objects within Firebase REST API documentation

1

There are 1 answers

0
Joey On BEST ANSWER

Answer:

Instead of webpush = messaging.WebpushFcmOptions(), the "Fcm" must be capitalized.

Correct code: webpush = messaging.WebpushFCMOptions()

Full correct code:

from firebase_admin import messaging
@app.route('/send', methods=['GET'])
def send_push():
    if request.method == "GET":
        # get registration token from wherever you've stored it
        registration_token = ''

        pushlink = "https://myurl.com" # must be https

        message = messaging.Message(
            data={
                'score': '850',
                'time': '2:45',
            },
            token= registration_token,
            webpush = messaging.WebpushConfig(
                fcm_options = messaging.WebpushFCMOptions(
                    link = pushlink
                )
            ),
            notification=messaging.Notification(
                title='New Event',
                body='You have a new event!'
            ),
        )
        
        # send a message to the device corresponding to the provided registration token
        response = messaging.send(message)
        print('Successfully sent message:', response) 
    
    return 'ok'