Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase Cloud Messaging - How to validate Tokens?

I am using Firebase Cloud Messaging (FCM) and as per the abreviated code below everytime a new Token is generated on the Customer Device... I send this new TOKEN to my SERVER DB (Cloud) where I save it in order to be able to send future Push Notification from the Server to the Device using the CFM API.

    //public class CFMInstanceIDService extends FirebaseInstanceIdService ...      public void onTokenRefresh() {         ...         String cfmToken = FirebaseInstanceId.getInstance().getToken();                 ...              sendRegistrationToServer(customerGuid, cfmToken);     } 

By doing this I have on the Server a list of ALL (multiples) Devices where a Customer is logged-in. (Tablet, Phone, iPhone, Android, etc)

Is there any way to verify/validate a Token at any time?

I would like to know/ensure that all the tokens that I have associated to a Customer belong to real Devices. I don't want to send Push Notifications to not-existing Tokens.

like image 240
Geo305 Avatar asked Jul 18 '16 17:07

Geo305


People also ask

How to check if a Firebase token is valid?

Verify ID tokens using the Firebase Admin SDK If the provided ID token has the correct format, is not expired, and is properly signed, the method returns the decoded ID token. You can grab the uid of the user or device from the decoded token. Note: This does not check whether or not the token has been revoked.

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.

Do Firebase FCM tokens expire?

It doesn't expire though. It renews itself if one of the following happens. According to https://firebase.google.com/docs/cloud-messaging/android/client: -The app deletes Instance ID.


2 Answers

Here is an example curl request that shows how to validate a token without actually having to send a message:

curl -H "Content-Type: application/json" -H "Authorization: key=$FCM_API_KEY" https://fcm.googleapis.com/fcm/send -d '{"registration_ids":["$FCMTOKEN"]}' 

Example invalid response:

{"multicast_id":7452350602151058088,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"InvalidRegistration"}]} 

Example valid response:

{"multicast_id":9133870199216310277,"success":1,"failure":0,"canonical_ids":0,"results":[{"message_id":"0:1502817580237626%f590ddc2f9fd7ecd"}]} 

I got this answer from google's firebase support team.

like image 126
AngularNerd Avatar answered Sep 20 '22 05:09

AngularNerd


Actually there is a workaround, you can use dry_run = true

This parameter, when set to true, allows developers to test a request without actually sending a message.

firebase docs

if user unsubscribe, you have a response with NotRegistered but real sending won't be performed

like image 37
FDG Avatar answered Sep 18 '22 05:09

FDG