Firebase onTokenRefresh() is not called

2019-01-07 14:21发布

In my MainActivityin my log, I can see the token using FirebaseInstanceId.getInstance().getToken() and it display the generated token. But it seems like in my MyFirebaseInstanceIDService where it is extends to FirebaseInstanceIdService, the onTokenRefresh() is not called, where in this function it was said that the token is initially generated here. I needed to call sendRegistrationToServer() that's why I'm trying to know why it doesn't go in the onTokenRefresh().

Here is my code

public class MyFirebaseInstanceIDService  extends FirebaseInstanceIdService {


@Override
public void onTokenRefresh() {
    // Get updated InstanceID token.
    String refreshedToken = FirebaseInstanceId.getInstance().getToken();
    Log.d(TAG, "Refreshed token: " + refreshedToken);

        sendRegistrationToServer(refreshedToken);
    }
}

13条回答
Lonely孤独者°
2楼-- · 2019-01-07 14:47
try {
    FirebaseInstanceId.getInstance().deleteInstanceId();
} catch (IOException e) {
    e.printStackTrace();
}

in onCreate method to delete token.

查看更多
淡お忘
3楼-- · 2019-01-07 14:50

I had the same problem like you. My mistake was the following: I placed my <service> tags outside the <application> tag in the AndroidManifest.xml file.

Now mine looks like this:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <service
        android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>

    <service
        android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>
</application>

And it works without any problem.

查看更多
来,给爷笑一个
4楼-- · 2019-01-07 14:51

There are several reasons to not calling the onTokenRefresh methon not called. I have listed that and mention it one by one .

  1. Please make sure you have added the internet permission

  2. check have you added the google-services.json file that generate from google console inside of app directory in your project

enter image description here

  1. In your module Gradle file (usually the app/build.gradle), add the apply plugin line at the bottom of the file to enable the Gradle plugin:
apply plugin: 'com.android.application'

android {
  // ...
}

dependencies {
  // ...
  implementation 'com.google.firebase:firebase-core:16.0.4'

  // Getting a "Could not find" error? Make sure you have
  // added the Google maven respository to your root build.gradle
}

// ADD THIS AT THE BOTTOM
apply plugin: 'com.google.gms.google-services'

4) add the play service class path in project level build.gridle file

buildscript {
    // ...
    dependencies {
        // ...
        classpath 'com.google.gms:google-services:4.1.0' // google-services plugin
    }
}

allprojects {
    // ...
    repositories {
        // ...
        google() // Google's Maven repository
    }
}

5) Make sure you have added those service in the AndroidManifes file inside application tag

<service
    android:name=".MyFirebaseMessagingService">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT"/>
    </intent-filter>
</service>

<service
    android:name=".MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service> </application>

6) onToken refresh is called when token refresh and when install the app for the first so make sure you have deleted the app manually or clear the data

7) Make sure you have extends the FirebaseMessagingService in your service class

查看更多
在下西门庆
5楼-- · 2019-01-07 14:58

I have experienced that MyFirebaseInstanceIDService is not called when you are running your application on a device where internet connectivity is not available. In this way onTokenRefresh() is not called. So make sure that your device must has internet connection during running your application for taking updated FirebaseToken.

Also uninstall the previous app & Re-Install it for taking updated token. The registration token change when:

1) The app deletes Instance ID

2) The app is restored on a new device

3) The user uninstalls/reinstall the app

4) The user clears app data.

查看更多
霸刀☆藐视天下
6楼-- · 2019-01-07 15:00

Make sure you have added

apply plugin: 'com.google.gms.google-services'

this to bottom of the app.gradle file

查看更多
孤傲高冷的网名
7楼-- · 2019-01-07 15:00

I had the same problem. My device(kitkat 4.4.2) couldn't get onTokenRefresh() for some reason. My internet connection was perfect, google play services was up to date and all necessary contiditions for firebase to work were met. My code worked perfectly on the devices 5.0.0 and higher. To solve this issue i had to reset my device to factory settings and application started to work. I know it's tough measure but maybe it may help somebody. There must be some problems or conflict with other application, wich caused onTokenRefresh() not to be called. Good luck!

查看更多
登录 后发表回答