2

I know there are a few other posts similar to mine but I've gone through the list of solutions with no results. My Android app has a very simple firebase implementation that is supposed to retrieve the token on startup and pass it via callback to the main activity for use later. I've heard that the token is supposed to be generated on startup, but during the first app install, I never receive it and my app crashes. If I install my app again (without removing it first), everything works normally and I get my token.

I've tried:

  • Updating and consolidating my firebase dependencies the app's build.gradle file
  • Confirming that my Manifest is using the correct firebase services / internet permissions
  • Deleting my instance id with FirebaseInstanceId.getInstance().deleteInstanceId() before manually calling getToken()
  • Testing on multiple devices

Looking at my logcat during first install, I get these two firebase errors:

E/FA: Discarding data. Failed to send app launch
E/FA: Failed to get app instance id

My InstanceIdService:

public class InstanceIDService extends FirebaseInstanceIdService {

    TokenCallback callback;

    public void initInstanceIDService(TokenCallback cb) {
        this.callback = cb;
    }

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();

        // Get updated token.
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();

        if (refreshedToken != null) {
            callback.onTokenReceived(refreshedToken);
        }
        //pass token to manager for use during authentication
        Log.d("Firebase", "Refreshed token: " + refreshedToken);
    }

    public interface TokenCallback {

        void onTokenReceived(String token);
    }
}

My Manifest:

        <service
            android:name="com.projectname.network.MessagingService">
            <intent-filter>
                <action android:name="com.google.firebase.MESSAGING_EVENT" />
            </intent-filter>
        </service>
        <service
            android:name="com.projectname.network.InstanceIDService">
            <intent-filter>
                <action android:name="com.google.firebase.INSTANCE_ID_EVENT" />
            </intent-filter>
        </service>

My Gradle dependencies:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    compile 'com.google.firebase:firebase-core:11.0.4'
    compile 'com.google.firebase:firebase-messaging:11.0.4'
    compile 'com.android.support:appcompat-v7:+'
}
apply plugin: 'com.google.gms.google-services'

I'm testing on physical devices and all return the same behavior.

If anyone has had experience with this problem, any suggestions or solutions would be greatly appreciated.

deMouser
  • 369
  • 1
  • 2
  • 14
  • A couple of other posts, for example [this one](https://stackoverflow.com/q/45054620/4815718), show the same error messages, apparently caused by running on a device that has an old version of Google Play Services. Does your logcat include a message for `Google Play services out of date`? – Bob Snyder Aug 23 '17 at 16:49
  • Possible duplicate of [Firebase FCM force onTokenRefresh() to be called](https://stackoverflow.com/questions/37454501/firebase-fcm-force-ontokenrefresh-to-be-called) – Yoni Aug 23 '17 at 16:51
  • @Bob Snyder - I checked and that was the problem. I downgraded my Firebase dependencies and the issue was resolved. If you post your comment as an answer, I'll gladly accept it. Thank you for your help! – deMouser Aug 24 '17 at 15:37

1 Answers1

1

This problem can occur when the device or emulator is running an old version of Google Play Services that is not compatible with the version of the Firebase libraries used. When this condition exists, the logcat will contain this warning message:

W/GooglePlayServicesUtil: Google Play services out of date.  Requires XXXXXXXX but found XXXXXXXX

The solution is to update Google Play Services, or downgrade the version of Firebase.

Because the Firebase APIs require a compatible version of Google Play Services, it's good practice to confirm the presence of a compatible version during app initialization using GoogleApiAvailability.

Bob Snyder
  • 37,759
  • 6
  • 111
  • 158