-1

I have initiated MyFirebaseInstanseIdService in order to get token, directly i will show you the code :

    @Override
    public void onTokenRefresh() {
        super.onTokenRefresh();
        String refreshedToken = FirebaseInstanceId.getInstance().getToken();


        storeRegIdInPref(refreshedToken);


        sendRegistrationToServer(refreshedToken);


        Intent registrationComplete = new Intent(Config.REGISTRATION_COMPLETE);
        registrationComplete.putExtra("token", refreshedToken);
        LocalBroadcastManager.getInstance(this).sendBroadcast(registrationComplete);
    }

    private void sendRegistrationToServer(final String token) {
        // sending gcm token to server
        Log.e(TAG, "sendRegistrationToServer: " + token);
    }

    private void storeRegIdInPref(String token) {
        SharedPreferences pref = getApplicationContext().getSharedPreferences(Config.SHARED_PREF, 0);
        SharedPreferences.Editor editor = pref.edit();
        editor.putString("regId", token);
        editor.commit();
    }

My manifest.xml :

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

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

I tried to get token via broadCast in my activity also with shared prefrences, but it seems this class didn't invoked.

In my log i see those lines :

D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.
D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.

and this

I/FirebaseInitProvider: FirebaseApp initialization successful
  • 3
    As AL says in his answer: your token has likely already been created, so `onTokenRefresh` doesn't get called. Either uninstall/reinstall the app to force a token refresh, or [get the current token](https://firebase.google.com/docs/cloud-messaging/android/client#retrieve-the-current-registration-token) by putting `FirebaseInstanceId.getInstance().getToken();` in your main view's `onCreate`. – Frank van Puffelen Jul 16 '17 at 13:52
  • 1
    @FrankvanPuffelen i faced this issue before, and like you said, just **uninstall/reinstall**. –  Jul 16 '17 at 20:43
  • 2
    That indeed works and is fine during development, which is when most developers encounter this behavior. But if you've already released the app, you can't ask your users to do the same. So instead you need to handle both cases: read the token `FirebaseInstanceId.getInstance().getToken()` in the main view and also monitor `onTokenRefresh()`. – Frank van Puffelen Jul 16 '17 at 20:56
  • @FrankvanPuffelen @Ibrahim it's worked after uninstall, and i will try to get `FirebaseInstanceId.getInstance().getToken()` in the main view. tanks –  Jul 17 '17 at 06:21

1 Answers1

2

The onTokenRefresh() method only gets called when the current token expires. The current token could expire for the following reasons:

  • App deletes Instance ID

  • App is restored on a new device

  • User uninstalls/reinstall the app

  • User clears app data

There's also the scenario where the first time you call getToken(), it might return a null value, from where you should expect onTokenRefresh() to trigger.

With that, you should call getToken() on your initial activity to generate a token.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • Thanks for response, it's worked after uninstall app. –  Jul 17 '17 at 06:21
  • @ArduinoAndroid You're welcome. If you think this completely answers your question, you could mark it as accepted by click on the checkmark on the left side of the post. Cheers! :) – AL. Jul 17 '17 at 06:38