Can't retrieve Firebase InstanceId Token after updating to newest library

Okay so after updating to the newest firebase messaging library, I can't seem to retrieve the token. I t said in the docs that it should not be run in the main thread but when I tried, it still didn't work. So I simply removed it. What could I be doing wrong?

Below is my try/catch code.

String device_token;
                    try {
                        device_token = FirebaseInstanceId.getInstance().getToken(R.string.sender_id, "FCM");

                        student_token_reference = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(uid);
                        student_token_reference.child("device_token").setValue(device_token).addOnSuccessListener(new OnSuccessListener<Void>() {
                            @Override
                            public void onSuccess(Void aVoid) {

                                DatabaseReference check_variablesRef = FirebaseDatabase.getInstance().getReference().child("MakeUpArtists_Info").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("Verification");
                                check_variablesRef.addValueEventListener(new ValueEventListener() {
                                    @Override
                                    public void onDataChange(DataSnapshot dataSnapshot) {

                                        String id_card = dataSnapshot.child("id_card").getValue().toString();
                                        String image = dataSnapshot.child("image").getValue().toString();

                                        if (id_card.equals("Not yet") && image.equals("Not yet")) {

                                            Toast.makeText(LoginActivity.this, "Please finish uploading your documents", Toast.LENGTH_SHORT).show();

                                            Intent id_card_intent = new Intent(LoginActivity.this, IDcard.class);
                                            startActivity(id_card_intent);
                                            finish();
                                        } else if (id_card.equals("Received") && image.equals("Received")) {
                                            Intent intentMain = new Intent(LoginActivity.this, MainActivity.class);
                                            intentMain.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                            startActivity(intentMain);
                                            finish();

                                        } else if (id_card.equals("Received") && image.equals("Not yet")) {

                                            Toast.makeText(LoginActivity.this, "Please upload your profile picture", Toast.LENGTH_SHORT).show();

                                            Intent success_intent = new Intent(LoginActivity.this, DisplayProfile.class);
                                            startActivity(success_intent);
                                            finish();
                                        }

                                    }

                                    @Override
                                    public void onCancelled(DatabaseError databaseError) {

                                    }
                                });
                            }

                        });
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

Any help would be greatly appreciated!

Upvotes: 0

Views: 1854

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38309

The Firebase Release Notes for the June 28 release recommend using FirebaseInstanceId.getInstance().getInstanceId(). Here is an example (as you requested in comments) of how to do that:

FirebaseInstanceId.getInstance().getInstanceId().addOnCompleteListener(
        new OnCompleteListener<InstanceIdResult>() {
    @Override
    public void onComplete(Task<InstanceIdResult> task) {
        if (task.isSuccessful()) {
            final InstanceIdResult iidResult = task.getResult();
            final String token = iidResult.getToken();
            Log.d(TAG, "token=" + token);
            // process token as you need...
        } else {
            Log.e(TAG, "get IID/token failed", task.getException());
        }
    }
});

Upvotes: 1

Related Questions