Reputation: 11
I´m trying to get token by using FirebaseInstanceId.getInstance().getToken()
in my MainActivity.java
but when I use this method to get token, app stars crashing.
Here's my onCreate() method
@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 (Project Level)
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 (App level)
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)
Upvotes: 1
Views: 2893
Reputation: 881
Add the below lines to your AndroidManifest.xml
<service android:name="com.google.firebase.components.ComponentDiscoveryService" >
<meta-data
android:name="com.google.firebase.components:com.google.firebase.iid.Registrar"
android:value="com.google.firebase.components.ComponentRegistrar" />
It worked in my case....
Upvotes: 1
Reputation: 75788
java.lang.NoSuchMethodError: No static method getNoBackupFilesDir(Landroid/content/Context;)Ljava/io/File; in class Lcom/google/android/gms/common/util/zzx;
FYI
ASAIK getNoBackupFilesDir
is present in android.support.v4.content.ContextCompat.
support-v4:25.3.1
from your build.gradle dependency section.Clean-Rebuild and Run
.It will be better if you use Latest version.
android {
// ...
compileSdkVersion 26
buildToolsVersion '26.0.1'
defaultConfig {
targetSdkVersion 26
}
// ...
}
Add
repositories {
maven { url 'https://maven.google.com' }
}
And
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'
// ...
}
NOTE
FirebaseInstanceId.getInstance().getToken()
Call to retrieve the current token . If the token has not yet been created, null is returned.
As the official guideline about Set up a Firebase cloud messaging client app on Android.
indicates:
"The
onTokenRefreshcallback
fires whenever a new token is generated, so callinggetToken
in its context ensures that you are accessing a current, available registration token. Make sure you have added the service to your manifest, then callgetToken
in the context ofonTokenRefresh
, and log the value (…)"
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);
}
// ...
}
Upvotes: 1
Reputation: 723
Try to use the same versions of firebase SDK and support SDK too.
Check dependency tree:
./gradlew :{yourappmodulename}:dependencies --configuration compile
If you check the tree you will see the different versions:
You can fix it if you update firebase to 9.8.0 and support SDK to 25.3.1.
Upvotes: 0