Ad

My Android App Can't Receive Fcm Message From Python

I'm building android app with firebase cloud messaging. My app can receive message from FCM console. However, it cannot receive from python, although the response is good. Could you give me some advice?

class fbMessaging():
    def __init__(self):
        cred = credentials.Certificate('./env/firebase.json')
        firebase_admin.initialize_app(cred)

    def send_to_device(self, text, token):
        message = messaging.Message(
            data = {
                'title': 'test',
                'body': text,
            },
            token = token,
        )
        response = messaging.send(message)
        return response

def main():
    fm = fbMessaging()
    res = fm.send_to_device('test', 'MY CORRECT TOKEN')
    print(res)

onMessageRecieved is here

    override fun onMessageReceived(message: RemoteMessage?) {
        val from = message!!.from
        val data = message.data

        Log.d(TAG, "from:" + from!!)
        Log.d(TAG, "data:$data")

    }

Printed response is below.

projects/match-XXXXX/messages/0:1554291593xxxxxx%43f99108f9xxxxxx

Ad

Answer

Using Firebase Cloud Messaging, you can send Notification payload or Data payload or both.

Notification Payload contains title - Notification Title body - Notification body

The key names fixed and can't be changed.

Data Payload, on the other hand, is simply a key-value pair and you can send any key name with string type as its value.

FCM Behavior:

Based on whether the app is in the foreground or background and the existence of Notification payload or Data payload or both, the FCM message is received by different components in the app.

Handling of FCM notification as per documentation,

  • Notification messages delivered when your app is in the background. In this case, the notification is delivered to the device’s system tray. A user tap on a notification opens the app launcher by default.

  • Messages with both notification and data payload, when received in the background. In this case, the notification is delivered to the device’s system tray, and the data payload is delivered in the extras of the intent of your launcher Activity.

This behavior has been explained clearly in the Receive Messages Section.

As you can see, if only in the case where Notification payload is sent in standalone you don't have to build Notification UI. Otherwise, you have create the Notification UI when onMessageReceived is called.

Using Python:

Notification Payload Example:

message = messaging.Message(
    notification=messaging.Notification(
        title='This is a Notification Title',
        body='This is a Notification Body',
    ),
    token=registration_token,
)

Data Payload Example:

message = messaging.Message(
    data={
        'score': '850',
        'time': '2:45',
    },
    token=registration_token,

Both:

message = messaging.Message(
    notification=messaging.Notification(
        title='This is a Notification Title',
        body='This is a Notification Body',
    ),
    data={
        'score': '850',
        'time': '2:45',
    },
    token=registration_token,
Ad
source: stackoverflow.com
Ad