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

ぐ巨炮叔叔 提交于 2019-12-12 11:17:11

问题


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 ?


回答1:


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"}
                  }
           }   
  }



回答2:


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>


来源:https://stackoverflow.com/questions/43889697/how-to-detect-if-token-is-expired-or-not-registred-firebase-fcm-notification-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!