0
votes

I'm trying to implement a notification system in which you can subscribe to another user (not Firebase topics) and get push notifications whenever that user posts something.

To do this, I decided to use Device Groups and registration tokens so that the user can get push notifications to every device he/she is logged into.

Currently, the user can subscribe to another user and is also able to receive push notifications to every device he's logged into using the notification key. To do this, I get the registration token for the device every time the user logs in and store it in the database (App server) using the following structure.

User

  • user_id (PK)
  • notification_key_name
  • notification_key

Device

  • user_id (FK)
  • registration_token

But, I'm having trouble figuring out how to manage token refreshes in the App Server.

Consider this case:

1) user_1 logs in for the first time in device.

2) A device group is created with registration_token_1 (both in FCM and in App Server).

3) user_1 uninstalls the app and reinstalls it (token is refreshed).

4) user_1 logs back in.

5) App Server adds refresh_token_1 to user_1, he now has 2 tokens, the old and the new.

6) user_1 logs out and user_2 logs in the same device, refresh_token_1 is transferred to user_2.

7) FCM deletes user_1's device group since user_1 no longer has any registration tokens. But user_1 still has notification_key and a device with registration_token_1 in the App Server.

8) user_1 is unable to login since the App Server thinks he already has a device group and tries to add refresh_token_1 to a notification_key that doesn't exist in FCM, getting error code 400 "notification key does not exist".

I have a couple questions regarding this:

If a token is refreshed, is there a way to retrieve the old token from FCM? This way the App Server can replace the old token with the new one without having to pair the token to a device identifier, which seems unreliable if the identifier does not live through uninstalls/factory resets, etc

Can you recommend a better way for mapping users (notification keys) with registration tokens in the App Server?

EDIT I've read around some other threads and found a possible solution in the form of pairing registration tokens to a device identifier, so when the token is refreshed, I can look for the device identifier and replace/remove the token. Now, I'm not sure BOTH iOS and Android have persistent identifiers that can exist beyond app uninstalls, data wipes and factory resets...

1

1 Answers

4
votes

So, after a bit of research I finally decided to give up on device groups. They are not worth the pain for the purposes of my application. I decided to use only registration tokens, pair them with user ID's and skip device groups entirely.

There is no way, using Firebase API alone, to retrieve the old token once the token is refreshed, managing tokens and devices is the developer's responsibility. Firebase can, in fact, return a list of invalid registration tokens once a notification has been sent, so that can be used to delete invalid tokens in the back-end.

I could not figure out any other way of keeping track of old tokens besides pairing them with unique device identifiers, only then can the back-end really know which token to update once it is refreshed. Keeping device identifiers in the database seems a bit hacky to me, and I am pretty sure iOS does not provide any kind of persistent identifier anymore, so that's a no go.

Keeping track of registration tokens when logging in is pretty straight forward to implement and easier to manage in the back-end. As for sending the notifications, they're split into groups of 1000 and then sent to the appropriate user. Once notifications have been sent, invalid tokens are then deleted from the database.