8

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 ?

KENdi
  • 7,576
  • 2
  • 16
  • 31
Prashant
  • 4,474
  • 8
  • 34
  • 82
  • Have you seen the sample code in [this answer](http://stackoverflow.com/a/31659073/4625829)? – AL. May 11 '17 at 03:54

2 Answers2

4

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"}
                  }
           }   
  }
looptheloop88
  • 3,026
  • 17
  • 20
0

create this class in your package

import android.util.Log;

import com.google.firebase.iid.FirebaseInstanceId;
import com.google.firebase.iid.FirebaseInstanceIdService;
import com.google.gson.Gson;



public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";


    @Override
    public void onTokenRefresh() {

        //Getting registration token
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        //Displaying token on logcat
        Log.d(TAG, "Refreshed token: " + refreshedToken);
        sendRegistrationToServer(refreshedToken);



    }

    private void sendRegistrationToServer(String token) {

        Context context = getApplicationContext(); 
        //You can implement this method to store the token on your server
        //if token is already inserted then update token in your server

    }


}

and add this in your AndroidManifest.xml in

<application>
        ..............
        <service
            android:name=".Listeners.MyFirebaseInstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
            </intent-filter>
        </service>

</application>
Bhupat Bheda
  • 1,968
  • 1
  • 8
  • 13
  • 1
    FirebaseInstanceId.getInstance().getToken() use this code to get FCM ID instantly – Harsh Patel May 10 '17 at 10:38
  • 3
    Hi Babul thanks, my problem is not how to detect new token, how to detect expired or unregistered token on server – Prashant May 10 '17 at 11:13
  • 1
    As the OP mentioned, this answer is not related with the solution. – Lashae Mar 27 '18 at 10:31
  • 3
    While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value. – Nic3500 Oct 09 '18 at 04:34