
我正在实现推送通知,但是在调用getToken时我收到了TIMEOUT异常.
此问题仅在某些设备上发生,如SC-03D(4.0).
这是我用来注册令牌的IntentService:
public class RegistrationIntentService extends IntentService {private static final String TAG = "GCM";public static final String TOKEN_ID = "registration_ID";/** * Constructor */public RegistrationIntentService() { super(TAG);}@OverrIDeprotected voID onHandleIntent(Intent intent) { try { // In the (unlikely) event that multiple refresh operations occur simultaneously, ensure that they are processed sequentially. synchronized (TAG) { // Initially this call goes out to the network to retrIEve the token, subsequent calls are local. InstanceID instanceID = InstanceID.getInstance(this); String gcm_sender_ID = getString(R.string.gcm_sender_ID); String token = instanceID.getToken(gcm_sender_ID, GoogleCloudMessaging.INSTANCE_ID_ScopE, null); String storagetoken = PrefsHelper.getTokenID(this); Log.d(TAG, "GCM Registration Token: " + token); } } catch (Exception e) { Log.d(TAG, "Failed to complete token refresh", e); }}解决方法:
您需要尝试使用指数后退来注册令牌
以下代码可能对您有帮助
public class RegistrationIntentService extends IntentService { private static final String TAG = "GCM"; public static final String TOKEN_ID = "registration_ID"; private final static int MAX_ATTEMPTS = 5; private final static int BACKOFF_MILli_SECONDS = 2000; /** * Constructor */ public RegistrationIntentService() { super(TAG); } @OverrIDe protected voID onHandleIntent(Intent intent) { // In the (unlikely) event that multiple refresh operations occur simultaneously, ensure that they are processed sequentially. synchronized (TAG) { Random random = new Random(); String token = null; InstanceID instanceID = InstanceID.getInstance(this); long backoff = BACKOFF_MILli_SECONDS + random.nextInt(1000); for (int i = 1; i <= MAX_ATTEMPTS; i++) { try { token = instanceID.getToken(getString(R.string.gcm_sender_ID);, GoogleCloudMessaging.INSTANCE_ID_ScopE, null); if(null != token && !token.isEmpty()) { break; } } catch (IOException e) { //Log exception } if (i == MAX_ATTEMPTS) { break; } try { Thread.sleep(backoff); } catch (InterruptedException e1) { break; } // increase backoff exponentially backoff *= 2; } // further processing for token goes here } }有关更多信息see this
总结以上是内存溢出为你收集整理的GCM getToken()在某些设备上发送java.io.IOException:TIMEOUT全部内容,希望文章能够帮你解决GCM getToken()在某些设备上发送java.io.IOException:TIMEOUT所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)