kostik
kostik

Reputation: 693

(Android) Firebase cloud messaging - ontokenrefresh never called on specific device

I was testing fcm on two devices.

On one device with API level 23 onTokenRefresh method was called.

On another device with API level 19 the onTokenRefresh method was never called.

//MyFirebaseInstanceIdService class

public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {

    private static final String TAG = "MyFirebaseIIDService";

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

        sendRegistrationToServer(refreshedToken);
    }

//manifest

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

//build.gradle

implementation 'com.google.firebase:firebase-messaging:11.6.0'
implementation 'com.google.firebase:firebase-core:11.6.0'

//build.gradle

classpath 'com.google.gms:google-services:3.2.0'

Reinstalling app did not help solve this problem.

Upvotes: 0

Views: 364

Answers (1)

AL.
AL.

Reputation: 37778

I just gave an answer to a similar question.

Expecting onTokenRefresh() to be called on the first install is not the usual case. From my linked answer:

The onTokenRefresh() method doesn't necessarily get called every time the app starts or if it is newly installed. It only triggers on some scenarios (see my answer here).

...

The getToken() method can immediately return the current token or null. If its null, it means that the FCM server is not yet finished generating or sending the token back to the client, in which case onTokenRefresh() would trigger upon completion.

Upvotes: 1

Related Questions