我想在我的应用中使用FCM,但是在添加了最新的firebase-messaging依赖项之后:
implementation 'com.google.firebase:firebase-messaging:18.0.0'
构建项目时,在编译器中出现以下错误:
error: cannot find symbol class GetTokenResult
该错误来自于我用来捕获用户令牌的 FirebaseUserInterceptor ,它工作得很好。但是,在添加了消息传递依赖性之后,“ GetTokenResult”和“ getToken()”都变得无法识别,因此引发了上述错误。
代码:
import android.util.Log;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.auth.GetTokenResult;
import java.io.IOException;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;
public class FirebaseUserInterceptor implements Interceptor {
private static final String X_FIREBASE_ID_TOKEN = "firebaseUserId";
private static final String TAG = "FirebaseUserInterceptor";
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
try {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user == null) {
Log.d(TAG, "intercept: not logged in");
throw new Exception("User not logged in");
} else {
Task<GetTokenResult> task = user.getIdToken(true);
GetTokenResult tokenResult = Tasks.await(task);
String idToken = tokenResult.getToken();
Log.d(TAG, "intercept: token=" + idToken);
if (idToken == null) {
Log.d(TAG, "intercept: idToken null");
throw new Exception("idToken null");
} else {
Request modifiedRequest = request.newBuilder()
.addHeader(X_FIREBASE_ID_TOKEN, idToken)
.build();
return chain.proceed(modifiedRequest);
}
}
} catch (Exception e) {
throw new IOException(e.getMessage());
}
}
}
Gradle(应用):
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.app_v1"
minSdkVersion 21
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
configurations {
all*.exclude group: 'com.google.guava', module: 'listenablefuture'
}
buildTypes {
customDebugType {
debuggable true
}
debug {
testCoverageEnabled = false
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'com.google.android.material:material:1.0.0'
implementation 'androidx.preference:preference:1.1.0-alpha05'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test:runner:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
//Firebase
implementation 'com.google.firebase:firebase-core:16.0.8'
implementation 'com.google.firebase:firebase-auth:16.2.1'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
//Retrofit
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
//Gson
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
//OkHttpLoggingInterceptor
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
//OkHttp
implementation 'com.squareup.okhttp3:okhttp:3.4.1'
//GraphView
implementation 'com.jjoe64:graphview:4.2.2'
//Room
implementation 'android.arch.persistence.room:runtime:1.1.1';
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1';
//TimeSquare date and date range picker
implementation 'com.squareup:android-times-square:1.6.5@aar'
implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
annotationProcessor 'androidx.lifecycle:lifecycle-compiler:2.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
implementation 'androidx.recyclerview:recyclerview:1.0.0'
implementation 'androidx.lifecycle:lifecycle-livedata:2.0.0'
implementation 'androidx.room:room-runtime:2.0.0'
annotationProcessor 'androidx.room:room-compiler:2.0.0'
}
apply plugin: 'com.google.gms.google-services'
Gradle(项目):
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.4.0'
classpath 'com.google.gms:google-services:4.2.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
我正在使用Android Studio(3.4版)
答案 0 :(得分:1)
更新以下依赖项:
public class Entry {
//declaring variables
private String month;
private String day;
private String year;
private float powerOutput;
private String file;
private int[] MonthList;
String inputFile = file;
//Constructors
public Entry() {
}
public Entry(String m, String d, String y, float p) {
month = m;
day = d;
year = y;
powerOutput = p;
}
//creating print to call ArrayList in main
public void print() {
System.out.println("Month: " + month + " Day: " + day + " Year: " + year + " Power Output: " + powerOutput);
}
public static void getFile() {
}
public String getMonth() {
return month;
}
public String getDay() {
return day;
}
public String getYear() {
return year;
}
public float getPowerOutput() {
return powerOutput;
}
public int[] getMonthList() {
return MonthList;
}
public String getInputFile() {
return inputFile;
}
}
对此:
implementation 'com.google.firebase:firebase-auth:16.2.1'
来自docs:
此更新的重大更改:
如果您使用Firebase身份验证,请更新至firebase-auth v17.0.0或更高版本,以确保功能与其他更新的Firebase库保持一致。