GCM getToken()在某些设备上发送java.io.IOException:TIMEOUT
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了GCM getToken()在某些设备上发送java.io.IOException:TIMEOUT,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2247字,纯文字阅读大概需要4分钟。
内容图文
我正在实现推送通知,但是在调用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);
}
@Override
protected 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所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。