Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I identify and delete the expired FCM token on server?

I'm considering a scenario where a user installs the app on multiple devices say mobile and desktop; and generates two different tokens.

My current plan is to store both the tokens for the user in database. However, I'm not sure how do I handle the scenario when the token is expired and a new token is generated?

For example, this could be the simple db structure -

id | user_id | token
-------------------------
1  | 1 | asdlgkj090sdf8q (desktop)
2  | 1 | zlkroqiuoiquoio (mobile)
3  | 2 | mnnjnjnjnjojuhq
4  | 2 | 498slkdsflksjfl

If the token for mobile gets updated; I've no way of knowing which row to update.

How do I handle such scenario?

like image 571
TheBigK Avatar asked Sep 09 '19 09:09

TheBigK


People also ask

How can I check my FCM token?

Check the format of the registration token you pass to the server. Make sure it matches the registration token the client app receives from registering with Firebase Notifications. Do not truncate or add additional characters. Notice that I added in "ABC" , in the registration_ids parameter.

Does FCM Token expired?

It doesn't expire though.

Where can I find FCM server key?

Click the Cloud Messaging tab next to the General tab. The Project credentials section appears. The Project credentials section displays the Firebase Cloud Messaging token, Sender ID, and the Server key.


1 Answers

If you want to remove the old token after a new one was generated, I can think of two ways of doing this:

Option 1: Introduce a client id

You could give each client a unique ID that you use to identify it. When you send the new push token to the server, you need to make sure that you also pass the client ID to the server. Your database structure would then look something like that:

id | user_id | client_id | token
------------------------------------------------------ 
1  | 1       | tthdh     | asdlgkj090sdf8q (desktop) 
2  | 1       | di4dq     | zlkroqiuoiquoio (mobile) 
3  | 2       | 5efgd     | mnnjnjnjnjojuhq 
4  | 2       | 56eff     | 498slkdsflksjfl

In your update script (on the server) you will check if a push token exists for the given client id and then either replace the old one or insert a new row.

Option 2: Remember the old token

The second option would be to store the current token on the client, and if it is updated you send both the old and the new token to the server. Then you search for the old token and replace it (or insert a new row if the old token is not present).

If you want to find out if a given token is expired, check this answer. Another approach that I've seen in the Firebase documentation was to remove the token that was used to send a push message if sending the message failed.

like image 78
Markus Penguin Avatar answered Sep 30 '22 00:09

Markus Penguin