Home > database >  Chat app - update token chat fragment - new version
Chat app - update token chat fragment - new version

Time:04-15

I'm creating a chat app and I imported this 3-year-old project so a lot of things have changed, especially this thing that I can't fix could anyone help me? Thanks.

The error that gives me:

'updateToken(java.lang.String)' in 'com.koddev.chatapp.Fragments.ChatsFragment' cannot be applied to '(com.google.android.gms.tasks.Task<java.lang.String>)'

public class ChatsFragment extends Fragment {

private RecyclerView recyclerView;

private UserAdapter userAdapter;
private List<User> mUsers;

FirebaseUser fuser;
DatabaseReference reference;

private List<Chatlist> usersList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_chats, container, false);

    recyclerView = view.findViewById(R.id.recycler_view);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

    fuser = FirebaseAuth.getInstance().getCurrentUser();

    usersList = new ArrayList<>();

    reference = FirebaseDatabase.getInstance().getReference("Chatlist").child(fuser.getUid());
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            usersList.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                Chatlist chatlist = snapshot.getValue(Chatlist.class);
                usersList.add(chatlist);
            }

            chatList();
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

    updateToken(FirebaseMessaging.getInstance().getToken());


    return view;
}

private void updateToken(String token){
    DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Tokens");
    Token token1 = new Token(token);
    reference.child(fuser.getUid()).setValue(token1);
}

private void chatList() {
    mUsers = new ArrayList<>();
    reference = FirebaseDatabase.getInstance().getReference("Users");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            mUsers.clear();
            for (DataSnapshot snapshot : dataSnapshot.getChildren()){
                User user = snapshot.getValue(User.class);
                for (Chatlist chatlist : usersList){
                    if (user.getId().equals(chatlist.getId())){
                        mUsers.add(user);
                    }
                }
            }
            userAdapter = new UserAdapter(getContext(), mUsers, true);
            recyclerView.setAdapter(userAdapter);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
}

CodePudding user response:

You're getting the following error:

'updateToken(java.lang.String)' in 'com.koddev.chatapp.Fragments.ChatsFragment' cannot be applied to '(com.google.android.gms.tasks.Task<java.lang.String>)'

Because you're calling the updateToken() method with a wrong argument. You defined the method to take a String argument, while you're calling it with a Task object, hence the error. If you want to get the token and call the method correctly, please use the following lines of code:

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
    @Override
    public void onComplete(@NonNull Task<String> task) {
        if (task.isSuccessful()) {
            String token = task.getResult();
            updateToken(token);
            Log.d("TAG", token); //Check the generated token.
        } else {
            Log.d("TAG", task.getException().getMessage()); //Never ignore potential errors!
        }
    }
});
  • Related