0

I have managed to do an implementation that allows me to create a device group, add and remove registration ids from it and send messages to it, but now I have come to realize I don't understand when should I be calling this methods.

What I initially thought was

  • Create a device group when a user registers to the app
  • Add a registration id to the device group on user login
  • Remove the registration id I added on login when the user logs out

The problem with this is that if the user was logged to the app in only one device and he logs out then when I remove the registration id the device group will be deleted. This means I won't be able to notify the user of anything until he login again. But this shouldn't be how it works, for example in Facebook even if you are not logged once you enter you get the old notifications.

But the problem if I don't remove the registration ids on logout is that two device groups can have the same registration is inside. When my app gets installed in a phone (in the case Android) it generates a registration id, so far for what I could debug that is the only time it generates one. This means that if a user logs in, logs out and then a new user logs in I could have add that registration id to both users device groups. Does this mean that they will both receive notifications?

I can force to generate a new token when on an user login by doing what is explained here, but I guess there has to be something better than this work-around.

Plus, at some point I need to remove registration ids, otherwise I will reach the 20 limit.

So my question is what is the correct (or a correct) life-cycle for device groups that takes into account the problems I have described and any other I haven't find out yet. Thanks!

Extra question: if I send a notification to a device group and then, before receiving the notification, the user logs in in a new device and I add its registration id to the device group, will it receive the notification? I guess he wouldn't.

moondaisy
  • 4,303
  • 6
  • 41
  • 70

1 Answers1

2

I think there's a slight confusion on how device groups should be used here.

First off, if I understand your flow correctly, as soon as a user installs and registers with your app, you immediately create a corresponding device group and add the user's registration token. This, in a sense is a bit too much use of it.

In general (and as advised), Device Group Messaging is used for in a case where a single user has multiple devices (this part I'm sure you got), but immediately creating one wouldn't be good.

The flow in my mind on how Device Group Messaging should be used is something like:

  1. User installs and registers in the app. Credentials with a unique id is included. You save the corresponding registration token under the same user.
  2. User logs-in to a different device, you detect the registration token of that device, if different, save (if you have a registration_tokens node or something) then generate a notification_key (device group), then use it accordingly.
  3. If user logs out, handle it accordingly. Also handle the device group mapping (see my answers here and here).

Then, every time you send a message, verify if the user only has a single registration token, if yes, then use that specific one directly.

Answer to extra question: Nope. (but haven't tested it out so I can't be so sure) The way I understand the flow of an FCM message to a device group, is that after you send the message:

  1. Message(s) sent to FCM servers.
  2. Message(s) are then queued to the corresponding devices in device group. Let's say device1 and device2.
  3. User logs-in to device3. It won't receive the message(s) it might already been lined up.

With all that said, this could be tested out further if you want to.

AL.
  • 36,815
  • 10
  • 142
  • 281