Send upstream messages from a Unity client app

If your app server implements the XMPP Connection Server protocol, it can receive upstream messages from a user's device to the cloud. To initiate an upstream message, the client app sends a request containing the following:

  • The address of the receiving app server in the format SENDER_ID@fcm.googleapis.com.
  • A message ID that should be unique for each sender ID.
  • The message data comprising the key-value pairs of the message's payload.

When it receives this data, FCM builds an XMPP stanza to send to the app server, adding some additional information about the sending device and app.

Send an upstream message from a Unity client app

To send messages upstream to the server, a Unity client app composes a message and calls Send as shown:

Firebase.Messaging.Message message;
message.To = SENDER_ID + "@fcm.googleapis.com";
message.MessageId = get_unique_message_id();
message.Data("my_message", "Hello World");
message.Data("my_action", "SAY HELLO");
message.TimeToLive = kTimetoLive;
Firebase.Messaging.FirebaseMessaging.Send(message);

where:

  • message.To is the address of the receiving app server in the format SENDER_ID@fcm.googleapis.com.

  • message.MessageId is a unique message identifier. All the message receiver callbacks are identified on the basis of this message ID.

  • message.Data is a map of keys and values as strings.

  • message.TimeToLive specifies how long (in seconds) the message should be kept in FCM storage if the device is offline. If FCM can't deliver the message before this expiration is reached, it sends a notification back to the client.

The FCM client library caches the message on the client app and sends it when the client has an active server connection. On receiving the message, FCM sends it to the app server.

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:

<message id="">
  <gcm xmlns="google:mobile:data">
  {
      "category":"com.example.yourapp", // to know which app sent it
      "data":
      {
          "hello":"world",
      },
      "message_id":"m-123",
      "from":"REGID"
  }
  </gcm>
</message>

Sending an ACK message

In response to an upstream message like the above, the app server must use the same connection to send an ACK message containing the unique message ID. If FCM does not receive an ACK, it may retry sending the message to the app server.

<message id="">
  <gcm xmlns="google:mobile:data">
  {
      "to":"REGID",
      "message_id":"m-123"
      "message_type":"ack"
  }
  </gcm>
</message>

See the XMPP Connection Server Reference for more information about upstream message syntax.