我正在实现推送通知功能,但在调用 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);
}
}