0

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?

Miguel Mesquita Alfaiate
  • 2,851
  • 5
  • 30
  • 56

1 Answers1

1

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

Umang
  • 966
  • 2
  • 7
  • 17
  • this answer says exactly the opposite: https://stackoverflow.com/questions/37451395/firebase-ontokenrefresh-is-not-called how should I call the registration method? Can you be more specific? – Miguel Mesquita Alfaiate Jan 11 '18 at 17:06
  • Updated the answer, the answer is right for the initial versions on firebase library the mechanism has changed now. The git repo shared is maintained by the firebase team you can refer it for best practise and guidance. – Umang Jan 11 '18 at 17:11
  • I had to uninstall the app and install it again to be able to trigger onTokenRefresh. Apparently token was already generated, and all I had to call was the code you mentioned above from anywhere in my code, so I will mark yours as an answer. – Miguel Mesquita Alfaiate Jan 11 '18 at 17:24