Miguel Mesquita Alfaiate
Miguel Mesquita Alfaiate

Reputation: 2968

Firebase Notifications integration onTokenRefresh not getting called

I am trying to enable firebase notifications and I can't seem to get it working.

If feels like I am missing a step here, but I am not sure what.

I have integrated firebase auth and firebase database in the same application, and both of these are working as expected.

I have added 2 services to my android manifest:

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

<service
    android:name="tld.domain.app.lib.firebase.MyFirebaseInstanceIDService">
    <intent-filter>
        <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
    </intent-filter>
</service>

I can Ctrl+click the classes so I am pointing the names to the right location.

I have also added a breakpoint to onTokenRefresh method of the class:

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.
     */
    // [START refresh_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);
    }
    // [END refresh_token]

    /**
     * Persist token to third-party servers.
     *
     * Modify this method to associate the user's FCM InstanceID token with any server-side account
     * maintained by your application.
     *
     * @param token The new token.
     */
    private void sendRegistrationToServer(String token) {
        Log.d(TAG, "sendRegistrationToServer: " + token);

        // TODO: Implement this method to send token to your app server.
    }

}

Why is this not being called? What am I missing here?

Upvotes: 0

Views: 162

Answers (1)

Umang
Umang

Reputation: 1070

This receiver will only be triggered/called when to token is refreshed. For initial registration you need to explicitly call the registration method.

The app needs to call the below method to get the push token. Token will not be generated on its own

FirebaseInstanceId.getInstance().getToken()

Refer to the official example for more details. Exact code snippet

Upvotes: 1

Related Questions