3

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());

1:

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?


2:

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.

Community
  • 1
  • 1
Ali Bdeir
  • 4,151
  • 10
  • 57
  • 117

1 Answers1

5

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.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • OK, wait. Doesn't each project have one SenderID? Isn't SenderID a unique name for your project? Similar to a URL? And what is my "App Server"? The RealtimeDatabase? Is it mandatory? – Ali Bdeir Nov 11 '16 at 16:23
  • And what is "notification key"? – Ali Bdeir Nov 11 '16 at 16:41
  • And what if the atomicinteger generated a key that isn't unique in the database? (It doesn't depend on the database to be unique after all) – Ali Bdeir Nov 11 '16 at 16:43
  • @AbAppletic Yes, each project has a unique Sender ID. Nope. It's not a URL, it a [numerical value](https://firebase.google.com/docs/cloud-messaging/concept-options#senderid). [App Server](https://firebase.google.com/docs/cloud-messaging/server) is mandatory if you're planning to implement Upstream messaging. The `notification_key` is for Device Group Messaging. Well, imagine querying for a message on your own database and receiving multiple results. The message ID depends entirely up to you anyways, so what value you use and how you use it is your choice. – AL. Nov 12 '16 at 03:37
  • If I store the messageID in the database, and I increment it everyone I send a message, would that sound right? (I'm sending to specific devices) – Ali Bdeir Nov 12 '16 at 04:10
  • And can you give me an example of when I use the addData method? When do I retrieve it? – Ali Bdeir Nov 12 '16 at 04:11
  • @AbAppletic That would be okay. So long as you make each id unique. You're sending to specific devices.?? Are you supposed to be implementing Downstream messaging? The sample included in your post and in the docs is the simplest it can get. *When do I retrieve it?* -- The reason you're implementing Upstream is because you have plans to use the data from your client app, right? Not sure what you mean by this question. – AL. Nov 12 '16 at 04:54
  • Yes, to specific devices. And I didn't fully understand when you use the `addData()` method. All I want to do is allow a user, USER1, to invite another user, USER2. And when USER1 invites USER2, he sends a notfication to USER2 informing him he got a new invitation. – Ali Bdeir Nov 12 '16 at 17:13
  • Let's saying I want so send "You've gotten a new invitation". what parameters, with the least possible, should I add with `addData`? – Ali Bdeir Nov 12 '16 at 19:50
  • Nevermind. Turns out I can't send downstream messages to specific devices. *Sigh...* Google always has caches in their features... – Ali Bdeir Nov 13 '16 at 15:05
  • @AbAppletic Each registered device has it's own unique registration token. So you *can* send to specific devices. It's the usual use case for push notifications. I'm not sure why you're saying you can't tho. Nonetheless, if you think my post was able to answer your initial questions, do mark it as accepted so that your post will be properly tagged. – AL. Nov 13 '16 at 15:28
  • Thanks. But can you mention how to send to a specific user using the Android client? They say I can send it using the console but not on the client side. – Ali Bdeir Nov 13 '16 at 15:29
  • Usually, I'd recommend to look around for similar posts or ask another question if there are further follow-ups to avoid lengthy comments section. But I'll go ahead on here for this one. The scenario you want is to send a message from device to device, if I understand correctly. The answer of @DiegoGiorgini [here](http://stackoverflow.com/a/39279716/4625829) should give you a brief idea. Simply on the part of having a server. Or you can make use of the realtime database like in the idea of this [post](http://stackoverflow.com/a/40401062/4625829). – AL. Nov 13 '16 at 15:39