How to detect if token is expired or not registred

2019-06-24 07:41发布

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 ?

2条回答
孤傲高冷的网名
2楼-- · 2019-06-24 08:24

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"}
                  }
           }   
  }
查看更多
放我归山
3楼-- · 2019-06-24 08:46

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>
查看更多
登录 后发表回答