How get Firebase Device Token like string?

323 views Asked by At

I want to send device Token information when sending membership information to server.

But how should I go because getToken is no longer in use?

I've been using this before:

params.put("deviceToken", FirebaseInstanceId.getInstance().getToken());

Easy and worked but getToken is deprecated

}, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }
    }) {

        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);
            params.put("deviceToken", deviceTokenID); <<<---- DEVICE ID TOKEN

            return params;
        }

    };

    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
2

There are 2 answers

2
Alex Mamo On

To solve this, you need to use a success listener like in the following lines of code:

FirebaseInstanceId.getInstance().getInstanceId()
    .addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String tokenId = instanceIdResult.getToken();
            //Do what you need to do with the token
        }
});
2
Anand On
 String deviceToken= FirebaseInstanceId.getInstance().getToken();
 params.put("deviceToken", deviceToken);