Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect if token is expired or not registred firebase FCM notification on app server?

I am using following code to send the FCM notification from server to device :

    String fcmServerKey = externalConfig.getFcmServerKey();
            CloseableHttpClient httpclient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(HTTPS_FCM_GOOGLEAPIS_COM_FCM_SEND);

            httpPost.setEntity(new StringEntity(message, ContentType.create("application/json")));
            httpPost.setHeader("Authorization", "key=" + fcmServerKey);

CloseableHttpResponse closeableHttpResponse= httpclient.execute(httpPost);

In above code when I get the response object closeableHttpResponse, how can I detect wether the fcm token used to send this request is expired or not registered ?

When from firebase application dashboard I try sending the notification to a device using its fcm token and After application is removed from device, I see Failed on firebase dashboard, on hovering cursor on Failed I see Unregistered registration token.

How can I detect above error situation of Unregistered registration token from api response object closeableHttpResponse ?

like image 739
Prashant Avatar asked May 10 '17 10:05

Prashant


People also ask

How do you check FCM token is expired or not?

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.

How long FCM Token expired?

The message expires The maximum time that FCM will retry is 28 days, which is the default value.

Does FCM token change on app update?

FCM token for an app may change due to various reasons that are specified in the Firebase documentation.


1 Answers

Use the Server Reference API to get the associated information about the device registration token. If the response is empty, it means that the token is expired or not registered.

Example GET request

https://iid.googleapis.com/iid/info/nKctODamlM4:...clJONHoA?details=true
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA

Example result

HTTP 200 OK
  {
    "application":"com.iid.example",
    "authorizedEntity":"123456782354",
    "platform":"Android",
    "attestStatus":"ROOTED",
    "appSigner":"1a2bc3d4e5",
    "connectionType":"WIFI",
    "connectDate":"2015-05-12
    "rel":{
        "topics":{
          "topicname1":{"addDate":"2015-07-30"},
          "topicname2":{"addDate":"2015-07-30"},
          "topicname3":{"addDate":"2015-07-30"},
          "topicname4":{"addDate":"2015-07-30"}
                  }
           }   
  }
like image 178
looptheloop88 Avatar answered Sep 21 '22 01:09

looptheloop88