我试图通过在我的FirebaseInstanceId.getInstance().getToken()
中使用MainActivity.java
来获取令牌,但是当我使用此方法获取令牌时,应用程序星级崩溃了。
这是我的onCreate()方法
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(MainActivity.this, FirebaseInstanceId.getInstance().getToken(), Toast.LENGTH_SHORT).show();
}
Build.gradle(项目级别)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.3'
classpath 'com.google.gms:google-services:3.1.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
Build.gradle(应用程序级别)
apply plugin: 'com.android.application'
android {
compileSdkVersion 25
buildToolsVersion "25.0.3"
defaultConfig {
applicationId "com.simple.app"
minSdkVersion 21
targetSdkVersion 25
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile ('com.android.support:appcompat-v7:25.0.1')
{
force=true
}
compile ('com.android.support.constraint:constraint-layout:1.0.2')
{
force=true
}
compile ('com.android.support:design:25.0.1')
{
force=true
}
compile 'com.firebaseui:firebase-ui:0.6.2'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.theartofdev.edmodo:android-image-cropper:2.4.+'
compile ('com.android.support:support-v4:25.3.1')
{
force=true
}
compile ('com.google.firebase:firebase-messaging:9.2.0')
{
force=true
}
testCompile 'junit:junit:4.12'}
apply plugin: 'com.google.gms.google-services'
Logcat
07-25 20:25:20.903 4563-4563/com.simple.app D/AndroidRuntime: Shutting down VM
07-25 20:25:20.907 4563-4563/com.simple.app E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.simple.app, PID: 4563
java.lang.NoSuchMethodError: No static method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Lcom/google/android/gms/common/util/zzx; or its super classes (declaration of 'com.google.android.gms.common.util.zzx' appears in /data/app/com.simple.app-2/split_lib_dependencies_apk.apk:classes42.dex)
at com.google.firebase.iid.zzg.zzkg(Unknown Source)
at com.google.firebase.iid.zzg.<init>(Unknown Source)
at com.google.firebase.iid.zzg.<init>(Unknown Source)
at com.google.firebase.iid.zzd.zzb(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
at com.google.firebase.iid.FirebaseInstanceId.getInstance(Unknown Source)
at com.simple.app.MainActivity.onCreate(MainActivity.java:138)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
最佳答案
java.lang.NoSuchMethodError:没有静态方法
getNoBackupFilesDir(Landroid / content / Context;)Ljava / io / File;在班上
Lcom / google / android / gms / common / util / zzx;
费耶getNoBackupFilesDir
中存在ASAIK android.support.v4.content.ContextCompat.
从build.gradle依赖项部分中删除support-v4:25.3.1
。
然后Clean-Rebuild and Run
。
如果使用最新版本会更好。
android {
// ...
compileSdkVersion 26
buildToolsVersion '26.0.1'
defaultConfig {
targetSdkVersion 26
}
// ...
}
加
repositories {
maven { url 'https://maven.google.com' }
}
和
dependencies {
// ...
compile 'com.android.support:appcompat-v7:26.1.0'
compile 'com.android.support:support-compat:26.1.0'
compile 'com.google.firebase:firebase-messaging:11.4.2'
// ...
}
注意
FirebaseInstanceId.getInstance().getToken()
调用以检索当前令牌。如果尚未创建令牌,则为null回到。
监控令牌生成
正如有关
Set up a Firebase cloud messaging client app on Android.
的官方指南所述:“
onTokenRefreshcallback
每当生成新令牌时都会触发,因此在其上下文中调用getToken
可确保您正在访问当前可用的注册令牌。请确保已将服务添加到manifest中,然后调用在getToken
的上下文中,并记录值(...)“例
public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {
private static final String TAG = "MyFirebaseIIDService";
/**
* Called if InstanceID token is updated. This may occur if the security of
* the previous token had been compromised. Note that this is called when the InstanceID token
* is initially generated so this is where you would retrieve the token.
*/
@Override
public void onTokenRefresh() {
// Get updated InstanceID token.
String refreshedToken = FirebaseInstanceId.getInstance().getToken();
Log.d(TAG, "Refreshed token: " + refreshedToken);
// If you want to send messages to this application instance or
// manage this apps subscriptions on the server side, send the
// Instance ID token to your app server.
sendRegistrationToServer(refreshedToken);
}
// ...
}
关于java - 应用程序在FirebaseInstanceId.getInstance()。getToken()时崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45307018/