Reputation: 4365
The example provided by Firebase is:
Your Android app can send an upstream message using
FirebaseMessaging.send
:FirebaseMessaging fm = FirebaseMessaging.getInstance(); fm.send(new RemoteMessage.Builder(SENDER_ID + "@gcm.googleapis.com") .setMessageId(Integer.toString(msgId.incrementAndGet())) .addData("my_message", "Hello World") .addData("my_action","SAY_HELLO") .build());
They've explained what the Message ID is:
A message ID that should be unique for each sender ID.
However, I didn't get what they mean in precise. So, every time I send a message, this number gets incremented? For what reason? And where do I store this value?
They also have the method addData()
, and I don't fully understand this what this does. A full, detailed explanation on this method will be accepted.
Upvotes: 5
Views: 9226
Reputation: 37798
1:
The message id (as the name implies) is the identifier of the message you sent from your device to be used to differenciate each message from one another. They also just mentioned that each message should be unique per each Sender ID.
For example, you have 2 Sender IDs, each Sender ID can both have a message id with the value of 1
, but that message id should not be repeated within the same Sender ID.
Technically, your implementation is incrementing the value of the message id before sending the message. It's not visible in the sample code for a simple upstream message, but the data type used for the msgId
variable is an AtomicInteger
as seen in the docs for Sending upstream messages to device groups:
FirebaseMessaging fm = FirebaseMessaging.getInstance();
String to = aUniqueKey; // the notification key
AtomicInteger msgId = new AtomicInteger();
fm.send(new RemoteMessage.Builder(to)
.setMessageId(msgId)
.addData("hello", "world")
.build());
The incrementAndGet()
makes sure that the value of the AtomicInteger is incremented before use, making it different (unique) each time it is called. (see this post for more ideas on the usage of an AtomicInteger)
Where you store it is the part I'm not entirely sure. The way I see it, since the message is intended to be sent towards your App Server, you should be storing it there once you receive it.
2:
The addData()
is where you include the key-value pair(s) of the message you intend to send. The details, contents, or whatever you intend to send towards your App Server.
Think of it as the same as the data
payload for downstream messaging.
Upvotes: 5