FirebaseInstaceId、InstanceIdResult 已弃用

FirebaseInstaceId, InstanceIdResult deprecated

由于 FirebaseInstanceId 和 InstanceIdResult 已弃用,我该如何更改此代码?它还从代码中声明 'cannot resolve method getToken()'。

FirebaseInstanceId.getInstance().getId().addOnSuccessListener(new OnSuccessListener<InstanceIdResult>() {
        @Override
        public void onSuccess(InstanceIdResult instanceIdResult) {
            String token = instanceIdResult.getToken();
            if (!token.equals(session.getData(Session.KEY_FCM_ID))) {
                UpdateToken(token, MainActivity.this);
            }
        }
    });

这不是您获取令牌的方式。所有这些方法都已弃用。如今,解决方案是:

FirebaseMessaging.getInstance().getToken().addOnCompleteListener(new OnCompleteListener<String>() {
    @Override
    public void onComplete(@NonNull Task<String> task) {
        if (task.isSuccessful()) {
            String token = task.getResult();
            //Do what you need to do with your token
            Log.d(TAG, token);
        }
    }
});