Firebase Messaging in flutter sendMessage()

1,348

Unfortunately this method does not do what you might think it does: it is not for sending messages from one device directly to another device through FCM, but rather an alternative protocol that is infrequently used these days (afaik).

The FlutterFire documentation describes the sendMessage method as:

Send a new RemoteMessage to the FCM server. Android only.

Unfortunately that is missing a few crucial details, so I recommend also checking out the Android documentation on the Firebase site, where this is covered under sending upstream messages, which starts with:

If your app server implements the XMPP Connection Server protocol, it can receive upstream messages from a user's device to the cloud.

Still a bit vague, but keep reading it also mentions:

Receive XMPP messages on the app server

When FCM receives an upstream messaging call from a client app, it generates the necessary XMPP stanza for sending the upstream message. FCM adds the category and from fields, and then sends a stanza like the following to the app server:

The key here is the mention of an app server. As many places explain: there is no way to directly send messages from one device to another, as doing so would allow any user to send any message they want to any other user, which is a security risk.

The sendMessage method you found allows you to send a message to the FCM infrastructure, which then forwards it to your app server. It isn't much used anymore these days, as tools like Cloud Functions offer (at least equivalent and probably better) scalability with better flexibility.

Share:
1,348
Youcef
Author by

Youcef

Updated on December 30, 2022

Comments

  • Youcef
    Youcef over 1 year

    I am a flutter programmer and I use in my projects when it comes to pushing notifications Firebase cloud messaging using node.js I just discovered that there is a method called sendMessage in the firebase_Messaging package in flutter but I looked every where for documentation and I tried everything to find out how it works but got nothing.

    That's all what the package offers as information:

    1

    so if anyone used it before or can help me to understand it I would really appreciate it <3