当我使用Spring模块实现Robospice时,GCM服务停止工作

时间:2015-02-09 14:18:37

标签: google-cloud-messaging robospice

我已按照Google开发者页面(Gcm implementation)上的说明实施了标准GCM客户端。推送消息在那里工作没有问题。但后来我在应用程序中实现了Robospice REST lib,GCM停止了工作。我确信它不起作用因为Robospice,因为一旦我删除了SpiceManager的代码,GCM就开始工作了。

通过Robospice Spring模块调用API工作正常。这是我对Robospice服务的声明:

enter<receiver
                  android:name="com.clover.spika.enterprise.chat.services.gcm.GcmBroadcastRe    ceiver"
        android:exported="true"
                android:permission="com.google.android.c2dm.permission.SEND" >
        <intent-filter>
            <action      android:name="com.google.android.c2dm.intent.RECEIVE" />
            <action      android:name="com.google.android.c2dm.intent.REGISTRATION" />

            <category     android:name="com.clover.spika.enterprise.chat.gcm" />
        </intent-filter>
    </receiver>

    <service     android:name="com.clover.spika.enterprise.chat.services.gcm.GcmIntentService" />
    <service
           android:name="com.clover.spika.enterprise.chat.services.robospice.Jackson2   SpiceService"
        android:exported="false" />
    <service    android:name="com.clover.spika.enterprise.chat.services.custom.PoolingServ   ice" /> code here

这是我的自定义spice服务:

package com.clover.spika.enterprise.chat.services.robospice;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.springframework.http.ContentCodingType;
 import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpRequest;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import         org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import android.app.Application;
import android.app.Notification;

import com.octo.android.robospice.SpringAndroidSpiceService;
import com.octo.android.robospice.persistence.CacheManager;
import     com.octo.android.robospice.persistence.exception.CacheCreationException;
import     com.octo.android.robospice.persistence.springandroid.json.jackson.JacksonO    bjectPersisterFactory;

public class Jackson2SpiceService extends SpringAndroidSpiceService {

// private static final int WEBSERVICES_TIMEOUT = 10000;

@Override
public RestTemplate createRestTemplate() {

    RestTemplate restTemplate = new RestTemplate() {

        @Override
        protected ClientHttpRequest createRequest(URI url, HttpMethod method) throws IOException {
            ClientHttpRequest request = super.createRequest(url, method);
            HttpHeaders headers = request.getHeaders();
            headers.setAcceptEncoding(ContentCodingType.ALL);
            return request;
        }
    };

    // bug on http connection for Android < 2.2
    // http://android-developers.blogspot.fr/2011/09/androids-http-clients.html
    // but still a problem for upload with Spring-android on android 4.1
    // System.setProperty("http.keepAlive", "false");

    // // set timeout for requests
    // ClientHttpRequestFactory factory = restTemplate.getRequestFactory();
    // if (factory instanceof HttpComponentsClientHttpRequestFactory) {
    // HttpComponentsClientHttpRequestFactory advancedFactory =
    // (HttpComponentsClientHttpRequestFactory) factory;
    // advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
    // advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
    // } else if (factory instanceof SimpleClientHttpRequestFactory) {
    // SimpleClientHttpRequestFactory advancedFactory =
    // (SimpleClientHttpRequestFactory) factory;
    // advancedFactory.setConnectTimeout(WEBSERVICES_TIMEOUT);
    // advancedFactory.setReadTimeout(WEBSERVICES_TIMEOUT);
    // }

    MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
    FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
    // StringHttpMessageConverter stringHttpMessageConverter = new
    // StringHttpMessageConverter();

    List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
    supportedMediaTypes.add(MediaType.TEXT_HTML);
    supportedMediaTypes.add(MediaType.APPLICATION_JSON);

    // stringHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
    jsonConverter.setSupportedMediaTypes(supportedMediaTypes);

    final List<HttpMessageConverter<?>> listHttpMessageConverters = restTemplate.getMessageConverters();

    listHttpMessageConverters.add(jsonConverter);
    listHttpMessageConverters.add(formHttpMessageConverter);
    // listHttpMessageConverters.add(stringHttpMessageConverter);

    restTemplate.setMessageConverters(listHttpMessageConverters);

    return restTemplate;
}

@Override
public CacheManager createCacheManager(Application application) throws CacheCreationException {

    CacheManager cacheManager = new CacheManager();
    JacksonObjectPersisterFactory jacksonObjectPersisterFactory = new     JacksonObjectPersisterFactory(application);
    cacheManager.addPersister(jacksonObjectPersisterFactory);

    return cacheManager;
}

@Override
public Notification createDefaultNotification() {
    return null;
}

}

有谁知道可能是什么问题?如果需要,我可以发布其他代码。

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题,它不是robospice或gcm错误,这是我们服务器处理中的一件事。

问题是我们从User-Agent标题中读取了一些我不知道的值,并且我没有将它设置为OkHttp客户端。