57

Working with the "new" Firebase Cloud Messaging, I would like to reliably save client device registration_id tokens to the local server database so that the server software can send them push notifications.

What is the smallest size of database field that I should use to save 100% of client registration tokens generated?

I have found two different libraries that use TextField and VarChar(255) but nothing categorically defining the max length. In addition, I would like the server code to do a quick length check when receiving tokens to ensure they "look" right - what would be a good min length and set of characters to check for?

jamesc
  • 5,852
  • 3
  • 32
  • 42

4 Answers4

38

I think this part of FCM is still the same as GCM. Therefore, you should refer to this answer by @TrevorJohns:

The documentation doesn't specify any pattern, therefore any valid string is allowed. The format may change in the future; please do not validate this input against any pattern, as this may cause your app to break if this happens.

As with the "registration_id" field, the upper bound on size is the max size for a cookie, which is 4K (4096 bytes).

Emphasizing on the The format may change in the future part, I would suggest to stay safe and have a beyond the usual max (mentioned above) length. Since the format and length of a registration token may also vary.

For the usual length and characters, you can refer to these two answers the latter being much more definitive:

I hasn't seen any official information about format of GCM registrationId, but I've analyzed our database of such IDs and can make following conclusions:

  • in most cases length of a registrationID equals 162 symbols, but can be variations to 119 symbols, maybe other lengths too;
  • it consists only from this chars: [0-9a-zA-Z\-\_]*
  • every regID contains one or both of "delimiters": - (minus) or _ (underline)
Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • 7
    We're also getting `:` in our tokens. The documentation https://firebase.google.com/docs/cloud-messaging/server shows an example token as `bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...` which definitely has `:`. – jamesc Oct 14 '16 at 11:33
  • 1
    @jamesc I've also seen `:` in tokens before. I think it's valid delimiter character. – AL. Oct 14 '16 at 11:48
  • @jamesc I commonly see the `:` for registration tokens for Chrome Client app. But I guess it's still possible for even Android and iOS regardless. Anyways, I think my answer above pretty much gives you an idea and covers your inquiries. If you agree, please accept it as the correct answer so it will be tagged as Answered. Cheers! – AL. Oct 15 '16 at 14:55
  • Haven't forgotten about this - I'll accept the answer once my code goes live :D – jamesc Oct 27 '16 at 10:16
  • @jamesc - No worries. Good luck with your app. :) – AL. Oct 27 '16 at 11:08
  • me too, 174, just FYI – behelit Nov 15 '18 at 23:36
  • I know this is old but regarding the colon (:) it seems to delimiter the instance id bound to the fcm project, though I haven't found any document on this. – Juan Apr 13 '19 at 20:17
  • The part before the colon is the instance-id (https://firebase.google.com/docs/reference/android/com/google/firebase/iid/FirebaseInstanceId.html), i.e. it identifies the device, not the project. – Tim Riemenschneider Jun 28 '19 at 13:40
21

I am now using Firebase Cloud Messaging instead of GCM.

The length of the registration_id I've got is 152.

I've also got ":" at the very beginning each time like what jamesc mentioned (e.g. bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1).

I make the token as varchar(255) which is working for me.

However, the length of registration_id has no relationship with size of 4k. You are allowed to send whatever size of the data through network. Usually, cookies are limited to 4096 bytes, which consist of name, value, expiry date etc.

Fred Lai
  • 1,131
  • 10
  • 21
2

This is a real fcm token:

c2aK9KHmw8E:APA91bF7MY9bNnvGAXgbHN58lyDxc9KnuXNXwsqUs4uV4GyeF06HM1hMm-etu63S_4C-GnEtHAxJPJJC4H__VcIk90A69qQz65toFejxyncceg0_j5xwoFWvPQ5pzKo69rUnuCl1GSSv

as you can see the length of token is: 152

Abolfazl Miadian
  • 3,099
  • 2
  • 19
  • 15
0

I don't think the upper limit for a registration ID is 4K. It should be safe to assume that it is much lower than that.

The upper limit for a notification payload is 4KB (link), and the notification payload includes the token (link). Since the payload also needs to include the title, body, and other data too, the registration ID should be small.

That's what I understand from the docs ¯\_(ツ)_/¯

The last tokens I got were 163-chars long. I think it's safe to assume that they will never exceed 255 chars. Some comments in the other answer reported much higher lengths!

Update

So far, in 4 months that I'm running my app, there are over 100k registration IDs, and every single one of them is 163-chars long. It's very possible that Google maintains the ID length stable in order not to crash apps. Hence, I'd suggest

  1. getting a few registration IDs in your local machine
  2. measuring their length and verifying it's constant (or at least it doesn't change significantly)
  3. picking a safe initial value, slightly higher than the ID length

I think it's unlikely for the length to change now, but I'll keep an eye. Please let me know if you noticed IDs of different lengths in your apps!

Kostis
  • 1,593
  • 15
  • 16