Search code examples
androidrestandroid-annotations

Firebase, Rest and AndoidAnnotiations


I want to add remote notifications to an existing android app. I followed a Firebase tutorial to do so. So I extended "FirebaseInstanceIdService" to get the token. This works fine, but now I want to send this token a REST service.

Here is my "FirebaseInstanceIdService", but this isn't working :( Client is always null. I also use this code inside an Activity for other Requests to the REST service, there it works fine...

Any suggestions on what is wrong here?

@EBean
public class MyFirebaseInstanceIdService extends FirebaseInstanceIdService {

    @Bean
    MyAuthInterceptor authInterceptor;
    @RestService RestClient client;

    public RestClient getRestClient()
    {
        return client;
    }

    @AfterInject
    void initAuth()
    {
        RestTemplate template = client.getRestTemplate();
        List<ClientHttpRequestInterceptor> interceptors = new ArrayList<ClientHttpRequestInterceptor>();;
        interceptors.add(authInterceptor);
        template.setInterceptors(interceptors);

        List<HttpMessageConverter<?>> converter = new ArrayList<HttpMessageConverter<?>>();
        converter.add(new GsonHttpMessageConverter());
        template.setMessageConverters(converter);
    }

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

        DeviceTokenJson token = new DeviceTokenJson();
        token.token = refreshedToken;
        token.env = "prod";
        token.os = "android";
        token.disabled = 0;

        RestClient test = getRestClient();
        ResponseJsonNotification response = getRestClient().registerToken(token);
    }
}

Update:

As suggested, I removed the static from the RestClient and added "MyFirebaseInstanceIdService_" to the manifest like this:

    <service
        android:name=".classes.MyFirebaseInstanceIdService_">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

Now I get an MyFirebaseInstanceIdService_ has no default constructor error message...

And when I start the App is crashes with:

java.lang.RuntimeException: Unable to instantiate service .classes.MyFirebaseInstanceIdService_: java.lang.InstantiationException: java.lang.Class<.classes.MyFirebaseInstanceIdService_> has no zero argument constructor


Solution

  • You have the wrong annotation for MyFirebaseInstanceIdService, since it's a service you have to use @EService

    @EService
    public class MyFirebaseInstanceIDService extends FirebaseInstanceIdService {...}